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

  • 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?

  • kurtm

    kurtm Correct answer

    7 years ago

    ctrlw is the standard "kill word" (aka werase). ctrlu 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 ctrlp it will erase a word from the line. And if I hit ctrla, it will erase the whole line.

    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`.

    @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`.

    ctrl + w effectively deletes the word but it also copies to the clipboard, erasing anything you had stored (or pushing it down?).

    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/'

    esc - delete just worked :-)

    In my case, Ctrl+P cycles through previous commands like ArrowUp `↑` does.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM