568

I can see that Ctrl+left/right jumps to the beginning/end of line. How to change this to Cmd+left/right arrow?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
chris
  • 7,222
  • 5
  • 31
  • 37

14 Answers14

971

Add in iTerm2 the following Profile Shortcut Keys

FOR ACTION SEND
⌘ ← "SEND HEX CODE" 0x01
⌘ → "SEND HEX CODE" 0x05
⌥ ← "SEND ESC SEQ" b
⌥ → "SEND ESC SEQ" f

Here is a visual for those who need it

iTerm add key visual

pradeexsu
  • 1,029
  • 1
  • 10
  • 27
Matthias
  • 10,816
  • 2
  • 19
  • 12
  • 3
    Do you know what the code/sequence for Command+Delete is? (clear out the prompt) – Steven Lu Mar 31 '13 at 18:37
  • 12
    I answered my first comment elsewhere, basically hex-code 0x15 gives Ctrl+U which clears the line. However I am finding 0x01 Ctrl+A to interfere with Tmux usage. Therefore I intend to map it instead to the Home key (same functionality as Fn+Left on a Mac keyboard). – Steven Lu Apr 04 '13 at 01:28
  • 7
    The escape sequence for Home is [1~ and the escape sequence for End is [4~ – Steven Lu Apr 04 '13 at 03:12
  • 1
    As in, Home is the sequence `^[[1~` and End is the sequence `^[[4~`. This will not interfere with tmux. I do, however, suggest taking advantage of tmux integration in iTerm. – Steven Lu Apr 04 '13 at 03:33
  • @nivoc: how do you know this? How I can learn to write my own hex code that serves the purpose I want? – Novellizator Mar 31 '15 at 17:47
  • 1
    Installing my new Mac, figuring out shortcuts don't work on iTerm, coming here, using this answer, pleased, want to upvote, figuring out I already did (probably with my previous mac). Thanks ;) – Avi Apr 06 '15 at 11:13
  • 2
    @Cyberwiz and that made my week :) – Matthias Apr 08 '15 at 22:35
  • for "⌥←" and "⌥→", I should also remove the key in Profile -> Key tab. and then it will work as expected. – Jerry Chen Jul 02 '15 at 13:53
  • I was hitting command and arrow one after another and was wondering why command does not get typed in :/ – Tarun Aug 04 '16 at 13:04
  • 49
    The correct binding is `⌘← "SEND ESC SEQ" OH` for Home and `⌘→ "SEND ESC SEQ" OF` for End (those are uppercase 'o's not zeros). This simulates actually pressing the Home and End keys, and as such will work in bash, vim, etc. – Timothy Zorn Feb 03 '17 at 08:38
  • Mine had issues with the last two commands so don't forget to remove the previous bindings from profiles. – sinaza Apr 26 '18 at 17:56
  • Use ^[[1~ and ^[[4~ not 0x1 and 0x4. 0x1 will interfere with tmux/screen if your prefix is Ctrl-A. – user464014 May 11 '20 at 21:43
  • 1
    for gods###, add a step: RESTART THE TERMINAL when done. I literally spent 30 minutes adding and removing shortkeys as they would not work initially lol. Joking apart, great tip – Alejandro Moreno Aug 04 '20 at 11:20
  • Why is it saying "The keyboard shortcut you have set for this profile will take precedence over an existing shortcut for the same key combination in a global shortcut." when I try the end/beginning of line commands? – wordsforthewise Aug 30 '21 at 13:31
  • 1
    It's worth pointing out that these bindings can be set from `~/.zshrc` so they work in other terminals as well (like the one built into VS Code).`bindkey '^[b' backward-word; bindkey '^[f' forward-word; bindkey "^A" beginning-of-line; bindkey "^E" end-of-line` – colton7909 Nov 18 '21 at 19:22
803

To jump between words and start/end of lines in iTerm2 pick one of the two solutions below.

1. Simple solution (recommended)

  1. Open Preferences
  2. Click "Profile" tab
  3. Select a profile in the list on the left (eg "Default")
  4. Click "Keys" tab
  5. Click "Key Mappings" tab (if it exists)
  6. Click the "Presets" dropdown and select "Natural Text Editing"

enter image description here

Note: if you have several profiles (f.e. Default and Hotkey Window) and want the same modification to be applied for all profiles, use these steps instead:

  1. Click "Keys" tab
  2. Choose "Key Bindings"

2. Mapping keys manually (Advanced)

If you don't want to use the "Natural Text Editing" preset mentioned above, you can map the keys you need manually:

  1. Open Preferences
  2. Click "Profile" tab
  3. Select a profile in the list on the left (eg "Default")
  4. Click "Keys" tab
  5. Click "Key Mappings" tab (if it exists)
  6. Click the [+] iconjuimp
  7. Add the shortcuts from the table below
SHORTCUT DESCRIPTION ACTION SEND
⌘ ← Jump left to beginning of line "SEND HEX CODE" 0x01
⌘ → Jump right to end of line "SEND HEX CODE" 0x05
⌘ ← Delete Delete line "SEND HEX CODE" 0x15
⌥ ← Jump left to beginning of word "SEND HEX CODE" 0x1b 0x62
⌥ → Jump right to end of word "SEND HEX CODE" 0x1b 0x66
⌥ ← DELETE Delete word "SEND HEX CODE" 0x1b 0x08
⌘ z Undo "SEND HEX CODE" 0x1f

Note

If keyboard bindings already exists for the shortcuts above, they must be removed for the new ones to take effect.

sqren
  • 22,833
  • 7
  • 52
  • 36
312

I see there's a lot of good answers already, but this should provide the closest to native OSX functionality as possible in more than just your shell. I verified that this works in ZSH, Bash, node, python -i, iex and irb/pry sessions (using rb-readline gem for readline, but should work for all).

Open the iTerm preferences +, and navigate to the Profiles tab (the Keys tab can be used, but adding keybinding to your profile allows you to save your profile and sync it to multiple computers) and keys sub-tab and enter the following:

Delete all characters left of the cursor

+←Delete Send Hex Codes:

0x15 More compatible, but functionality sometimes is to delete the entire line rather than just the characters to the left of the curser. I personally use this and then overwrite my zsh bindkey for ^U to delete only stuff to the left of the cursor (see below).

or

0x18 0x7f Less compatible, doesn't work in node and won't work in zsh by default, see below to fix zsh (bash/irb/pry should be fine), performs desired functionality when it does work.

Delete all characters right of the cursor

+fn+←Delete or +Delete→ Send Hex Codes: 0x0b

Delete one word to left of cursor

+←Delete Send Hex Codes:

0x1b 0x08 Breaks in Elixir's IEX, seems to work fine everywhere else

or

0x17 Works everywhere, but doesn't stop at normal word breaks in IRB and will instead delete until it sees a literal space.

Delete one word to right of cursor

+fn←Delete or +Delete→ Send Hex Codes: 0x1b 0x64

Move cursor to the front of line

+ Send Hex Codes: 0x01

Move cursor to the end of line

+ Send Hex Codes: 0x05

Move cursor one word left

+ Send Hex Codes: 0x1b 0x62

Move cursor one word right

+ Send Hex Codes: 0x1b 0x66

Undo

+z Send Hex Codes: 0x1f

Redo typically not bound in bash, zsh or readline, so we can set it to a unused hexcode which we can then fix in zsh

++Z or +y Send Hex Codes: 0x18 0x1f

Now how to fix any that don't work

For zsh, you can setup binding for the not yet functional +←Delete and ++Z/+y by running:

# changes hex 0x15 to delete everything to the left of the cursor,
# rather than the whole line
$ echo 'bindkey "^U" backward-kill-line' >> ~/.zshrc

# binds hex 0x18 0x7f with deleting everything to the left of the cursor
$ echo 'bindkey "^X\\x7f" backward-kill-line' >> ~/.zshrc

# adds redo
$ echo 'bindkey "^X^_" redo' >> ~/.zshrc

# reload your .zshrc for changes to take effect
$ source ~/.zshrc

I'm unable to find a solution for adding redo in bash or readline, so if anyone know a solution for either of those, please comment below and I'll try to add them in.

For anyone looking for the lookup table on how to convert key sequences to hex, I find this table very helpful.

Community
  • 1
  • 1
Travis
  • 13,311
  • 4
  • 26
  • 40
  • 3
    That is really useful, makes a lot of difference! I would like to point it out that some people can get confused with the `$` in front of `echo` (just remove it?). Other than that, it's perfect. Thank you. – Edmundo Santos Dec 15 '15 at 15:45
  • ⌥+← Send Hex Codes: 0x1b 0x62 does not work for me, not that the converse (-> works). – Christophe May 27 '16 at 14:12
  • @Christophe What shell are you using? For bash, run `bind -p | grep backward-word` for zsh run `bindkey | grep backward-word` and let me know what the output is. Both zsh and bash should both bind `backward-word` to `"\eb"` or `"^[b"` (which are both `esc` + `b`) by default. – Travis May 27 '16 at 14:48
  • @Travis Here is what I get: ➜ / bindkey | grep backward-word "^[B" backward-word "^[[1;5D" backward-word "^[b" backward-word – Christophe May 27 '16 at 18:14
  • The result is that going backward inserts 3 'b' by the way (forward works). – Christophe May 27 '16 at 18:18
  • 1
    @Christophe So it looks like the key binding is there like it should be. Either you made a mistake adding the hex values in or keybinding for backward-word is being overwritten. Run `bindkey | grep "\^\[b"` and that'll tell you if something else is overwriting it after it's been set. It **should** only return `#=> "^[b" backward-word`. If it's being overwritten, you'll have to find where its being overwritten and remove it. If that's not possible, you can run `echo 'bindkey "^[b" backward-word' >> ~/.zshrc` it will add it to the last line of your `.zshrc`, then restart iTerm and it should work – Travis May 27 '16 at 19:36
  • Let me know if that doesn't work for you. I'm happy to help where I can as I know how frustrating a broken keybinding can be. – Travis May 27 '16 at 19:39
  • @Travis, sorry did not come back to this before today. So your debug line returns me: `"^[b" backward-word`. And only that. So looks like I'm good? But still does not work. I have a French keyboard layout if that matters. – Christophe May 30 '16 at 13:01
  • 2
    @Christophe check the `keys` tab in the iTerm preferences to see if it's also mapping `⌥`+`←` to something. If so remove it. You can test if your `b` is working correctly by quickly pressing `esc` releasing, then pressing `b`. That should jump a word to the left. Alternatively, you can map `⌥`+`←` to hex `0x1b 0x42` **or** `0x1b 0x5b 0x31 0x3b 0x35 0x44`. I verified that both of those work for me. I really think you have `⌥`+`←` being overwritten in iTerm though, either in your `profile` `keys` tab or `keys` tab. – Travis May 31 '16 at 14:10
  • @Travis thanks a lot `0x1b 0x5b 0x31 0x3b 0x35 0x44` works for me. – Christophe May 31 '16 at 14:59
  • 1
    the Undo command in iTerm is I-N-S-A-N-E. everyone will want that, i bind it to cmd-u so it doesn't collide with the iterm default shortcut for cmd-z. – Merlin Oct 24 '17 at 08:20
  • 3
    Amazing! Just learned about Undo plus Redo bindings from here. – ecbrodie Mar 31 '18 at 18:44
  • 8
    I did this manually in and I thought why can't I import a profile to do this? Then I created [GabLeRoux/iterm2-macos-dynamic-profile](https://github.com/GabLeRoux/iterm2-macos-dynamic-profile) based on these instructions. It works flawlessly, thanks – GabLeRoux Aug 01 '18 at 19:07
  • In recent versions of iTerm, there's a "Natural Text Editing" preset of keys which has many of these key-bindings pre-set. Was also noted here: https://stackoverflow.com/a/42575631/544618 – nomæd Sep 22 '18 at 15:46
  • 1
    @GabLeRoux By god man, don't hide that gem in a comment hidden in a collapsible section of the comments to this answer! Make an answer of your own and shamelessly promote your repo. Also, Travis, you should point to this repo in your own answer so that others don't have to do go through the manual process. – elifiner Dec 28 '18 at 21:16
  • Has anyone had luck with `⌥+←Delete` working on directories deleting folder by folder and not the whole directory? I'm using `0x1b 0x08` (iTerm2 & zsh) Sorry for necroing btw ‍♂️ – JBK Apr 18 '19 at 07:21
  • @JBK if you're referring to `⌥+←Delete` to delete by each directory level in `path/to/something`, then `0x1b 0x08` *should* work. Verify that when you type `bindkey | grep "\^\[\^H"` you should get `"^[^H" backward-kill-word` returned. If not, ideally you would want to remap `^[^H` to `backward-kill-word` by adding it to your `.zshrc` with `echo 'bindkey "^\[^H" backward-kill-word' >> ~/.zshrc` – Travis Apr 18 '19 at 16:44
  • @Travis First of all - thanks for replying! I get `backward-kill-word` for `bindkey | grep "\^\[\^H"` and have added `bindkey "^\[^H" backward-kill-word'` to my `.zshrc` file regardless, `⌥+←Delete` still deletes whole paths. Looking at my `.zshrc` file I had `autoload -U select-word-style select-word-style bash` configured inside, is it of any concern to the issue? – JBK Apr 19 '19 at 11:51
  • 1
    @JBK redefining the behavior is starting to step outside of my knowhow. However one of the answers within here might be able to help you https://stackoverflow.com/questions/444951/zsh-stop-backward-kill-word-on-directory-delimiter – Travis Apr 19 '19 at 12:48
  • 1
    @Travis thanks Travis, I got it fixed. My issue was that I sourced the `zsh-syntax-highlighting` plugin before using autoload! Thanks a lot for your time – JBK Apr 19 '19 at 15:18
  • will `bindkey "^U" backward-kill-line` be a global shortcut? – alper May 14 '20 at 12:11
  • @alper If you simply run `bindkey "^U" backward-kill-line` in your terminal, it'll only work within that terminal session. If you run `echo 'bindkey "^U" backward-kill-line' >> ~/.zshrc` , it will append `bindkey "^U" backward-kill-line` to your `~/.zshrc` file, which is loaded every time a new zsh session is created, so it will bind the key for all new sessions. – Travis May 14 '20 at 19:42
  • I tried that but seems like it does not work on the zsh session where I connected to a remote machine using `ssh` @Travis – alper May 14 '20 at 20:47
  • @alper if `bindkey "^U" backward-kill-line` works by running it in your terminal for that session, then it sounds like your .zshrc is loading properly. Drop a `echo "hello world"` into your .zshrc to confirm if you'd like. – Travis May 15 '20 at 00:27
61

In iTerm 3.0.12 you can switch to Natural Text Editing preset:

enter image description here

iTerm → Preferences → Profiles → Keys

Warning As it is a preset, it can override the keys you have binded before. So it's better to save your current key bindings before applying a preset.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
  • 2
    Could you elaborate more your answer? As in, what that preset includes and might overwrite? – Neithan Max Nov 15 '17 at 22:00
  • Thanks for the answer. Also chiming in it's probably best practice to apply this to your own custom profile that way you aren't screwing with the defaults and can toggle the profile as a natural text editing "mode" – aug Jan 29 '18 at 18:36
  • 1
    Simplest and easiest solution by far. Should be #1 answer. – wordsforthewise Aug 30 '21 at 13:34
  • @CarlesAlcolea I think it will remove your custom presets and hotkeys so you should export the profile if you want to save them. – wordsforthewise Aug 30 '21 at 13:34
  • @CarlesAlcolea and @wordsforthewise Look at the `warning` – Nike Kov Aug 31 '21 at 10:31
54

For quick reference of anyone who wants to go to the end of line or start of line in iTerm2, the above link http://hackaddict.blogspot.com/2007/07/skip-to-next-or-previous-word-in-iterm.html notes that in iTerm2:

  • Ctrl+A, jumps to the start of the line, while
  • Ctrl+E, jumps to the end of the line.
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
0 _
  • 10,524
  • 11
  • 77
  • 109
50

Follow the tutorial you listed above for setting up your key preferences in iterm2.

  1. Create a new shorcut key
  2. Choose "Send escape sequence" as the action
  3. Then, to set cmd-left, in the text below that:
    • Enter [H for line start
      OR
    • Enter [F for line end
Stphane
  • 3,368
  • 5
  • 32
  • 47
Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
13

I used Travis answer and I created a dynamic profile you can import based on its instructions.

GabLeRoux/iterm2-macos-dynamic-profile

Instructions are in the readme and it's a lot faster to import this than it is to add them all manually. I made this an answer as per @gooli's request because this was hidden in a comment. Hope you enjoy this

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
11

Just to help out anyone that is having the same issue but specifically using Zsh shell with iTerm 2. It turns out that Zsh doesn't read /etc/inputrc properly, and so fails to understand any key bindings you set up through the preferences!

To fix this, you need to add some key bindings to your .zshrc file, such as:

# key bindings
bindkey "\e[1~" beginning-of-line
bindkey "\e[4~" end-of-line

Note the backslashes in the example above before the "e", the linked article does not show them, so add them into your .zshrc file when adding bindings.

Nerdwood
  • 3,947
  • 1
  • 21
  • 20
7

The only things that work for for moving to the beginning and end of line are

⌘← "SEND ESC SEQ" OH - to move to the beginning of line
⌘→ "SEND ESC SEQ" OF - to move to the end of line

zx485
  • 28,498
  • 28
  • 50
  • 59
add787
  • 383
  • 2
  • 4
  • 12
4

The old fashion emacs bindings can still work in iterm2 and os x terminal:

Preferences -> Profiles -> Keys (sub tab in profiles)

  • Set Left/Right option <kbd>⌥</kbd> key acts as +Esc (similar in os x terminal)

This should enable alt-f and alt-b for moving words by words. (Still ctrl-a and ctrl-e always work as usual)

If set as meta those old bindings will work while some iterm2 bindings unavailable.

halfelf
  • 9,737
  • 13
  • 54
  • 63
3

As Explains in here, you can do it with a simple steps:

By default, word jumps (option + → or ←) and word deletions (option + backspace) do not work. To enable these, go to "iTerm → Preferences → Profiles → Keys → Load Preset... → Natural Text Editing → Boom! Head explodes"

Islam.Ibrahim
  • 789
  • 6
  • 19
2

bind -p will show a list of bound escaped keys in your shell, that might help giving you more ideas / search terms.

Jose Alban
  • 7,286
  • 2
  • 34
  • 19
2

explained in https://aaronaddleman.com/articles/hexcodes-and-iterm/

you can use xxd -psd to get key hex code.

Zitao Xiong
  • 956
  • 1
  • 10
  • 18
1

For me, the following combination worked:

Key Action Esc+ End result
Send Escape Sequence a Send ^[ a
Send Escape Sequence e Send ^[ e
Halil Özgür
  • 15,731
  • 6
  • 49
  • 56