Shell – .zshrc Shortcut Working on Arch but Not Ubuntu

keyboard shortcutsshellzsh

I use the zsh shell as default shell on both Ubuntu and Arch.

I configured a shortcut (the up arrow) to autocomplete from history in my zsh shell, using the following line in my .zshrc:

bindkey "^[[A" history-beginning-search-backward

However, when I source my .zshrc and/or reboot in Ubuntu, the shortcut does not work (I only get the previous command, no matter what I started typing), whereas on Arch it works fine (I only get the last command starting with what I typed).

Does anyone know how to solve this?

Best Answer

On most xterm-like terminals, the Up (and it's similar for most navigation keys) send either ␛[A or ␛OA depending on whether the terminal has been put in keypad transmit mode or not. The smkx and rmkx terminfo entries can be used to put a terminal in or out of that mode.

The kcuu1 (key cursor up by 1) terminfo entry describes the sequence sent by Up when in keypad transmit mode, that is ␛OA.

Debian and derivatives have a /etc/zsh/zshrc file that does a

    function zle-line-init () {
        emulate -L zsh
        printf > /dev/tty '%s' ${terminfo[smkx]}
    }

Which puts the terminal in that mode when zle is active, which means you can now rely on the terminfo database to know what character sequences keys transmit.

The file also defines a $key associative array based on the terminfo entries to help you map those to widgets. So on those systems, you can do:

(($+key[Up])) && bindkey $key[Up] history-beginning-search-backward

For something that works on systems where the terminal is in keypad transmit mode and those that don't or don't have the $key hash, you can do:

bindkey $terminfo[kcuu1] history-beginning-search-backward
bindkey ${terminfo[kcuu1]/O/[} history-beginning-search-backward

See also:

Related Question