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
- Press Load Preset...
- Select Natural Text Editing
Then, you can move a word backwards using Option ⌥ + ← and a word forwards using Option ⌥ + →. Also you can delete a word backwards using Option ⌥ + ⌫, delete the whole line using Command ⌘ + ⌫, and more.
If the preset doesn't appear, reinstall iTerm2. If you installed it using Homebrew+Cask:
brew cask reinstall iterm2
I like the following setup.
- Preferences > 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
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.
Similar to other answers, but for Zsh it took me a while to find this:
If you are using Zsh, like Oh My Zsh, in iTerm then go to: Preferences > Profiles > Keys sub-menu
Click + sign
Add your shortcut combo, choose "Send Escape Sequence"
inputs for left and right below.
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.
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