For developers accustomed to Vim's modal editing, switching between PowerShell and their preferred editor can feel jarring. Unlike Unix-like shells that offer set -o vi
, PowerShell requires a different approach due to its different architecture.
Modern PowerShell (5.1+) includes the PSReadLine module which provides basic Vim emulation:
Set-PSReadLineOption -EditMode Vi
Set-PSReadLineOption -ViModeIndicator Cursor
For a richer Vim experience, create a PowerShell profile with these customizations:
# ~\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Set-PSReadLineOption -EditMode Vi
Set-PSReadLineOption -BellStyle None
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineKeyHandler -Key Tab -Function Complete
Add Vim-style navigation to your PowerShell session:
Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+u' -Function BackwardDeleteLine
Set-PSReadLineKeyHandler -Chord 'Ctrl+a' -Function BeginningOfLine
Set-PSReadLineKeyHandler -Chord 'Ctrl+e' -Function EndOfLine
Enable visual selection mode with these additions:
Set-PSReadLineKeyHandler -Chord 'v' -Function ViCommandMode
Set-PSReadLineKeyHandler -Chord 'y' -Function Copy
Set-PSReadLineKeyHandler -Chord 'p' -Function Paste
While PSReadLine provides good basic Vim emulation, some advanced features like macros aren't available. Consider these alternatives:
- Use Windows Terminal with WSL and a proper bash/zsh setup
- Try the ConsoleZ terminal with PowerShell
- Use Vim itself with PowerShell commands via
:! pwsh -c "command"
If keybindings don't work as expected:
- Check PowerShell version (
$PSVersionTable
) - Verify PSReadLine is loaded (
Get-Module PSReadLine
) - Ensure your profile script is executing (
Test-Path $profile
)
As a longtime Vim user transitioning to Windows development, I desperately missed my muscle memory when working in PowerShell. The solution exists, but it's not as straightforward as Unix-like systems' set -o vi
command.
PowerShell 7+ includes PSReadLine which supports basic Vim mode:
Set-PSReadLineOption -EditMode Vi
For a richer Vim experience, add this to your $PROFILE
:
Set-PSReadLineOption -EditMode Vi
Set-PSReadLineOption -ViModeIndicator Cursor
Set-PSReadLineOption -BellStyle None
Set-PSReadLineKeyHandler -Key Ctrl+r -Function ReverseSearchHistory
Some particularly useful Vim bindings in PowerShell:
Esc
- Switch to normal modei
- Return to insert modeh,j,k,l
- Navigation (works in normal mode)dd
- Delete current linecw
- Change word
PowerShell's profile is the equivalent of .vimrc
for these bindings. Example customizations:
Set-PSReadLineKeyHandler -Key Ctrl+u -Function BackwardDeleteLine
Set-PSReadLineKeyHandler -Key Ctrl+w -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Key '/' -Function SearchHistory
Common issues and fixes:
# If bindings stop working
Remove-Module PSReadLine -Force; Import-Module PSReadLine
# To check your current mode
Get-PSReadLineOption | Select-Object EditMode
For true Vim enthusiasts, consider these alternatives:
- Windows Terminal + neovim integration
- VSCode with Vim extension for PowerShell ISE replacement
- ConEmu with custom Vim keybindings
Heavy customization might impact startup time. Profile your $PROFILE
with:
Measure-Command { . $PROFILE }