528

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?

syntagma
  • 10,888
  • 62
    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
  • @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 '19 at 18:23
  • 3
    @P-i- You should only downvote a question if the question itself has problems, not because you disagree with OP's actions – TerryA Mar 21 '20 at 00:17

12 Answers12

1318

Killing a fly with a cannon:

  1. Go to iTerm2 (in the menu bar) > Settings... > Profiles > Keys (not "Preferences... > Keys"!),
  2. On current versions (since version 3.4.9): switch to the Key Mappings tab,
  3. Press Presets... dropdown button,
  4. Select Natural Text Editing.

screenshot of dialog with Presets

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
t0r0X
  • 200
  • 8
    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: https://stackoverflow.com/questions/6205157/iterm-2-how-to-set-keyboard-shortcuts-to-jump-to-beginning-end-of-line – MikeyN0 Oct 03 '18 at 23:07
  • Only issue is that say you have to ssh into a remote box, you need to know the standard terminal shortcuts. I just discovered this was possible now and was like "whoa why have I been twisting my fingers into ctrl+w to backspace a word, but on second thought, maybe its good to keep the muscle memory of the standard ones. – vaughan Oct 31 '19 at 20:12
  • this is a better answer but i got a side effect: now CMD+arrow-key moves the cursor to the start/end of line (similar to Option + arrow-key) instead of navigating between iterm tabs. – Lafi Apr 08 '20 at 15:28
  • 8
    Go to iTerm Preferences → Profiles, select your profile, then the Keys tab. Click Load Preset... and choose Natural Text Editing. – Amarjit Dhillon May 07 '21 at 08:39
  • 3
    THANKS, this has to be the correct/verified answer!. ✌ – Rajat Singh Aug 27 '21 at 09:06
  • 25
    With the new versions you'll need to go to Key Mappings then select a preset. You could also use Terminal.app Compatibility which gives the same result. – Ibrahim.H Sep 23 '21 at 08:19
  • 1
    @Ibrahim.H Thanks, this worked for me! iTerm2 v. 3.4.10 installed via brew. – Jannik Oct 07 '21 at 09:43
  • "Natural Text Editing" breaks the fn + backspace shortcut for "Delete" for me, which is a dealbreaker. – Alec Rust Nov 27 '21 at 12:40
  • this is absolute gold. saved me – romanzdk Dec 22 '21 at 18:53
  • Natural - should have been the default. Why keep a non-Natural thing as default – melchi Feb 17 '22 at 06:21
  • 1
    Perfect! Note that as of now (2022 March) it also includes the start/end mappings. (@MikeyN0's additional step is no longer needed) – Ambrose Little Mar 21 '22 at 15:03
  • 1
    This changing preset method should have been the accepted answer. – Shan Dou Apr 01 '22 at 17:44
  • 4
    why isn't this the accepted answer? This definitly is the best one! Thx – Naderio May 30 '22 at 07:40
  • 1
    I think this should be the accepted answer instead. The currently accepted answer is technically correct, but this one actually lands it by leveraging the preset mappings already available in iTerm to get you to feel like at home in iTerm, again. Thanks anyway!! – RickB Jun 10 '22 at 01:19
  • ure a lifesaver !!! – Manuel Lazo Jun 28 '22 at 18:48
  • 1
    You made my day! Should be the accepted answer. – Charles Morin Jul 21 '22 at 18:29
  • 2
    Thanks, this is great. Why isn't this the default setting in iTerm2? – a06e Oct 17 '22 at 21:12
  • This should be the accepted answer. Thank you – Keet Sugathadasa May 17 '23 at 10:11
  • Yesss. And when it asks "Remove all key mappings before loading the preset" hit "Remove" – valk May 24 '23 at 09:19
  • Confirmed working v3.4.20 – forresthopkinsa Aug 29 '23 at 05:04
  • Fresh Mac running Sonomo Prerelease, I don't see the presets. brew reinstall --cask iterm2 surely won't do anything other than re-install it (and I installed it yesterday). I had to add shortcuts in "keys", observe they were getting overridden by a profile, then "profiles -> keys -> delete the 2 offending lines" – P i Sep 07 '23 at 17:15
  • This preset should be set as default on iTerm2! Your answer should be accepted btw – Henry Bui Jan 11 '24 at 07:50
196

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

  • 3
    On MacOS 10.12, I need to use hex code 0x17 to get delete to beginning of word – midopa Jan 20 '17 at 04:16
  • works like a charm with macOS 10.12 and zsh – Sébastien Feb 07 '17 at 14:23
  • 1
    best answer ever you have a beer from me :) – infinity Jun 09 '17 at 13:46
  • 2
    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 02: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
  • @Meredith you can either select "Send hex" and put 0x1b before, which corresponds to Esc, or you can select "Send Escape" which prepends automatically Esc – Ciprian Tomoiagă Oct 16 '17 at 08:57
  • I had to do the first two, but the last two were already setup by default. Thanks! – bennythejudge Jan 10 '18 at 13:30
  • For delete to end of word, where does the code d come from? – Rian Rizvi Mar 22 '18 at 21:02
  • Try pressing ESC and then pressing d with some editable text typed in after the cursor. Any text after the cursor should be deleted, until a space is encountered. Please read https://www.gnu.org/software/bash/manual/html_node/Introduction-and-Notation.html#Introduction-and-Notation and https://www.gnu.org/software/bash/manual/html_node/Readline-Killing-Commands.html#Readline-Killing-Commands – David Winiecki Mar 25 '18 at 03:34
  • Why does 0x1B 0x08 (escape backspace) work as intended? Where can I redefine word boundary? – John Jiang Aug 17 '18 at 18:08
  • To answer your first question, if you read the two links in my last comment you can learn that pressing and releasing ESC, then pressing backspace, is notated as M-DEL, and that M-DEL will "Kill from the cursor [to] the start of the current word, or, if between words, to the start of the previous word." That behavior is built in to readline, the library that bash uses. I was not able to quickly find an answer to your second question. – David Winiecki Aug 20 '18 at 18:40
  • 1
    I created the shortcuts as suggested but initially they didn't work as they were being overridden by keyboard shortcuts in my profile (see profile menu). Deleting the respective entries in my profile seems to have done the trick. – Matt Parkins Feb 20 '19 at 14:25
