23

Is there a way to get vim keybindings in powershell as you would in bash or zsh with a command like

set -o vi
moshen
  • 1,534
  • 1
  • 9
  • 13
Bryan Ward
  • 395
  • 1
  • 3
  • 10
  • posted a possible answer here: http://stackoverflow.com/questions/28067589/powershell-ise-vim/30458056#30458056 (Powershell ISE + vim), but more of a workaround – MacMartin May 26 '15 at 12:10

5 Answers5

16

PSReadline module provides this.

To install is simple not sure what the requirements are but I think at least windows 10 for this method provided. If you dont have OneGet or not sure you can follow the instructions at PSReadline

To install PSReadline without elevating privileges

Install-Module PsReadline -Scope CurrentUser

Check you have a powershell profile

Test-Path $PROFILE

If you dont create one

New-File -Type File $PROFILE

Open $PROFILE in ISE

ise $PROFILE

Add this line

Set-PSReadlineOption -EditMode vi

Reload $PROFILE or just restart powershell

& $PROFILE
strings
  • 261
  • 2
  • 3
  • 3
    It's New-Item -Type File – Alex Jan 27 '18 at 02:23
  • And PS refused to run an unsigned profile. There's a discussion on doing so, here: https://serverfault.com/questions/31194/how-do-i-change-my-default-powershell-profile-digitially-sign-my-profile-file – Screenack Aug 10 '21 at 13:28
14

These days you have a couple of options. With the default console you can just run Vim on Windows (you might already have it if you have git or other tools installed). To make the integration more seamless place this in your PowerShell profile:

$VIMEXEPATH    = "c:\<location>\vim.exe"

Set-Alias vim  $VIMEXEPATH
Set-Alias vi   $VIMEXEPATH

I'd also recommend adding the following to your vimrc file so it executes shell commands with PowerShell itself.

set shell=powershell
set shellcmdflag=-command

Otherwise the only ISE that supports a Vi mode right now is PSReadLine now available on the PS Gallery

There are usage notes but in brief for VI mode you need to add the following line to your ISE profile.

Set-PSReadlineOption -EditMode vi

Note that Vi support was added in 1.2 but 1.1 shipped with Windows 10. You will need to use the -force parameter to update shipping (aka manually installed) version.

Lex
  • 296
  • 1
  • 5
  • 1
    Support for vi mode was added via PsReadline in PowerShell 5. See @strings answer below... – Lucas Aug 08 '16 at 15:00
  • 2
    Is there a way to have the cursor change so that you can see if you are in normal or insert mode? – bradgonesurfing Jun 19 '18 at 12:28
  • I don't think so, however another beta for version 2 released the other week. So you could try that, and if it isn't there I'd try asking the maintainers it might motivate them to support it (and continue development in general) – Lex Jun 20 '18 at 13:55
  • Also, if you followed the internet rabbit hole here, you may also want: Set-PSReadLineOption -ViModeIndicator Cursor – stevens Jun 18 '21 at 15:00
1

I wouldn't swear that there isn't an undocumented feature of Powershell that allows this, but as far as I know the answer is no. At least I have never seen anything in Powershell documentation to suggest it's possible.

JR

John Rennie
  • 7,756
  • 1
  • 22
  • 34
0

As it happens, you can load PowerShell syntax-highlighting into VIM. It's what I use for script development.

http://www.vim.org/scripts/script.php?script_id=1327

While it isn't in the shell itself, it's handy for building longer scripts. I have one window in VIM, and have a PowerShell prompt in another. I find it very handy.

sysadmin1138
  • 131,083
  • 18
  • 173
  • 296
0

The powershell engine now offers the hooks to make this relatively straightforward but I don't know of anyone who has done it yet.

The PSReadln project has an emergant emacs mode, though it is in its infancy. Adding a vi mode in PSReadln would be the obvious thing to do but I don't have time to devote to it.