19

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.

savanto
  • 4,470
  • 23
  • 40
justingordon
  • 12,553
  • 12
  • 72
  • 116

4 Answers4

38

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

savanto
  • 4,470
  • 23
  • 40
  • Save my issue!! Thanks a lot!! – Brady Huang Jan 05 '18 at 03:17
  • Many thanks!! Was looking for solutions when I just started exploring iTerm2 + zsh lately. And: 1. Delete one word with: ⌥ Delete: Send hex code `0x17` 2. Delete from cursor to beginning of line with: ⌘ Delete: Send hox code `0x15` – Shan Dou Nov 04 '18 at 00:59
  • The [solution below](https://stackoverflow.com/a/31979039/260574) is a lot simpler – n00b Jun 15 '22 at 00:30
18

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

doingweb
  • 3,608
  • 2
  • 18
  • 12
17

Add the following to .zshrc

# Skip forward/back a word with opt-arrow
bindkey '[C' forward-word
bindkey '[D' backward-word
justinwehrman
  • 330
  • 2
  • 4
  • 1
    Works great and much more automatable when setting up new machines than having to fiddle with a GUI. – Janosh Dec 02 '19 at 11:28
  • Agree with the above comment! This is a much more simple and repeatable solution! – n00b Jun 15 '22 at 00:30
1

Simply add these to your .zshrc


bindkey "\e[1;3D" backward-word     # ⌥←
bindkey "\e[1;3C" forward-word      # ⌥→
bindkey "^[[1;9D" beginning-of-line # cmd+←
bindkey "^[[1;9C" end-of-line       # cmd+→

It works for me using kitty

E_net4
  • 27,810
  • 13
  • 101
  • 139
Elad Amsalem
  • 1,490
  • 1
  • 12
  • 14