257

How can I delete a word backward at the command line? I'm truly used to some editors deleting the last 'word' using Ctrl+Backspace, and I'd like that functionality at the command line too.

I am using Bash at the moment and although I could jump backward a word and then delete forward a word, I'd rather have this as a quick-key, or event as Ctrl+Backspace.

How can accomplish this?

1

6 Answers 6

341

Ctrl+W is the standard "kill word" (aka werase). Ctrl+U kills the whole line (kill).

You can change them with stty.

-bash-4.2$ stty -a
speed 38400 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc -xcase
iflags: -istrip icrnl -inlcr -igncr -iuclc ixon -ixoff ixany imaxbel
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -ocrnl -onocr -onlret -olcuc oxtabs -onoeot
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = <undef>;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
-bash-4.2$ stty werase ^p
-bash-4.2$ stty kill ^a
-bash-4.2$

Note that one does not have to put the actual control character on the line, stty understands putting ^ and then the character you would hit with control.

After doing this, if I hit Ctrl+P it will erase a word from the line. And if I hit Ctrl+A, it will erase the whole line.

9
  • 2
    Using the stty command I attempted to assign the "kill word" to Ctrl+BckSpc by typing stty werase followed by Ctrl-v Ctrl-Backspace. This inserts the literal Ctrl-Backspace character as the argument for stty werase. Unfortunately it does not require typing Ctrl-Backspace to delete the word but works with just the Backspace key. So I would like to second the request of @terdon to learn how to do this. To reset the werase to default, use stty werase Ctrl-v Ctrl-w. Oct 10, 2013 at 7:26
  • 2
    @TimothyMartin I'm a little stuck on how to do ctrl-backspace. Backspace is generally sent as either ctrl-h or ctrl-?, so ctrl-backspace would somehow be ctrl-ctrl-backspace. I did try to figure out if something different was sent, but putty, at least, just sends backspace when you type ctrl-backspace.
    – kurtm
    Oct 10, 2013 at 14:13
  • 3
    ctrl + w effectively deletes the word but it also copies to the clipboard, erasing anything you had stored (or pushing it down?). Dec 28, 2015 at 17:44
  • 2
    Anyone know how to make the 'word' stop at non word characters rather than only white spaces? IE with: 'curl a/asdf/xcvb', pressing ^w will get 'curl', but what I want is 'curl a/asdf/' Nov 23, 2016 at 5:49
  • 3
    esc - delete just worked :-) Nov 23, 2016 at 5:53
90

Alt+Backspace works for me in bash and zsh.

7
  • 5
    Doesnt work with iterm :( Jun 1, 2016 at 10:25
  • 12
    For example deleting a URL and using Ctrl+W it deletes the whole URL while Alt+Backspace deletes a word and it stops deleting when a special character is found.
    – Metafaniel
    Aug 24, 2016 at 15:20
  • 2
    And as always you have ctrl-y to yank/undo/paste.
    – Pablo A
    Sep 20, 2018 at 0:04
  • 2
    This solves it for iterm: github.com/fish-shell/fish-shell/issues/2124
    – Pascalius
    Jan 3, 2019 at 10:13
  • 1
    Ctrl+W close tab in cmder. Dec 15, 2020 at 10:41
43

You can also do this with CtrlW.

Another option is to set your own shortcut but this will depend on the terminal emulator you are using. For xterm and rxvt and maybe others, add this line to your ~/.inputrc (create the file if it does not exist):

## rxvt, xterm
"\b":backward-kill-word

gnome-terminal and its ilk seem to have AltBackspace by default but apparently require you to patch readline() in order to get CtrlBackspace.

Also see:

0
15

In Ubuntu 18.04.4 pressing Ctrl+V and then Ctrl+Backspace reveals that the Ctrl+Backspace combination is written as ^H.

You can show all current terminal key combinations using stty -a. In this case we want to change werase to ^H which can be done with

stty werase ^H

For any other key you want to remap you would again see how to write the new combination using Ctrl+V followed by the combination.

And you can find the command name (such as werase) by looking at stty -a and looking for the key combination an action is currently bound to.

3
  • It's 2020 and we can use backspace correctly on Linux :). The good news is I don't think this is specific to Ubuntu (see cs.colostate.edu/~mcrob/toolbox/unix/keyboard.html for some info). I've tried several different terminal emulators on arch and ^H works in each case.
    – pip
    Aug 30, 2020 at 17:40
  • My man! How does this only have +6? Works on Cygwin as well. Mar 6, 2021 at 0:25
  • And on Git Bash <3. Finally I have the same combination of keys in each shell. Great thanks! For anyone wondering how to do this for forward delete (CTRL + DEL), it's: bind '"5~": kill-word' Feb 20, 2022 at 9:53
2

On Mac, you can use:

Fn+Delete

3
  • 2
    That deletes 1 character in front of the cursor, not 1 word behind
    – Dannid
    Oct 14, 2016 at 18:45
  • Don't know if this is still relevant, but that would be <Alt> + <Backspace> on Mac.
    – Hassan
    Jan 15, 2017 at 2:15
  • <OPTION> <DELETE> Jul 26 at 1:08
2

Surprisingly, esc then delete works on macOS too.

Also, if using iTerm2, you can update the default profile:

Keys > Key Mapping > Presets > Natural Text Editing

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .