Would it be possible to configure iTerm 2 to go backwards and forwards one word through the curent text in the command line using a keyboard shortcut?
Ctrl-[ b jumps back a word. You can also use
Esc instead or
f to go forward.
[ release and then
More information can be found at this other discussion on AskDifferent.
Killing a fly with a cannon:
- Go to Preferences... > Profiles > Keys (not Preferences... > Keys)
- On current versions (3.14+) you then switch to the Key Mappings tab
- Press Presets... dropdown button.
- Select Natural Text Editing
Then, you can
- move a word backwards Option ⌥ + ←
- move a word forwards Option ⌥ + →
- move to the start of the line fn + ←
- move to the end of the line fn + →
- delete a word backwards Option ⌥ + ⌫
- delete the whole line Command ⌘ + ⌫
If the preset doesn't appear, reinstall iTerm2. If you installed it using Homebrew+Cask:
brew cask reinstall iterm2
or since Homebrew 2.6.0 (December 2020)
brew reinstall --cask iterm2
I like the following setup.
- Preferences > Keys (or Preferences > Profiles > Keys)
- Click the plus.
move forward one word
option+right send escape sequence f
move back one word
option+left send escape sequence b
delete to beginning of word (credit)
option+delete send hex code 0x1B 0x08
delete to end of word
fn+option+delete send escape sequence d
(I don't remember for sure, but I think I copied this answer from jherran's answer below and added more to it. I should have added the extra information in comments or suggested edits on that answer. I don't know how to improve the situation, but now it's known.)
To Get Forward (Alt-f), Backward (Alt-b) and Delete (Alt-d) Word
- Open iTerm.
- Go to iTerm > Preferences... > Profiles > Keys
- Under Profile Shortcut Keys, click the + sign.
- Type your key shortcut (option-b, option-f, option-d, option-left, etc.)
- For Action, choose Send Escape Sequence.
- Write b, d or f in the input field.
This works at least for bash. For zsh there are other ways to navigate.
Configure Left (and / or) Right Option key to send Esc+
If you messed with your presets, you may need to load the default preset (beware this could wipe your custom keybinds!)
If you see weird characters after you do this you may need to configure your ~/.inputrc or /etc/inputrc
Add this to your ~/.inputrc:
"\e[1;5C": forward-word "\e[1;5D": backward-word "\e[5C": forward-word "\e[5D": backward-word "\e\e[C": forward-word "\e\e[D": backward-word
full example inputrc:
# /etc/inputrc - global inputrc for libreadline # See readline(3readline) and `info rluserman' for more information. # Be 8 bit clean. set input-meta on set output-meta on # To allow the use of 8bit-characters like the german umlauts, uncomment # the line below. However this makes the meta key not work as a meta key, # which is annoying to those which don't need to type in 8-bit characters. # set convert-meta off # try to enable the application keypad when it is called. Some systems # need this to enable the arrow keys. # set enable-keypad on # see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys # do not bell on tab-completion # set bell-style none # set bell-style visible # some defaults / modifications for the emacs mode $if mode=emacs # allow the use of the Home/End keys "\e[1~": beginning-of-line "\e[4~": end-of-line # allow the use of the Delete/Insert keys "\e[3~": delete-char "\e[2~": quoted-insert # mappings for "page up" and "page down" to step to the beginning/end # of the history # "\e[5~": beginning-of-history # "\e[6~": end-of-history # alternate mappings for "page up" and "page down" to search the history # "\e[5~": history-search-backward # "\e[6~": history-search-forward # mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving "\e[1;5C": forward-word "\e[1;5D": backward-word "\e[5C": forward-word "\e[5D": backward-word "\e\e[C": forward-word "\e\e[D": backward-word $if term=rxvt "\e[7~": beginning-of-line "\e[8~": end-of-line "\eOc": forward-word "\eOd": backward-word $endif # for non RH/Debian xterm, can't hurt for RH/Debian xterm # "\eOH": beginning-of-line # "\eOF": end-of-line # for freebsd console # "\e[H": beginning-of-line # "\e[F": end-of-line $endif
You can set your terminal in vi mode with
set -o vi to be able to use the usual vi motion commands (add the line in
.bash_profile to store the setting permanently.)
So, as if in vi, you can hit
b to move one word backward (
w for forward), go to the beginning of the line with
0, or search a character backward with
F + the char.
i to go back to Normal mode and insert.
Those familiar with vi can do much more. A cheat sheet can be found here.
For new version of iterm 2 (3.4.4), I had to use the suggestion from https://coderwall.com/p/a8uxma/zsh-iterm2-osx-shortcuts. This work specifically for zsh
In zsh you can use ctrl + a/e to move to beginning/end of line and esc + W/B to move one word backward/forward, but that's not very handy.
Here is a solution to map ⌥ + ← / → and ⌘ + ← / → to work in iTerm2 as expected
Put this in your .zshrc
bindkey "[D" backward-word bindkey "[C" forward-word bindkey "^[a" beginning-of-line bindkey "^[e" end-of-line
Answer with more intuitive word breaks. If I have:
ls foo/bar foo/bar/baz
I want a word to be
foo. It took a while a while to get that working.
From this I added to
# Use Ctrl-g instead of Ctrl-f because Ctrl-f is mapped in macs to Find bind '"\C-g":vi-fWord' bind '"\C-b":vi-bWord'
From what I could tell, it's not possible to mention Option key in
.bash_profile. So I had iTerm2 map from C-g -> Option-right, C-b -> Option-left:
With iTerm2 3.1.4, I was able to setup the following without adding individual key mappings.
- Go to Preferences > Profiles > Keys
- Left/Right ⌥ Key: Select Esc+
With a new Terminal session you are now able to use:
Option ⌥ + f to Get Forward
Option ⌥ + b to Get Forward
Option ⌥ + Delete ⌫ to Delete Word