35

I'm fairly familiar with the default bash shell shortcuts (emacs style). For example, if I want to move my cursor forward by a word I would press Alt+F (same as you would in emacs). I can get to the previous command with Ctrl+P.

Is it even possible in windows powershell to override these shortcuts? If so, does anyone know of a good way to map the default bash shell shortcut configurations to powershell (in a bash shell you can say set -o emacs, for example)?

If it helps, I don't really care about powershell per say, I care about posh-git shell. However, git-shell inherits its properties from powershell from what I can tell.

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52

1 Answers1

77

You can do that with the PSReadLine module which ships with PowerShell 5.0 (Windows 10).

Simply set the EditMode option to Emacs:

Set-PSReadLineOption -EditMode Emacs

In addition to the default Windows mode, it also has a Vi mode.

The project README file mentions that neither are fully implemented so don't expect full feature parity, but I can confirm that it switches tab completion to bash-style and supports the Alt+F and Ctrl+P key bindings for moving forward and jumping to the previous command respectively

(I'm not an experienced emacs user so I'm unable to tell whether this comes "close enough" to what you expect)

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • That did work for powershell. Thanks! Unfortunately, the posh-git shell still didn't like it when I included this command in the default profile. Have you used the posh-git shell at all? – Matt Messersmith Apr 08 '17 at 15:54
  • @mwm314 No, unfortunately I don't use posh-git (your question said "git-shell", I tested with the Git Shell that comes with GitHub Desktop for Windows) – Mathias R. Jessen Apr 08 '17 at 18:56
  • 3
    Ctrl + A, Ctrl -E, Ctrl -P, Ctrl-N work well at least. Very helpful – bzhu Dec 07 '19 at 14:46
  • 4
    This setting only lives in the current session. how could I make it take effect permanently – yuzhen May 15 '20 at 08:32
  • 4
    @yuzhen Add it to your [profile](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles)! – Mathias R. Jessen May 15 '20 at 10:40
  • @MathiasR.Jessen Thanks, BTW, you have to generate a Powershell profile script by typing $profile, secondly, grant Set-ExecutionPolicy to unrestricted, finally paste your run time settings into your newly created script and restart PowerShell. not so easy. – yuzhen May 22 '20 at 01:56
  • Vim $profile Set-PSReadLineOption -EditMode Emacs Add the above line into $profile Set-ExecutionPolicy unrestricted Restart powershell ( How to make it permanently emacs style) – yuzhen Mar 11 '21 at 01:36