103

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.

Volsk
  • 3,040
51

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.

jherran
  • 13,284
24

In build 3.3.12, you can select the Natural text editing preset and it will add all the necessary escapes to make it feel like you're navigating text in any other app.

enter image description here

Clement
  • 351
  • 2
    I have 3.4.15 and these options are gone (only defaults). Moreover, I can't find where to download these itermkeymaps... – rafraf Dec 29 '21 at 13:25
  • 1
    Thank you for the screenshot. This made is easy for me to get this small feature back. – Preet Sangha Mar 19 '22 at 05:50
  • 2
    @rafraf, I had that problem too, but I was looking at the wrong screen. Make sure you're on Settings -> Profiles -> Keys, and not on Settings -> Keys -> Key Bindings! – JakeRobb May 01 '23 at 16:23
  • How is this different from the Ricardo's THREE years older answer? – Andre Figueiredo Sep 10 '23 at 19:07
23

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
bdanin
  • 331
11

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
yosefrow
  • 211
4

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
syntagma
  • 10,888
esdee
  • 41
3

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.

rfabbri
  • 103
sebnukem
  • 131
  • 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 06 '19 at 17:59
0

Answer with more intuitive word breaks. If I have:

ls foo/bar foo/bar/baz

I want a word to be foo/bar, not foo. It took a while a while to get that working.

From this I added to .bash_profile:

# 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:

Screenshot of iTerm2 Preferences

Melissa
  • 101
0

For those how are interested this issue has the solution https://gitlab.com/gnachman/iterm2/-/issues/9806

-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

  • 4
    How does this differ from the other highly voted answer? https://apple.stackexchange.com/a/293988/237 – mmmmmm Feb 22 '18 at 19:04