What's the shortcut to delete a word "forward" in a unix terminal?

14,804

Solution 1

This depends on your shell and its active command line editing mode.

For a shell with Emacs command line editing mode (set -o emacs in some shells), use Alt+D (this doesn't work on macOS for whatever reason, but prints the character , use Escd instead).

For a shell with Vi command line editing mode (set -o vi in some shells), use Escdw (this does work on macOS as well).

Solution 2

In the Linux terminal, you can try Ctrl+K to delete from the cursor to the end of the line.

There are few other shortcuts such as:

  • Ctrl+E: move the cursor to the end of the line.
  • Ctrl+T: flip the two characters around the cursor.
  • Ctrl+Y: yank the text from the kill ring (paste previously deleted text).
  • Ctrl+U: delete the text from the start of the line to the cursor.
  • Ctrl+O: submit the current line and load the next line from history (if any).
  • Ctrl+P: fetch the previous line from history (like ).
  • Ctrl+K: delete the the rest of the line after the cursor.
  • Ctrl+A: move the cursor to the beginning of the line.
  • Ctrl+D: if the line is empty, equivalent to EOF; typically, exit the shell. Otherwise, delete the character after the cursor.
  • Ctrl+F: move the cursor forward by one character.
  • Ctrl+H: same as Backspace.
  • Ctrl+J: same as Enter.
  • Ctrl+M: same as Enter.
  • Ctrl+B: same as .
  • Ctrl+W: delete the word to the left of the cursor.

Solution 3

For the tty line discipline, ^W deletes the previous white-space delimited word.

In the vi editor in insert mode, ^W deletes backward to the start of the first sequence of alnums or non-alnums (on foo-+-bar.. baz, it first deletes baz, then .., then bar, then -+-, then foo).

In the emacs editor, ^W deletes from the cursor position to the mark (the one you set with Ctrl+Space).

Some line editors like readline (used by bash, gdb...), zle (used by zsh), pdksh's when in vi mode, behave like vi in that regard and when in emacs mode behave like the tty line discipline (not emacs).

libedit (used by BSD shells or (optionally) dash), tcsh, AT&T ksh, in emacs mode, behave like emacs where ^W deletes to the mark (initially at the beginning of the buffer).

For deleting a word forward, in the vi editor, you'd do it in command (normal) mode with dw to delete to the beginning of the next sequence of alnums or non-alnums (or the end of the line) and dW to delete to the next sequence of non-blanks (the pendant of the ^W of the tty line discipline).

In the emacs line editor, Meta-D would delete to the end of the next sequence of alnum characters. The word motion operators (Meta-B, Meta-F) behave similarly.

command line editors, when in vi mode, behave like vi, but in emacs mode, you have two main schools: the tcsh school and the ksh school.

The ksh school (readline, ksh, yash) behaves mostly like emacs (fish's behaviour is slightly different in how it treats non-alnum, non-whitespace characters in both emacs and vi mode).

In the tcsh school (tcsh, libedit, zsh), word motions are based on whitespace-delimited words so are consistent with the tty line discipline's ^W in that regard.

In zsh, the behaviour is customizable with different word styles for all the word motion widgets.

For readline, you can get the tcsh school (and have Meta-D delete the same kind of word as Ctrl-W does) by adding to your ~/.inputrc:

set keymap vi-move
"\e[EMACS~": emacs-editing-mode
set keymap emacs
"\e[VI~": vi-movement-mode
"\ed": "\e[VI~dW\e[EMACS~"
"\ef": vi-fWord
"\eb": vi-bWord

Or on the other hand, align ^W to the other word motion widgets (behave like Meta-Backscape) with:

set keymap emacs
"\C-W": backward-kill-word

As to what the Meta key is, see there.

Share:
14,804

Related videos on Youtube

Glide
Author by

Glide

Updated on September 18, 2022

Comments

  • Glide
    Glide over 1 year

    I do Ctrl+W in the mac terminal to delete a word (deletes from where the cursor is at to the beginning of the word)

    How do I do the opposite - deletes from where the cursor is to the end of the word?

  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    Only in zsh though does Meta-D delete the same type of word that Ctrl-W does (blank delimited ones). In bash in vi mode, you can use dW to delete those
  • Kusalananda
    Kusalananda over 6 years
    @StéphaneChazelas Yes, they (the two keycombos) have a different view of what constitutes a "word".
  • Jozef Legény
    Jozef Legény almost 6 years
    You can check the "use option as meta" in the terminal Keyboard preferences to make it work.
  • neverMind9
    neverMind9 over 5 years
    Both Esc+D and Alt+D work in xfce4 terminal 0.6.3.