WindowsPowerShell/readline.ps1

34 lines
1.5 KiB
PowerShell

# Enable Vi line editing mode.
Set-PSReadLineOption -BellStyle None -EditMode Vi
# Make C-w backward delete a word.
Set-PSReadLineKeyHandler -Chord Ctrl+w -Function BackwardDeleteWord
# Make C-[ escape insert mode.
# https://github.com/PowerShell/PSReadLine/issues/906#issuecomment-916847040
Set-PSReadLineKeyHandler -Chord 'Ctrl+Oem4' -Function ViCommandMode
# Enable menu style tab completions. https://stackoverflow.com/a/37715242
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# Autosuggestion bindings for PSReadLine 2.1.0+
Set-PSReadLineKeyHandler -Chord "Ctrl+p" -Function ForwardChar
Set-PSReadLineKeyHandler -Chord "Ctrl+o" -Function ForwardWord
# Use grey but not italic for autosuggestion highlights
Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m" }
# Change cursor shape based on what Vi mode the line editor is in.
# Tested and works with PowerShell 5.1
# NOTE: Requires: Install-Module PsReadline -Scope CurrentUser -Force
# TODO: This doesn't handle all cases well. For example when exiting terminal
# nvim in a PowerShell session the cursor is a block while in insert mode.
function OnViModeChange {
if ($args[0] -eq 'Command') {
# Set the cursor to non-blinking block.
Write-Host -NoNewLine "$([char]0x1b)[2 q"
} else {
# Set the cursor to non-blinking line.
Write-Host -NoNewLine "$([char]0x1b)[6 q"
}
}
Set-PSReadLineOption -ViModeIndicator Script -ViModeChangeHandler $Function:OnViModeChange
# And set the cursor to a non-blinking line on profile load.
Write-Host -NoNewLine "$([char]0x1b)[6 q"