0

I have

# auto-complete
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search
bindkey "^[[B" down-line-or-beginning-search

in my .zshrc. This works as expected normally, e.g. writing ssh and pressing the up arrow will cycle through all ssh commands in the history.

However, if I ssh into a server first and the exit back into my local terminal, pressing the up arrow ignores what has been written and simply cycles through the history, i.e. it shows all commands; not commands only starting with ssh. This only happens when I ssh into a server that has an older ZSH version. If I ssh into a server with the same ZSH version, everything works as expected.

The server has an empty .zshrc. My ZSH versions are zsh 5.7.1 (x86_64-debian-linux-gnu) for the server running Debian 10 and zsh 5.9 (x86_64-pc-linux-gnu) for the local machine running Arch. Any idea how to fix this?

2
  • Does the problem go away after running tput rmkx or printf "\e[?1l"? Commented Sep 21, 2022 at 11:02
  • @user1686 Yes, it goes away when I run tput rmkx.
    – user110971
    Commented Sep 21, 2022 at 12:16

1 Answer 1

2

I ran into the exact same issue. The issue is that, depending on whether "application mode" is active (as opposed to "normal mode"), the terminal is sent a different escape sequence for a given arrow key press. For the up arrow, the sequence is ^[[A (CSI A) in normal mode, versus ^[OA (SS3 A) in application mode.

This is a problem because the bindkey lines only catch the CSI sequence, not the SS3 sequence -- the "CSI sequence" being ^[[, and the "SS3 sequence" being ^[O. But knowing this, the fix is pretty simple:

# auto-complete
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search
bindkey "^[OA" up-line-or-beginning-search
bindkey "^[[B" down-line-or-beginning-search
bindkey "^[OB" down-line-or-beginning-search
  • Some further background info here.
  • Good reference on the escape codes etc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .