powershellvipsreadline

Is there a way to map CTRL+[ to escape in Powershell?


I discovered there is a vi mode in powershell, however it appears CTRL+[ is not mapped to escape like it is in vi and bash. I added the setting to my profile like this:

New-Item $profile -Type File -Force
echo "Set-PSReadLineOption -EditMode vi" >> $profile

I read through the docs here and found nothing relavant to my problem. The current solution I am considering is an AutoHotKey script that remaps the keys when a Powershell window is present. Please let me know if anyone knows of a more native way to accomplish this. I am also thinking that a system-wide change via a regedit or something similar wouldn't work as I need CTRL+[ to function normally outside of the powershell window.


Solution

  • I found a command that does the trick

    Set-PSReadlineKeyHandler -Chord Ctrl+[ -Function ViCommandMode
    

    Edit: I figure I can share a bit more about how I found this answer. Running this command

     Get-PSReadLineKeyHandler -Bound -Unbound
    

    will give you a list of all available mappings as described here. I located escape which was bound to the function ViCommandMode which I used to construct the solution pasted above.