David
David

Reputation: 115

How can one scroll through multi-line commands in the Zsh history without selecting each line of each command?

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

Answers (1)

Marlon Richert
Marlon Richert

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.

enter image description here

Upvotes: 5

Related Questions