iTerm2: delete line?

MacosTerminalIterm2

Macos Problem Overview


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?

Macos Solutions


Solution 1 - Macos

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 - Macos

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 - Macos

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 - Macos

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](https://coderwall-assets-0.s3.amazonaws.com/uploads/picture/file/2677/Screen_Shot_2014-03-01_at_3.08.41_PM.png "iterm2 keys")

Solution 5 - Macos

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.

Solution 6 - Macos

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.

Solution 7 - Macos

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven LuView Question on Stackoverflow
Solution 1 - MacosSteven LuView Answer on Stackoverflow
Solution 2 - MacosShawn WangView Answer on Stackoverflow
Solution 3 - MacosTravisView Answer on Stackoverflow
Solution 4 - MacosRyan EfendyView Answer on Stackoverflow
Solution 5 - MacosJZAUView Answer on Stackoverflow
Solution 6 - MacosRudolf RealView Answer on Stackoverflow
Solution 7 - MacosPublickerView Answer on Stackoverflow