With iTerm2 on Mac, how to delete forward a word from cursor on command-line?
Asked Answered
T

9

49

With iTerm2 1.0.0 on Mac OS X 10.6.8, I'd like to delete from cursor to the next end of word, i.e. deleting one word forward. I tried Alt+d but this types the delta operator symbol and doesn't delete. How to suppress the typing but let it delete?

Thiosinamine answered 9/9, 2012 at 1:17 Comment(0)
M
35

I don't have a mac handy, but ESC d should in theory do the same thing that ALT-d does.

NOTE: this means hitting ESC, releasing it, then hitting d. Look ma, no key chords.

Malamute answered 9/9, 2012 at 1:36 Comment(3)
Correct; ESC typically acts as the meta key in Mac OS X, since the Alt key (Option) is used for other purposes.Hankering
Where to see a complete list of such key combinations for editing at command line?Thiosinamine
meh. it works but it ain't so very comfortable on new machines where the ESC is a on the touch bar. seems like manually mapping this to Alt-d is the better choice.Multiplechoice
I
79

PreferencesProfileKeys add the following shortcuts:


Send Escape Sequence Esc+ b

Send Escape Sequence Esc+ f

Send Escape Sequence Esc+ [H

Send Escape Sequence Esc+ [F

←Delete Send Hex Code 0x18 0x7f (add bindkey "^X\\x7f" backward-kill-line to .zshrc if you use zShell)

←Delete Send Hex Code 0x1B 0x08

Del→ Send Hex Code 0x0b

Del→ Send Escape Sequence d

Indifferentism answered 9/6, 2016 at 7:55 Comment(2)
⌥←Delete : Send Hex Code 0x1B 0x08 to delete a word backwards till spl. character. ( self notes )Advertisement
If you're using Emacs and want to send Meta-d (delete next word) when pressing ⌘+d, then set: ⌘+d to Send Hex Code 0x1B 0x64.Ire
M
35

I don't have a mac handy, but ESC d should in theory do the same thing that ALT-d does.

NOTE: this means hitting ESC, releasing it, then hitting d. Look ma, no key chords.

Malamute answered 9/9, 2012 at 1:36 Comment(3)
Correct; ESC typically acts as the meta key in Mac OS X, since the Alt key (Option) is used for other purposes.Hankering
Where to see a complete list of such key combinations for editing at command line?Thiosinamine
meh. it works but it ain't so very comfortable on new machines where the ESC is a on the touch bar. seems like manually mapping this to Alt-d is the better choice.Multiplechoice
R
19

Just to add to Alexsander's answer the easiest way to replicate this behaviour is to select the option "Left option key acts as +Esc" in the Preferences->Profiles-> Keys screen.

Reprise answered 24/10, 2012 at 13:22 Comment(0)
W
14

You can add a shortcut for it, go to "Preferences" → "Profiles" → "Keys", and then click on "+" to add a custom shortcut, and add this:

enter image description here

Now you should be able to delete a word forward by clicking + d.

Waits answered 28/12, 2017 at 6:30 Comment(0)
B
12

Even simpler solution is to go to "Preferences" → "Profiles" → "Keys" and in the "Presets..." dropdown, choose "Natural Text Editing"

Blisse answered 3/7, 2022 at 15:4 Comment(0)
G
6

You can follow this article to set up the key binding in iterm2. It works for me. The hex codes for...
Deleting a word: 0x17.
Deleting a line: 0x15.

enter image description here

Ginsburg answered 19/5, 2020 at 12:32 Comment(0)
H
4

Surprised no-one mentions this one, which works for me:

Settings -> Profiles -> Default (or whatever) -> Keys -> General -> Left Option key: switch from Normal to Esc+.

Heeltap answered 8/11, 2021 at 10:11 Comment(1)
Did this in old one and somehow forgot this for my new mac book. This is simple. Now ALT + back/forward arrow moves word. ALT + d delete word forward , ALT + backspace delete word behind the cursor.Serif
K
1

maxbellec answer was really helpful, I'll just add that:

⌘← Send Escape Sequence Esc+ [H

⌘→ Send Escape Sequence Esc+ [F

did not do the trick for me. First, these shortcuts were already configured in the Preferences -> Keys -> Key Bindings as other actions shortcut (next/previous tab). But changing these shortcuts to other combinations also did not work. So, I've added:

bindkey "^[[F" end-of-line

bindkey "^[[H" beginning-of-line

to the .zshrc, which solved it.

Katabolism answered 5/12, 2021 at 14:2 Comment(0)
T
0

ctrl+d was a forward delete for me in irb and iex in iTerm2. But since I wanted to use the regular del key on the keyboard with the numpad, I looked for something else. And then I found a list of hex codes corresponding to ctrl+something key strokes: 4 is number and the number is 4. iTerm2 Del key mapping

Tremendous answered 1/12, 2015 at 8:57 Comment(2)
This is forward delete a character, the question is about deleting a wordGeosyncline
It closes terminal window as well. Not the best solution.Inae

© 2022 - 2024 — McMap. All rights reserved.