172

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?

  • 15
    This doesn't really answer the question but it's relevant: you can option+click anywhere in a command line to move the cursor there. – David Winiecki Apr 19 '18 at 22:52
  • Downvoting for not accepting the correct answer – P-i- Aug 21 at 10:37
  • @P-i- Why do you think that accepted answer is not the correct one? 1. It appeared almost 3 years before the one with most votes. 2. It presents a solution that doesn't works by default. – syntagma Aug 21 at 18:23
49

Ctrl-[ b jumps back a word. You can also use Esc instead or Ctrl-[, and f to go forward.

That is Ctrl+[ release and then b orf. Or Esc and b or f.

More information can be found at this other discussion on AskDifferent.

  • 3
    This would be better if there was a way to go back lots of words quickly while holding down a button, as opposed to having to type a sequence of keys for each word. – user1717828 Feb 28 at 19:43
  • What is "Ctrl-[" ? – Shihao Xu Aug 20 at 22:57
  • With "Ctrl-[ " I mean hitting the "Ctrl" key and "[" key simultaneously. – Volsk Aug 23 at 8:33
345

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 ⌥ + , move to the start of the line using fn + and to the end of the line with fn + . Also you can delete a word backwards using Option ⌥ + , delete the whole line using Command ⌘ + .

If the preset doesn't appear, reinstall iTerm2. If you installed it using Homebrew+Cask:

brew cask reinstall iterm2
  • 52
    This should be the accepted answer! – guyarad Sep 7 '17 at 2:31
  • 3
    This doesn't work for me. Parameters: iTerm2 3.1.2, macOS 10.12.6, german Keyboard. And I'm not alone... – t0r0X Oct 29 '17 at 18:16
  • 2
    This is great and will add most of your needs. It however does not add a 'go to start/end of line via CMD + left arrow/right arrow`. For that action, see: stackoverflow.com/questions/6205157/… – MikeyN0 Oct 3 '18 at 23:07
  • 42
    My life has changed after I saw this answer. I divorced my wife, now I am in a Lambo with a supermodel. – Dragos Rizescu Oct 16 '18 at 20:51
  • 13
    Note that this is Preferences > Profiles > Keys > Load Presets not Preferences > Keys > Load Preferences – kevin Apr 26 at 2:44
149

I like the following setup.

  1. Preferences > Keys (or Preferences > Profiles > Keys)
  2. 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.)

  • 2
    On MacOS 10.12, I need to use hex code 0x17 to get delete to beginning of word – midopa Jan 20 '17 at 4:16
  • works like a charm with macOS 10.12 and zsh – Sébastien Feb 7 '17 at 14:23
  • 1
    best answer ever you have a beer from me :) – infinity Jun 9 '17 at 13:46
  • 1
    Consider upvoting jherran's answer. I think I just improved on their answer a little and probably should have suggested an edit instead of creating my own answer. I kind of feel like I stole it. – David Winiecki Jun 17 '17 at 2:54
  • Referenced this: to do Esc-b, I had to send 0x1b 0x42, and for Esc-f, I had to send 0x1b 0x46 :D – Meredith Oct 13 '17 at 17:59
36

To Get Forward (Alt-f), Backward (Alt-b) and Delete (Alt-d) Word

  1. Open iTerm.
  2. Go to iTerm > Preferences... > Profiles > Keys
  3. Under Profile Shortcut Keys, click the + sign.
  4. Type your key shortcut (option-b, option-f, option-d, option-left, etc.)
  5. For Action, choose Send Escape Sequence.
  6. Write b, d or f in the input field.

This works at least for bash. For zsh there are other ways to navigate.

  • 2
    I tried to add an option+right shortcut and got a warning message about a conflict, because I didn't realize that you can configure keys both in Preferences > Keys > Global Shortcut Keys and also Preferences > Profiles > Keys > Profile Shortcut Keys. Configuring it in the default profile worked. – David Winiecki Sep 8 '15 at 3:48
  • 1
    It works for zsh as well! – Andrii Abramov Jun 30 '17 at 16:33
  • 2
    I think your answer (to the same solution) was the easiest to understand +1 – nzaleski Oct 29 '18 at 12:39
  • 1
    Very useful explanation. – Conti Jan 28 at 10:37
  • Perfect. You can also use Command+F etc so the "Alt" key is in the same position and no adjustment is necessary when switching between PC and Mac. – friederbluemle Sep 8 at 17:59
12

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.

left:

[1;5D

right:

[1;5C
  • 1
    You saved my life mate – thibaut noah May 31 '17 at 8:40
  • This is the only solution that worked for me – Bryji Oct 2 '17 at 10:44
  • Finally, a simple solution that works :D Thanks for sharing it with us! – Periback Aug 1 '18 at 7:08
  • This should be the answer. Upvoted. – ehime Sep 27 at 17:16
2

enter image description here

Open Preferences

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!)

enter image description here

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
  • 1
    Perfect... tks. – paivaric Oct 18 at 23:51
  • No problem! Glad it helps :) – yosefrow Oct 22 at 10:18
2

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 Esc, then 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.

Hit i to go back to Normal mode and insert.

Those familiar with vi can do much more. A cheat sheet can be found here.

  • 2
    vim solves everything! :D – Breno Salgado Jun 20 '18 at 20:19
  • Hitting Esc then I to just go once to the beginning of the line is slightly less convenient than Ctrl-a - this use case happens often when you just want to modify the command name. So I use vi mode with some keybinding changes in iTerm2 to map Ctrl-A and similar to escape sequence Esc+0. That way these cases are even faster as it ends in edit mode. – rfabbri Aug 6 at 17:59
-1

With iTerm2 3.1.4, I was able to setup the following without adding individual key mappings.

  1. Go to Preferences > Profiles > Keys
  2. 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

You must log in to answer this question.

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