Skip to content

Instantly share code, notes, and snippets.

@rkttu
Last active June 6, 2023 09:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkttu/f319745e821c36b8be1d8cdc6d0e6709 to your computer and use it in GitHub Desktop.
Save rkttu/f319745e821c36b8be1d8cdc6d0e6709 to your computer and use it in GitHub Desktop.
PowerShell 7 Profile Code for macOS
# Apply this code to the path pointed to by the $PROFILE variable.
# Introduction
# This code includes asdf and oh-my-posh support in addition to brew.
# Requirements
# You must have the latest version of the PSReadLine module installed. Use the Install-Module -Name PSReadLine -AllowClobber -Force command.
# Also, you need to go to Nerd Font (https://www.nerdfonts.com/) and change the font to display the oh-my-posh prompt normally.
# Troubleshooting
# If it says that brew-related programs cannot be found, check if the brew path is registered in /etc/paths.d.
function Get-Path {
[CmdLetBinding()]
Param()
$PathFiles = @()
$PathFiles += '/etc/paths'
$PathFiles = Get-ChildItem -Path /private/etc/paths.d | Select-Object -Expand FullName
$PathFiles | ForEach-Object {
Get-Content -Path $PSItem | ForEach-Object {
$_
}
}
$Paths
}
function Add-Path {
Param($Path)
$env:PATH = "${env:PATH}:$Path"
}
function Update-Environment{
[CmdLetBinding()]
Param()
$Paths = $env:PATH -split ':'
Get-Path | ForEach-Object {
If ($PSItem -notin $Paths) {
Write-Verbose "Adding $PSItem to Path"
Add-Path -Path $PSItem
}
}
}
Update-Environment
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -EditMode Window
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
$parameters = @{
Key = 'Alt+w'
BriefDescription = 'SaveInHistory'
LongDescription = 'Save current line in history but do not execute'
ScriptBlock = {
param($key, $arg) # The arguments are ignored in this example
# GetBufferState gives us the command line (with the cursor position)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
# AddToHistory saves the line in history, but does not execute it.
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
# RevertLine is like pressing Escape.
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}
}
Set-PSReadLineKeyHandler @parameters
$env:POSH_THEMES_PATH = Join-Path -Path "$(brew --prefix oh-my-posh)" -ChildPath themes
[scriptblock]::Create($(oh-my-posh completion powershell)).Invoke()
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/clean-detailed.omp.json" | iex
$env:PATH = "$(brew --prefix asdf):${env:HOME}/.asdf/shims:${env:PATH}"
# $env:DOCKER_DEFAULT_PLATFORM="linux/amd64"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment