ALT+arrow moving between words in zsh and iTerm2

19

10

I logged in on one of hosting provider servers and noticed ALT + left and ALT + right moved between words in a shell prompt in GNU Screen.

What kind of key bindings I need to configure and where to get this behavior to my local OS X zsh running in iTerm2?

Mikko Ohtamaa

Posted 2012-05-21T20:16:20.237

Reputation: 1 790

People interested in this question may also be interested to know that zsh words are not bash words. FOO=BAR is one word to zsh and 2 words to bash. Similarly, if you set your cursor to the end of foo --bar and do alt+backspace, in bash you will have foo -- and in zsh you will have foo. Zsh adds a lot of features to bash, but it also has lots of insane defaults to override. – weberc2 – 2017-11-06T20:08:11.143

Answers

28

I found the solution here: https://coderwall.com/p/h6yfda. Will copy the most important parts of it, in case the link goes down.

  1. Go to Preferences, Profile, Keys.
  2. Set your left ⌥ key to act as an escape character.
  3. Locate the current shortcut for ⌥ ← or create a new one, with the following settings:
    • Keyboard Shortcut: ⌥←
    • Action: Send Escape Sequence
    • Esc+: b
  4. repeat for the ⌥→ keyboard shortcut with the following settings:
    • Keyboard Shortcut: ⌥→
    • Action: Send Escape Sequence
    • Esc+: f

Willington Vega

Posted 2012-05-21T20:16:20.237

Reputation: 381

1It's worth mentioning that it's specific to one particular terminal emulator — not to zsh in general. – Hi-Angel – 2016-11-18T14:43:53.623

6

You are looking for the keywords backward-word and forward-word. So if you are on a shell where the keybindings aren't working try bindkey -L | grep backward-word in order to check if they are even configured. There's more information about this in zshzle(1).

You can manually set the keybinding by typing something like this:

bindkey 'Ctrl+v Alt+Right' forward-word

bindkey 'Ctrl+v Alt+Left' backward-word

I've had some troubles with keybindings too and the problem was almost always that the Option/Alt key sent something different than the expected Meta/Escape.

Sebastian Stumpf

Posted 2012-05-21T20:16:20.237

Reputation: 604

You can also use emacs-forward-word and emacs-backward-word. The difference is that you jump forward to the end of the word and backward to the begining of the word instead of jumping always at the begining of the word. – Aalex Gabi – 2017-09-25T08:57:58.523

This one is the most robust and clean solution. It can adapt to different kind of key mapping and/or all kind of ssh-tmux-zsh combination. Other solutions that involve hard-coded escape sequence only solve some of the cases in some particular system. – HKTonyLee – 2018-09-25T05:47:13.350

3

I can't speak for iTerm but these are the keybindings I used to solve this problem under GNOME Terminal, on Fedora 19, running ZSH 5.0.7 with Oh-my-zsh:

bindkey "\e[1;3C" forward-word
bindkey "\e[1;3D" backward-word

where \e == The escape-key-sequence(as documented under section 4.1.1)

and [ == O (uppercase O; as documented under section 4.2.1), in some cases. For e.g. under tmux this substitution is necessary for me, however without tmux it is required that no substitution be made and [ == [

The key codes for a sequence can be obtained using cat and pressing the desired sequence. For example the results of pressing <Alt+Right> should be interpreted like so:

$ cat
^[[1;3C

^[ == \e == The escape-key-sequence

[ == [ without tmux OR [ == O (uppercase o) with tmux

1;3 == I'm not sure about this one, but it should logically mean <Alt>

C == The right arrow key

Then this sequence is given to bindkey in the ~/.zshrc file for persistance, as the first argument, and is bound, meaning that the keystroke in argument one will execute a particular editor command (or widget in zsh terms), to the widget, which in the first line of the above example is forward-word.

The ~/.zshrc should be re-sourced after these two commands are appended to it with:

$ source ~/.zshrc

Now one annoyance on my system is that this particular combination caused the terminal emulator to issue a beep each time the command was issued, this I remedied by disabling the

'Edit'->'Profile Preferences'->'Terminal Bell' checkbox.

rivanov

Posted 2012-05-21T20:16:20.237

Reputation: 165

1

What worked best for me in regards to making iTerm2's command line navigation more intuitive for me (I am a young adult who didn't grow up on a command line, but I've spent a lot of time in text editors and IDEs) was to:

  • Go to Preferences -> Profile -> Keys
  • Under the list of Key Mappings there is a box to add/remove or load Presets (combo box)
  • Select the Natural Text Editing option in the Presets drop down.

This defaults the editor's keys to a more standard arrangement without me having to modify every option individually.

loganjones16

Posted 2012-05-21T20:16:20.237

Reputation: 111

0

I suggest you use zkbd to configure keybindings. I use it and Alt+Left / Alt+Right works fine on iTerm2. See zshcontrib(1) for details on zkbd.

Daniel Serodio

Posted 2012-05-21T20:16:20.237

Reputation: 764