Bash – Delete whole argument in current bash command-line

bashinputrcreadline

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

Best Answer

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'