135

I'm trying to map ⌘+Delete (backspace) to delete to the beginning of the line (like it works in browsers and text editors) in iTerm2 and I'm unable to find a working escape code for it. I tried 1K (^[1K) based on what I read in Wikipedia. It just prints a "K".

Edit: I found Ctrl+U. Now to find out how to map it. Maybe Hex code 21 (U being 21st letter), so 0x15?

JV Lobo
  • 5,526
  • 8
  • 34
  • 55
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • 6
    try `CTRL+A CTRL+K` in the meantime. Or `CTRL+C` – Carlos Mar 31 '13 at 19:09
  • 2
    ctrl+K is a good one to learn for sure. But I'm more used to cmd+delete. I must have it! It works everywhere else! – Steven Lu Mar 31 '13 at 19:11
  • 1
    Here are a few more you might find useful. `ALT+Backspace` -> Send Hex 0x17, `ALT+RightArrow` -> Send Exc Seq f, `ALT+LeftArrow` -> Send Exc Seq b – Carlos Mar 31 '13 at 19:16
  • Alt+Delete already worked out of the box. I did just recently set ^[b and ^[f, and they work great. I really want specifically Cmd+Delete to delete the whole line. – Steven Lu Mar 31 '13 at 19:20
  • @Carlos, good point with ctrl+C and I've been doing that quite a bit actually. It's different behavior though and I don't want to accidentally terminate stuff either. For example if I'm in some kind of interpreter and want to delete the line I entered into it. The Ctrl+C only works in a specific state, and this sort of state dependency is precisely what I'm trying to get away from. State dependency is the bane of usability. – Steven Lu Mar 31 '13 at 19:21
  • I agree. I would actually like to have that Cmd+Delete mapping so ping me if you find it ;) – Carlos Mar 31 '13 at 19:28
  • @Carlos hopefully you saw the answer. – Steven Lu Jan 11 '14 at 18:44

8 Answers8

167

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!

Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • 4
    I wish I can upvote this 5 more times. I spend so much time looking for this. How do you figure 0x15 from 0x1 and 0x5? – bizi Sep 12 '13 at 01:47
  • is it: hex( char('U') - char('A') + 1 ) – bizi Sep 12 '13 at 01:51
  • 2
    Haha, well, `u` is just the 21st letter of the alphabet! So I believe that expression is correct. – Steven Lu Sep 12 '13 at 05:44
  • 2
    For info, following the same logic, to delete until the END of the line, Ctrl-K... K is the 11th letter of the alphabet, or 0xB in Hexadecimal. So assign 'Command-Del->' to hex 0xB to delete until the end of the line. – user2707671 Mar 25 '15 at 14:24
  • Well Delete is actually backspace in Apple-land so you mean Cmd+Fn+Del, yes. – Steven Lu May 12 '15 at 20:26
  • 1
    @bizi Expanding on Steven Lu's answer, 'u' being the 21st and 'k' the 11th letters of the alphabet, the hex codes desired are hex(21) & hex(11), which come out to '0x15' & '0xb'. I found myself looking those up more often, so for ease just run (replacing `x` with the desired letter, of course): `python -c "import string; print(hex(string.ascii_lowercase.find('x') + 1))"` or if you want to get fancy/fun, add this to your `~/.bashrc` or `~/.bash_profile`: `alias hexletter='python -c "import string,sys; print(hex(string.ascii_lowercase.find(sys.argv[-1]) + 1))"'` – TCAllen07 Feb 03 '16 at 22:52
  • Ctrl + Q also seems to delete a whole line – Amit Dash Apr 26 '16 at 05:06
  • Is there a master list of these commands? The ones stated are incredibly helpful, I'm sure there's a bunch more that would help a bunch of people out with there command line efficiency. – mikeymurph77 Oct 26 '16 at 17:53
  • 2
    Just gonna leave this here. https://garbagecollected.org/2017/01/31/four-column-ascii/ – Steven Lu Feb 01 '17 at 21:32
128

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

Shawn Wang
  • 2,314
  • 1
  • 16
  • 19
68

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

Travis
  • 13,311
  • 4
  • 26
  • 40
  • Interesting. Those hex values correspond to Ctrl+X (cancel), DEL (see an ascii chart). It is pretty sensible for this sequence to become interpreted as a more potent form of DEL, though this is the first I am hearing of it. When I type it in the terminal though (without setting any binds, on Linux), Zsh just beeps at me and Bash just inserts a `^X` and then the backspace will just erase it. I did try this with a keybind in iTerm (which will send the sequence together without human delay) and see the same behavior. this indicates to me that bash would also require a bind for it. – Steven Lu Sep 02 '15 at 00:45
  • But you're totally right that `backward-kill-line` is the one to use for true parity with `⌘ + ←Delete`. The extra configuration is just a bit cumbersome, it's not bad at all though. – Steven Lu Sep 02 '15 at 00:47
  • 1
    For some reason it deletes it from history. When i use up arrow i no longer get the entry i deleted – Xitcod13 Feb 04 '20 at 19:26
57

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

nullify0844
  • 4,641
  • 1
  • 22
  • 16
17

I hope this may help you

map ⌥ <- Delete to Send Hex Codes: 0x1B 0x08

I had test for it, and it is correct.


18.09.2013 update

this delete one word, not a line.

DawnSong
  • 4,752
  • 2
  • 38
  • 38
JZAU
  • 3,550
  • 31
  • 37
  • 1
    This is interesting! What you describe is equivalent to Alt+Ctrl+H, which my particular PuTTY setup also issues when pressing Alt+Ctrl+Backspace. In my zsh shell it backspaces over a word (not the whole line). – Steven Lu Jun 19 '13 at 17:36
  • I'll note that Ctrl+H `^H` is supposed to actually mean "backspace" for many terminals. The Alt-/meta-/escape-prefix turning that into a "kill-word" seems sensible, though it's not clear how widely supported it is. – Steven Lu Sep 12 '13 at 05:48
  • 1
    It's deleting a word not a line, but exactly what I was looking for :) – hyouuu Sep 17 '13 at 22:45
  • Thank you thank you thank you thank you thank you. I have no idea what changed, but this was the exact behavior on my old Mac (and, in fact, is how it works in this text box), but for some reason on my new Mac this was entirely different. Every other suggestion caused it to treat "foo/bar/baz" as _one_ word rather than 3. – Dan Jan 12 '16 at 18:31
7

As pointed ^U deletes the line. You can easily remap the command by using Better Touch Tool.

It also has cool features for automation, mouse, pad and keyboard mapping. Also includes a window feature for smart borders.

Rudolf Real
  • 1,948
  • 23
  • 27
  • 1
    I think some people are looking for a built-in solution and don't want to use additional tools, thus the downvote. Unfortunately this is not always possible and delegating to a second tool is a good cheat. – Rudolf Real May 10 '19 at 15:28
7

On iTerm2 you can set the exact same shortcuts of your OS. In this case, as the default shortcut to delete a line on Mac OS is ⌘+Delete (backspace), you can do so.

To set the default keybindings of iTerm you have to:

  1. Go to Preferences (or ⌘+,)
  2. Profiles
  3. Keys
  4. Key Mappings
  5. Presets...
  6. Select the "Natural text editing" option

enter image description here

Publicker
  • 71
  • 1
  • 2
2

The location has changed. In order to enable natural editing go to:

Preferences -> Profile -> Keys -> Load Presets... -> and select Natural Text Editing

enter image description here

For more info and tweeks go https://blog.arturofm.com/install-iterm-terminal-emulator-on-macos/

Arturo
  • 3,254
  • 2
  • 22
  • 61