iTerm2: delete line?

Solution 1:

I got it. I have no idea why Hex Code mappings in iTerm2 produce the associated Ctrl+key mappings, but they do. No idea what 0x00 means, either, as it's not assigned to A as might be expected. (though I do believe Unix has its own conventions relating to treating null bytes -- we have e.g. xargs accepting a null byte delimiting format from find for example -- It would be neat if we can bind this to a hotkey with iTerm2)

I was able to find that Ctrl+U does nearly the exact task I want (it deletes the entire line rather than deleting only what is before cursor, but whatever... Ctrl+Y as a bonus can bring it all back). Then I curiously saw that I had hex codes 0x1 and 0x5 mapped to ^A and ^E respectively, for my Cmd+Left and Cmd+Right... so 0x15 is for ^U!

Solution 2:

For Mac OS, most editor share the common shortcut + Delete: delete to start of the line, in iTerm2 we can switch to this key configuration

enter image description here

Solution 3:

Mapping hex code 0x15 to + ←Delete in most shells deletes the entire line (content to the left and right of the cursor). While sometimes not as compatible, I find that mapping:

+←Delete to Send Hex Codes:

0x18 0x7f

performs the desired functionality. If you're running ZSH, you'll likely also need to add this to your .zshrc file:

$ echo 'bindkey "^X\\x7f" backward-kill-line' >> ~/.zshrc

as by default ZSH doesn't map backward-kill-line to anything.

Furthermore, you can also delete everything to the right of your cursor by mapping:

+fn+←Delete or +Delete→ to Send Hex Codes:

0x0b

I wrote a comprehensive guide to adding most of OSX's standard keybinding to your terminal here

Solution 4:

In OSX, ⌥+⌫ and ⌘+⌫ are the shortcuts for deleting a word and deleting a line respectively. ⌘+ ← and ⌘+ → are for going to the beginning and end of lines. By default, iTerm2 isn't configured this way, and there are a lot of misleading guides online. The following is what I've found to work on my machine.

  1. Open the preferences (⌘+,) and go to the Keys tab.
  2. Add a global shortcut key, and just type in your shortcut
  3. In the Action dropdown, select Send Hex Code

The hex codes for...

  1. Deleting a word: 0x17.
  2. Deleting a line: 0x15.
  3. Moving to the beginning of the line: 0x01.
  4. Moving to the end of the line: 0x05. Just open a new tab, and it should work!

Here is a screenshot, for clarity: alt text