Reputation: 115
If one runs the following commands in succession in Zsh (on macOS or Ubuntu):
date +%s
echo \
a\
b\
c
One can then scroll back to the first command by pressing the up-arrow 5 times.
Is there a way to scroll back to the first command with just 2 key presses (i.e. the first key press selects the echo
command and the 2nd key press selects the date
command)?
When the history contains several long multi-line commands, scrolling back through them with the up-arrow key is a pain.
I was hoping to find some Zsh key shortcuts that would scroll up and down through the command history without stopping en-route to edit multi-line commands, but I haven't yet managed to find any.
Upvotes: 4
Views: 2696
Reputation: 7020
Add the following to your .zshrc
file:
bindkey '^P' up-history
bindkey '^N' down-history
Now you can press ControlP to go up in history and ControlN to go down in history, regardless of how many lines the current editing buffer has or which line you are on.
For more info, see https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#History-Control
Alternatively, my zsh-autocomplete
plugin contains a history menu that lets you step up and down through history items, with just one keypress per item, including multi-line items.
Upvotes: 5