14

Using ^W (unix-word-rubout) I can easily delete a single "word" from the current command in my bash shell.

However, when dealing with quoted arguments that contain spaces (or unquoted arguments containing backslash-escaped spaces) this doesn't work nicely as it only deletes one word and not the whole argument.

So I'm looking for a way to delete a whole argument.

Example (cursor is always at the end):

$ foo --bar "foo bar" meow
[black magic]
$ foo --bar "foo bar"
[black magic]
$ foo --bar
[black magic]
$ foo
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
ThiefMaster
  • 2,297
  • 2
  • 23
  • 25
  • Is switching to zsh an option? (I don't know how to do it in zsh either offhand, but it has some command line parsing code accessible to the line editor (or at least the completion system), so that should make it easier). – Gilles 'SO- stop being evil' Apr 05 '13 at 22:02

1 Answers1

15

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

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

bind '"\C-p": shell-backward-kill-word'
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Nykakin
  • 3,919
  • 1
  • 19
  • 18