40

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?

1
  • 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
    Nov 6, 2017 at 20:08

6 Answers 6

61

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
3
  • 3
    It's worth mentioning that it's specific to one particular terminal emulator — not to zsh in general.
    – Hi-Angel
    Nov 18, 2016 at 14:43
  • Very descriptive and useful! Worked like a charm
    – Kraken
    Oct 14, 2021 at 12:28
  • Also worth mentioning that 2 is an isolated thing from 3&4. That is you can skip step 2 if you just want to use arrow navigation, or just 2 if you want ⌥f and ⌥b navigation (and ⌥<delete>)
    – Kaos
    Sep 13, 2022 at 13:41
24

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.

2
  • YES! This did exactly what I needed. Thanks Feb 21, 2023 at 1:24
  • This is the setup I was used to from my old iTerm (which I believe was the default as of... 2015?). The perfect fix for setting up iTerm2 on a new laptop in 2024.
    – kotoole
    Jan 23 at 18:07
8

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.

2
  • 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
    Sep 25, 2017 at 8:57
  • 1
    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
    Sep 25, 2018 at 5:47
6

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.

4

To supplement loganjones16's answer, here is a screenshot of the Natural Text Editing menu item:

Natural Text Editing menu item

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .