After adding the following lines to my .zshrc:

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

I ran exec zsh (from zsh), and the lines above did not take effect. I then did the following (not sure this context is necessary, but maybe relevant): exit to drop me back into my login shell (bash), exit to log out, then ssh machine to log back in, then exec zsh, and then the lines above were working.

Does anyone know why?


My title question was incorrect. It turns out that .zsh is behaving inconsistently from login to login with regard to the lines above only. Sometimes they work as expected, sometimes they do not. I've attempted to find a pattern, but cannot. I think (but am not sure) login is key as once I've sshed into the machine, the behavior seems to remain the same regardless of different zsh instances.

The output from read (up) (down) is always the same as is the output from history 0 and the up and down arrows otherwise seem to behave themselves.

May or may not be useful info--for the failing case (I haven't been able to get it to work in a while now):

%bindkey | grep 'or-beginning'
"^[[A" up-line-or-beginning-search
"^[[B" down-line-or-beginning-search
%zmodload | grep zle
up-line-or-beginning-search:zle:19: widgets can only be called when ZLE is active
up-line-or-beginning-search:zle:21: widgets can only be called when ZLE is active
^[[A^[[B^C% (up,down,ctrl-c)
  • That's not relevant to the question, but why didn't you just source .zshrc instead?
    – dessert
    Nov 9, 2017 at 22:51
  • @dessert - I'd read that source isn't quite the same as a fresh shell in some cases and was trying to avoid the ambiguity (unsuccessfully, as it turns out)
    – zzxyz
    Nov 9, 2017 at 22:59
  • @dessert I would caution that “fixing” the question is often better done in answers or comments; let the asker use terminology that makes sense to them. Corrections in answers and comments will then be seen by future readers who come here by way of similar wording.
    – Jeff Schaller
    Nov 9, 2017 at 23:20
  • @zzxyz It's not really an answer but to add onto what dessert said: you could call reset before source .zshrc.
    – cprn
    Nov 9, 2017 at 23:24
  • 1
    @JeffSchaller OK, but I just suggested this edit, OP himself approved it.
    – dessert
    Nov 9, 2017 at 23:26

2 Answers 2

% echo echo echo >> ~/.zshrc
% exec zsh

So an exec zsh does indeed run the echo just added to ~/.zshrc. Perhaps run

zsh -ixc exit >grepthis 2>&1

and then inspect the grepthis file for whether your lines were read or not, or if other bindkey related things happened afterwards, or ...

  • What does -x? It's not in man zsh.
    – cprn
    Nov 9, 2017 at 23:30
  • 1
    @cprn see zoptions(1) or search all of them via zshall(1)
    – thrig
    Nov 9, 2017 at 23:32
  • Thanks, got it. Also, for anyone with the same issue: redirecting command 2>&1 >logfile doesn't work on my zsh but the following shortcut is equivalent and works: command &>logfile
    – cprn
    Nov 9, 2017 at 23:38
  • Yeah, my terminal got clobbered with output. I spent a few minutes going over it, but didn't see anything nefarious or different between the failure and success cases. Both output the relevant lines from my .rc (toward the end)
    – zzxyz
    Nov 9, 2017 at 23:47
  • 1
    @cprn oh oops > needs to happen leftwards of 2>&1
    – thrig
    Nov 9, 2017 at 23:54

I still don't know why it sometimes worked, but the following helped me: http://zshwiki.org/home/zle/bindkeys

Hitting '<ctrl-v><up>' (rather than "^[[A") as the key sequence fixed things. Fortunately it also worked in vim.

The <ctrl-v><up> sequence is printable, but not directly typable: 0x1B 0x4F 0x42

Also, (as the article also says), there are friendly bindings for some of the simpler keys that in my system are defined in /etc/zsh/zshrc.

So on my system, the following also works:

bindkey "$key[Up]" up-line-or-beginning-search

You must log in to answer this question.

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