ohmyzsh: bindkey is not work in "~/.zshrc"

d### Describe the bug

I add this three line at the end of ~/.zshrc

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

but when I restart the shell, the keys up and down did not perform as expected. But when I run source ~/.zshrc, the keys up and down is work, which can find the history. I tried adding some printable commands to ~/.zshrc, like echo "this is work", this is work when I restart the shell.

I’m incredibly confused, any help is greatly appreciated

Steps to reproduce

  1. add in the end of ~/.zshrc

    bindkey "^[[B" history-beginning-search-forward
    bindkey "^[[A" history-beginning-search-backward
    
  2. restart shell

Expected behavior

when I restart shell the Shortcuts should work。

Screenshots and recordings

No response

OS / Linux distribution

Macos 12.2 (21D5039d)

Zsh version

5.8

Terminal emulator

Apple Terminal, VScode Terminal

If using WSL on Windows, which version of WSL

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 24 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Maybe not for everyone, but for me issue was with this vi mode plugin It overwrites the keybindings so you have to run it before defining any keybindings

I may have the same problem. fzf will register the hotkeys ctrl-R and ctlr-T. ctrl-T is work, but ctrl-R only works after I source ~/.zshrc explicitly.

You need to make sure that ^[[B and ^[[A are the keyboard sequences for Down and Up arrow keys. You can test that by pressing <kbd>CTRL-V</kbd> and then pressing Down or Up arrow, which will display the keyboard sequence in the command line.

For me, Up arrow is ^[OA and Down arrow is ^[OB. That’s going to depend on your terminal (it might be different in Terminal.app and VS Code terminal).

The vi-mode was indeed causing it for me. I temporary removed bindkey -v and all the other keybindings were working again.

So as written above you can use vi-mode but it needs to be run at the beginning of the .zshrc

BTW for the specific case of vi-mode, you can define your key bindings for all 3 different keymaps as follows:

bindkey -M emacs '^[[A' history-substring-search-up
bindkey -M viins '^[[A' history-substring-search-up
bindkey -M vicmd '^[[A' history-substring-search-up

bindkey -M emacs '^[[B' history-substring-search-down
bindkey -M viins '^[[B' history-substring-search-down
bindkey -M vicmd '^[[B' history-substring-search-down