23

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 '17 at 20:08
30

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
1
  • 1
    It's worth mentioning that it's specific to one particular terminal emulator — not to zsh in general. – Hi-Angel Nov 18 '16 at 14:43
7

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 '17 at 8:57
  • 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 '18 at 5:47
6

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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.