ALT+arrow moving between words in zsh and iTerm2

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?


Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.