How can I delete a word backward at the command line (bash and zsh)?

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.


Alt+Backspace works for me in bash and zsh.


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:

  • https://superuser.com/questions/95626/control-backspace-unix-kill-rubout-for-readline
  • https://help.ubuntu.com/community/HowToReadline < for konsole