PowerShell bind arrow keys to command history search
Asked Answered
S

1

9

In bash, I can bind the Up and Down arrow keys to history search with

"\e[5~": history-search-backward
"\e[6~": history-search-forward

in ~/.inputrc. If at the prompt I type ca and then the Up key, it will bring the next command line in history that matches ca as the beginning.

Using Ctrl + R for (reverse-i-search) is useful, and PS has this. But I find the bindings above quite more efficient to work.

Can this be achieved with PowerShell?

Serrell answered 13/7, 2020 at 20:27 Comment(0)
A
13

Add the following lines to your Powershell profile.

Mine is at %HOME%\Documents\WindowsPowerShell\profile.ps1.

# Reverse Search
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

These are sourced from the powershell PSReadLine repo.

Funnily enough, we came to ask the same question 12 hours apart :)

EDIT months later as I still come back to this:

This provides bash-like autocomplete

Set-PSReadlineKeyHandler -Key Tab -Function Complete
Acerbity answered 14/7, 2020 at 8:45 Comment(1)
Worked like a charm. Thanks!Serrell

© 2022 - 2024 — McMap. All rights reserved.