How can I scroll up one line in zsh?

Solution 1:

I think it depends from the terminal emulator.

For example, with zsh, in konsole it works Shift+Up.
In xterm not, but it works Shift+PgUp.

It seems that with iTerm2 it should work by default :

  • to move one line up
  • to move one line down

Solution 2:

The function you are looking for is provided by the up-history and up-line-or-history widgets (down-history and down-line-or-history for scrolling down).

By default up-line-or-history should be bound to Up but you can bind it to (almost) any other key sequence with bindkey. In terminals special keys (keys that do not generate a printable character) generate a sequence of keys that is then interpreted by the shell - or not, as your example of 2A for Shift+Up shows. These key sequences also may differ between terminal emulators. In order to get the correct key sequence for your terminal, open it, then press Ctrl+v followed by the key combination you want (Shift+Up). On my terminal (rxvt-unicode) I get ^[[a. You can then bind the sequence to the up-history widged:

bindkey <in-string> up-history

Just replace <in-string> with the found sequence. In my case

bindkey '^[[a' up-history

Note 1: You most likely need to put the sequence in quotes as it probably will contain characters that will be interpreted by zsh otherwise.

Note 2: Some key combinations may generate identical key sequences. For example, letters combined with Ctrl+Shift usually do not differ from the same letters combined with just Ctrl. Both, Ctrl+Shift+a and Ctrl+a usually produce ^A. In some terminal emulators Ctrl+Shift combinations are even used for shortcuts of the terminal itself and are not available in the shell.