MacOS – How to get control+left arrow to go back one word in iTerm2

itermiterm2keyboard shortcutsmacmacos

I was reading up in the Linux manual and I noticed that it said I could use control+left and control+right to move forward and back words in the terminal while editing.

"Pressing Ctrl plus the Left or Right arrow key moves backward or forward a word at a time, as does pressing Esc and then B or F."

On OS X control+left and control+right normally control spaces. I have disabled those. I also tried to use the preferences pane to set the keyboard shortcuts:

enter image description here

enter image description here

However this does not work and causes this error:

enter image description here

Also, if I am in iTerm and use alt+escape then B or F the character moves back and forth. This was happening before any of my config changes. But I'd really like to be able to use control + the arrow keys.

Best Answer

bash

Just add the following to ~/.inputrc:

"\e[1;5D": backward-word
"\e[1;5C": forward-word

See this archived Wiki post for some more explanation. If you want to use the alt key instead for word-to-word movement (like default OS X behavior), use:

"\e[1;9D": backward-word
"\e[1;9C": forward-word

zsh

zsh by default does not use the readline library and therefore won't read ~/.inputrc. To get the same functionality, you could add the following to your ~/.zshrc – this again would use the alt key:

bindkey -e
bindkey '^[[1;9C' forward-word
bindkey '^[[1;9D' backward-word

See this documentation for more about the built-in zsh line editor (zle).


Why is this? You've set up your profile to use the Xterm defaults:

enter image description here

This is why you'll need to "catch" this sequence and tell readline what to do.


If the above still doesn't work and you are using OS X 10.9 (Mavericks) or there abouts, you probably need to disable the global Mission Control shortcuts which prevent Control+arrow keys from reaching iTerm, even if Mission Control itself is disabled. You can do so from System Preferences → Keyboard → Shortcuts → Mission Control:

mission control keyboard prefs

Related Question