94

Consider the following bash command line, where ^ denotes the cursor location:

svn commit -m very/long/path/to/some/file "[bug 123456] Fix the pixel issue"
              ^

I'd like to commit a different file with the same message. How can I delete the current word, from cursor location to the next space? Is there also a shortcut for backward deletion, form the cursor to the first space backwards?

Update: ctrl+w erases backwards, but which shortcut erases one word forward?

wjandrea
  • 14,109
  • 4
  • 48
  • 98
Adam Matan
  • 12,289
  • 24
  • 71
  • 90
  • 2
    `Ctrl u` is an option for erasing from cursor location to beginning... – nutty about natty Apr 08 '13 at 15:23
  • 30
    Try `alt + d`, that might be useful, when emacs editing mode is set. –  Apr 08 '13 at 16:20
  • @Mik how do you enable emacs mode? – Adam Matan Apr 09 '13 at 09:00
  • @AdamMatan `set -o emacs` enables it, but it is usually the default; if not, you can put that line in `.bashrc` or `.bash_aliases`, then source the file or reload the terminal. However, then the shortcuts you may be used to in vi mode won't be available, although ones such as ctrl+c will because they are not Bash shortcuts. –  Apr 09 '13 at 09:25

2 Answers2

62

I answered similar question on unix.stackexchange.com:

Bash has readline commands that aren't bound by default. You can find them at reference: http://www.gnu.org/software/bash/manual/html_node/Bindable-Readline-Commands.html#Bindable-Readline-Commands

Command you are looking for is called "shell-backward-kill-word". You have to select shortcut first. Let's use Crtl+p, since it's "previous command" - same as up arrow.

bind '"\C-p": shell-backward-kill-word'

The only difference is you have to use "shell-kill-word" command instead, since you want to delete forward.

There is also a "kill-word" command with Meta+d shortcut (try Esc+d if you don't have Meta key). It will delete only one part of path at once.

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
Nykakin
  • 3,732
  • 19
  • 22
  • 37
    Alt-d works for me – ricab Jan 20 '15 at 14:24
  • 9
    Generally, in the world of keyboards where Meta doesn't exist, Alt = Meta. – Nate C-K Mar 20 '15 at 03:49
  • I the world where you have many different keyboards, a convenient and fairly useful fact is that Esc as a prefix also acts as Meta. – tripleee Nov 30 '15 at 18:03
  • 1
    In short, add e.g. this to your $HOME/.inputrc file:`"\ew": shell-kill-word` , that will make Alt-w forward delete a word , much like CTRL-w backwards deletes a word (and unlike Alt-d which will forward delete but stop at e.g. a / or - and other characters) – user964970 Jul 06 '17 at 10:17
  • @NateC-K "in the world of keyboards where Meta doesn't exist": which today is, more or less, the world? – Peter - Reinstate Monica Aug 09 '20 at 12:48
14

Tested both the esc+d and alt/opt+d on OSX Mavericks and they work there as well.

Gayan Weerakutti
  • 3,670
  • 26
  • 37
visyoual
  • 157
  • 1
  • 4