'Binding option left and right arrows to move by words in zsh command line

I'm using iTerm2 on Mac OSX 10.8 with an xterm key binding and zsh.

I'd like zsh to use option left arrow and option right arrow to do the standard Mac bindings of left and right word.

If I hit ctrl-v then option-left and right arrows, where are the two key sequences that print:

^[[1;9D 

^[[1;9C

I tried using binding these sequences using bindkey -v, but with no luck.



Solution 1:[1]

You can configure iTerm2 to do this like so:

  • Go to iTerm2 > Preferences > Profiles > Keys
  • If there is already an ? ? or ? ? setting, delete it by selecting it and hitting -.
  • Add a new shortcut by hitting the + button.
  • Type ?+? in the Keyboard shortcut box.
  • Select Send Escape Sequence in the Action box.
  • Enter b for Characters to send.
  • Click Ok.

Repeat the above procedure for ? ?, this time entering f for the Characters to send.

Taken from this great tutorial which describes the whole process in detail and with pictures:
Use ? ? and ? ? to jump forwards / backwards words in iTerm 2, on OS X | Coderwall

Solution 2:[2]

Add the following to .zshrc

# Skip forward/back a word with opt-arrow
bindkey '[C' forward-word
bindkey '[D' backward-word

Solution 3:[3]

If you're looking to easily add this and a bundle of similar mappings, there's a "Natural Text Editing" preset under Preferences > Profiles > Keys (version 3):

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 justinwehrman
Solution 3 doingweb