iTerm2: delete line?
MacosTerminalIterm2Macos 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
) 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
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.
- Open the preferences (⌘+,) and go to the Keys tab.
- Add a global shortcut key, and just type in your shortcut
- In the Action dropdown, select Send Hex Code
The hex codes for...
- Deleting a word: 0x17.
- Deleting a line: 0x15.
- Moving to the beginning of the line: 0x01.
- 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:
- Go to Preferences (or
⌘+,
) - Profiles
- Keys
- Key Mappings
- Presets...
- Select the "Natural text editing" option