[macos] iTerm 2: How to set keyboard shortcuts to jump to beginning/end of line?

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

In order to get Alt+left/right arrow working correctly for word jumping follow this article.

This question is related to macos bash iterm iterm2

The answer is


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.


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

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


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.


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


For me, the following combination worked:
| Key Action Esc+ End result | |-----------------------------------------------------| | ?? Send Escape Sequence a Send ^[ a | | ?? Send Escape Sequence e Send ^[ e |


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"


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") and click "Keys" tab
  4. Click the "Presets" dropdown and select "Natural Text Editing"

enter image description here


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 “Keys” tab
  3. Click the [+] icon

You can now add the following keyboard shortcuts:

Move cursor one word left

  • Keyboard shortcut: ? + ?
  • Action: Send Hex Code
  • Code: 0x1b 0x62

enter image description here

Move cursor one word right

  • Keyboard Combination: ? + ?
  • Action: Send Hex Code
  • Code: 0x1b 0x66

Move cursor to beginning of line

  • Keyboard Combination: ? + ?
  • Action: Send Hex Code
  • Code: 0x01

Move cursor to end of line

  • Keyboard Combination: ? + ?
  • Action: Send Hex Code
  • Code: 0x05

Delete word

  • Keyboard Combination: ? + ?Delete
  • Action: Send Hex Code
  • Code: 0x1b 0x08

Delete line

  • Keyboard Combination: ? + ?Delete
  • Action: Send Hex Code
  • Code: 0x15

Undo

  • Keyboard Combination: ? + z
  • Action: Send Hex Code
  • Code: 0x1f

Don't forget to remove the previous bindings:

  • Open the “Profiles” tab
  • Click the sub-tab ”Keys”
  • Remove the mappings for key combinations ? + ? and ? + ?

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.


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


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.


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.

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

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


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to iterm

How can I scroll up more (increase the scroll buffer) in iTerm2? How to export iTerm2 Profiles iTerm2 keyboard shortcut - split pane navigation iTerm 2: How to set keyboard shortcuts to jump to beginning/end of line?

Examples related to iterm2

How can I scroll up more (increase the scroll buffer) in iTerm2? How to export iTerm2 Profiles iTerm 2: How to set keyboard shortcuts to jump to beginning/end of line?