How to set keyboard shortcuts to jump to beginning/end of line? [closed] How to set keyboard shortcuts to jump to beginning/end of line? [closed] bash bash

How to set keyboard shortcuts to jump to beginning/end of line? [closed]


Add in iTerm2 the following Profile Shortcut Keys

FORACTIONSEND
⌘ ←"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


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

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 “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 +


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.