7

For some reason I am unable to bind Ctrl-Left Arrow and Ctrl-Right Arrow to move between words in ZSH after installing oh-my-zsh on my MacBook Pro.

In iTerm2 I have my keybindings configured to "Send Escape Sequence" [1;5D and [1;5C, which worked before installing oh-my-zsh.

I also tried uncommenting

bindkey '^[^[[C' emacs-forward-word
bindkey '^[^[[D' emacs-backward-word

in ~/.oh-my-zsh/lib/key-bindings.zsh, also to no avail. In both scenarios, hitting Ctrl-Left/Right just sends "5D" and "5C" to the screen.

TLDR: Has anyone gotten Ctrl-Left and Ctrl-Right to move between words when using oh-my-zsh?

5 Answers 5

8

Change your bindkey lines to:

bindkey '^[[1;5C' emacs-forward-word
bindkey '^[[1;5D' emacs-backward-word
7

Press ^v(Ctrl-v) or cat in the zsh shell, followed by the key-combinations(Ctrl+left) whichever you wanna use, to get the corresponding bindkey value. Use this output in the command. It varies from system to system depending on the terminal driver, etc.

In my case Ctrl-left was ^[O5D. Hence, bindkey '^[O5D' backward-word

If you wanna go one step ahead, https://stackoverflow.com/a/8645267/2538943 provides the idea to generalize zsh key bindings, with the help of the cmd autoload zkbd ; zkbd. Once done, add the following line, source ~/.zkbd/$TERM-${${DISPLAY:t}:-$VENDOR-$OSTYPE} to your .zshrc. From now on, you may write it like [[ -n ${key[Left]} ]] && bindkey "${key[Left]}" backward-char. In this way, you can copy the same .zshrc to different machines too.

Additional info: Peter Stephenson says ...the function keys on your keyboard... It depends a good deal on how your windowing system or terminal driver handles them... Hence it is better to validate all those key binds before running any such script, though it might not vary in most of the cases.

For detailed understanding of key bindings for zsh, check this guide. http://zsh.sourceforge.net/Guide/zshguide04.html.

If you wanna know how a key pressed is displayed, https://unix.stackexchange.com/questions/116629/how-do-keyboard-input-and-text-output-work/116630#116630

4

This one worked for me:

bindkey "[C" emacs-forward-word   #control left
bindkey "[D" backward-word        #control right

In fact, I pressed Control+left and Control+right between the "" and that did the magic.

2

I came across this because I was having similar issues on a macbook using external keyboard.

Turns out ctrl+Left and ctrl+Right are set as Keyboard shortcuts for mission control.

Disabling this on the system fixed my issue

Adding

bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word 

Enabled alt+right and alt+left to do the same thing

1
  • Thanks, this solved it for me. I couldn't figure out why my terminal didn't output anything at all for CTRL+<arrow>, but it was of course swallowed, so iTerm2 never got the key event at all.
    – gnud
    Jul 6, 2022 at 8:08
0

So you have your terminal emulator sending one CSI sequence (ESC+[+1;5C) for the key, but you've told your shell with the bindkey command to watch the terminal input for a different CSI sequence (ESC+ESC+[+C), and you are wondering why this doesn't work?

It doesn't work for the very obvious reason.

You must log in to answer this question.

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