90

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?

4
  • 2
    Ctrl u is an option for erasing from cursor location to beginning... Apr 8 '13 at 15:23
  • 28
    Try alt + d, that might be useful, when emacs editing mode is set.
    – user76204
    Apr 8 '13 at 16:20
  • @Mik how do you enable emacs mode?
    – Adam Matan
    Apr 9 '13 at 9: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.
    – user76204
    Apr 9 '13 at 9:25
61

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.

5
  • 36
    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 3: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 6 '17 at 10:17
  • @NateC-K "in the world of keyboards where Meta doesn't exist": which today is, more or less, the world? Aug 9 '20 at 12:48
12

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

6
  • 18
    Option + d gives me ∂ on OSX Apr 16 '16 at 7:35
  • 4
    -1 OSX is not Ubuntu. Feel free to add a comment, but this is not an answer.
    – wjandrea
    Sep 24 '16 at 16:37
  • you have to first enable the checkbox "use option as meta key" in the terminal preferences (profiles -> keyboard tab -> checkbox) Apr 23 '18 at 16:15
  • control + d on mac Sierra
    – mfink
    Aug 9 '18 at 17:42
  • 4
    @IvanBalashov if you are using iterm set your let opt key as esc+ . Under Profiles >keys
    – tessie
    Nov 27 '18 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.