Use CTRL + D to exit and CTRL + L to cls in Powershell console

PowershellKeyboard Shortcuts

Powershell Problem Overview


I am trying to make

CTRL + D - exit Powershell console

and

CTRL + L - clear the screen

like in bash.

So far, I have seen that we can define

function ^D {exit}

but that means I have to do CTRL+D and then hit enter for it to work.

Also, it doesn't even let me define

function ^L {exit}

Is there anyway to add these key bindings in the default Powershell console?

Powershell Solutions


Solution 1 - Powershell

Old question, but with PowerShell 5.1 and PowerShell Core 6.x and later:

Set-PSReadlineKeyHandler -Key ctrl+d -Function ViExit

Solution 2 - Powershell

There is a new library PSReadline for Powershell v3.0 that emulates the GNU Bash tab completion and key bindings. Even CTRL + R for reverse incremental search works. Exactly what I wanted.

Solution 3 - Powershell

If you don't mind relying on an external program, you could do the following with AutoHotKey:

#IfWinActive ahk_class ConsoleWindowClass
^L::SendInput , {Esc}cls{Enter}
^D::SendInput , {Esc}exit{Enter}
#IfWinActive

The above will work with the PowerShell or CMD console. Otherwise the only thing I can think of would be to work up some P/Invoke magic with SetWindowsHookEx.

Edit: Fixed AutoHotkey script to pass through the shortcut keys to other programs.

Solution 4 - Powershell

There is also a PowerShell snapin called PSEventing which will allow you to do this (see the demo on the front page:

http://pseventing.codeplex.com/releases/view/66587

# clear screen in response to ctrl+L, unix style 
register-hotkeyevent "ctrl+L" -action { cls; write-host -nonewline (prompt) }

Solution 5 - Powershell

You can set your PSReadline to emacs mode, it will not only exit with ^D, you will be able to go to beginning of line with ^A, end of the line with ^E

Include this in your profile: Set-PSReadlineOption -EditMode Emacs

I'm using cmder which uses ConEmu, find profile.ps1 with <appdir>/vendor/ for that case and you can add into that file.

Otherwise you can add to default locations where powershell loads it. One of tutorials HERE.

Solution 6 - Powershell

The keybindings are controlled by PSReadLine. PSReadLine's default edit mode is Windows style, where Ctrl-D is unbound.

Set your edit mode to Emacs

Set-PSReadlineOption -EditMode Emacs

or bound the key

Set-PSReadLineKeyHandler -Key 'Ctrl+d' -Function DeleteCharOrExit

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanojldsView Question on Stackoverflow
Solution 1 - PowershellGlennView Answer on Stackoverflow
Solution 2 - PowershellmanojldsView Answer on Stackoverflow
Solution 3 - PowershellRynantView Answer on Stackoverflow
Solution 4 - PowershellRussellView Answer on Stackoverflow
Solution 5 - Powershelluser3905644View Answer on Stackoverflow
Solution 6 - PowershellSuman SaurabhView Answer on Stackoverflow