Binding option left and right arrows to move by words in zsh command line
Asked Answered
B

4

21

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.

Babbling answered 23/11, 2013 at 21:15 Comment(0)
T
41

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

Tera answered 30/5, 2014 at 20:40 Comment(3)
Save my issue!! Thanks a lot!!Seditious
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 0x15Pulitzer
The solution below is a lot simplerPlayreader
F
23

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

Flatwise answered 5/2, 2020 at 22:59 Comment(1)
Super simple! I got back all the old hotkeys, thanks!Haystack
N
17

Add the following to .zshrc

# Skip forward/back a word with opt-arrow
bindkey '[C' forward-word
bindkey '[D' backward-word
Nomism answered 13/8, 2015 at 3:30 Comment(2)
Works great and much more automatable when setting up new machines than having to fiddle with a GUI.Furore
Agree with the above comment! This is a much more simple and repeatable solution!Playreader
D
4

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

Dulcia answered 4/8, 2022 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.