I recently switched to oh-my-zsh and I can't get used to the way the arrow up key works. When I enter ssh for example and press up, it only shows me commands starting with ssh while I'd prefer it to show me previous command instead. How could I make it behave the way Bash does, which is what I am already accustomed to?

Note that adding the lines from the answer to the end of my zshrc didn't help and it didn't happen after the upgrade, just after switching to oh-my-zsh from bash.

  • 3
    Possible duplicate of Broken history search after upgrade of oh-my-zsh – jasonwryan Nov 20 '16 at 1:51
  • @jasonwryan it doesn't look like a duplicate of that. Adding the lines from the answer to the end of my zshrc didn't help and it didn't happen after the upgrade, just after switching to oh-my-zsh from bash. – d33tah Nov 20 '16 at 1:53
  • Try removing the directory ~/.oh-my-zsh/plugins/history-substring-search. – apricot boy Nov 20 '16 at 4:52
  • 1
    This is not a duplicate. Ironically, the other question is the opposite of this one. In this one, the arrow key recognition and filtered history are working and the questioner wants to know how to turn them off; in that question, the problem is that they are not working and the questioner wants to know how to repair them. – JdeBP Nov 20 '16 at 6:53

Ironically, the answer in the question that people are proposing as a duplicate …

bindkey '\e[A' history-beginning-search-backward
bindkey '\e[B' history-beginning-search-forward
… is exactly the wrong answer. It ensures that your terminal's control sequences for the arrow editing keys are mapped by ZLE to the extension widgets history-beginning-search-backward and history-beginning-search-forward that are provided by Oh My ZSH's history-substring-search plug-in.

That is exactly what you do not want.

You're trying to turn this feature off, because you prefer another behaviour; not trying to repair it because it is broken.

What you want is those keys to map to the widgets as in vanilla Z Shell, Oh My ZSH being a barrelful of extensions to and customizations of the Z Shell that some (but not all) people like. Vanilla Z Shell's widgets have roughly the old Bourne Again shell behaviour that you prefer. (Unlike the Bourne Again shell, though, you if have a multiple-line editing buffer they will move up and down within it before, at the top and bottom edges of the buffer, moving up and down the history.)

Those widgets would be:

bindkey "$terminfo[kcuu1]" up-line-or-history
bindkey "$terminfo[kcud1]" down-line-or-history

Note how one works to not hardwire one specific terminal type into one's scripts. One could use "$termcap[ku]" and "$termcap[kd]", alternatively. The Z Shell, presuming that you have the appropriate modules loaded, maintains a map for both. Generally the world prefers terminfo if it has it nowadays, though. To be strictly bulletproof, in something that you use with heterogeneous systems or give to other people, you'll need a test -n in there — just as you'll find the Oh My ZSH extensions doing in fact.

test -n "$terminfo[kcuu1]" && bindkey "$terminfo[kcuu1]" up-line-or-history
test -n "$terminfo[kcud1]" && bindkey "$terminfo[kcud1]" down-line-or-history

(In extremis, to cope with some unlikely possibilities, you'll need some "${terminfo[x]-${termcap[y]}}" variable expansion. The Oh My ZSH extensions do not bother with that latter, and in practice you can likewise largely get away without it. In practice, almost everyone has the termcap and terminfo modules loaded, and all that you have to worry about is whether the record for your current terminal type actually defines the relevant control sequences, which is what the test -n is for.)

A more exact approximation of the Bourne Again shell behaviour would be

bindkey "$terminfo[kcuu1]" up-history
bindkey "$terminfo[kcud1]" down-history
That's not the vanilla Z Shell default, and after editing your first multiple-line command line you might decide that the default is what you prefer. ☺

  • 2
    Awesome, that fixed it for me! Just one note - could you make it somehow obvious that the first two lines are wrong to the ones that are sleepy / only skim the answers? Like adding headers "wrong answer" and "right answer" so that whoever gets there next can juts copy-paste the right answer ;) – d33tah Nov 20 '16 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.