You are not logged in.

#1 2012-05-06 17:58:47

From: Belgium
Registered: 2005-07-23
Posts: 256

Behaviour of ctrl+w in bash


The shortcut ctrl+w in bash deletes the word at the cursor until it reaches whitespace. This is often very handy. But sometimes I'd like to have a shortcut that deletes up to the last non-letter (or non-identifier) character, instead of up to whitespace.

E.g. if I have this in my terminal:

optipng images/1.png

Then pressing ctrl+w gives this:


While I'd like to have a shortcut that gives this:

optipng images/

Does that exist? I couldn't find any.



#2 2012-05-06 18:24:30

From: New York, USA
Registered: 2009-10-22
Posts: 4,097

Re: Behaviour of ctrl+w in bash

This sort of behavior is documented in readline(3), not by directly by bash. You can get this behavior by binding the 'unix-filename-rubout' action, e.g. by adding the below to your .inputrc:

"\C-e\C-w": unix-filename-rubout

^e^w will then delete backwards, respecting slashes as word boundaries.

Last edited by falconindy (2012-05-06 18:28:28)


#3 2012-05-06 19:24:29

From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: Behaviour of ctrl+w in bash

With the cursor at the end of the line, I would have used Alt+b to move backwards by punctuation-separated word, then used Ctrl+k to delete from the cursor to the end of the line.

Alt+b recognizes hyphens,underscores and slashes, as well as whitespace, as word boundaries.  Alt+f works similarly but forwards.  Ctrl+b and Ctrl+f are backwards and forwards by character – those arrow keys sometimes seem such a long distance away for my fingers that I use these shortcuts frequently.

Some other keyboard navigation and editing that I use:

Ctrl+a and Ctrl+e are shortcuts to the beginning and end of a line.
Ctrl+d deletes a single character; Alt+d deletes forward by punctuation-separated word.

Ctrl+Underscore undoes line edits one-by-one, you can repeat till you've finally restored to the bare prompt.


#4 2012-05-06 19:45:45

From: Belgium
Registered: 2005-07-23
Posts: 256

Re: Behaviour of ctrl+w in bash

Hmm, whoever designed those bash shortcuts did it really weird, they're so inconsistent...

Deleting a word backwards does not respect punctuation, and uses CTRL in the shortcut.
Deleting a word forwards does respect punctuation, and uses ALT in the shortcut.

Not to mention that there's no connection at all between the letters and the action for those smile


Last edited by aardwolf (2012-05-06 19:50:16)


#5 2012-05-06 20:08:22

From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,176

Re: Behaviour of ctrl+w in bash

OP may be interested in putting

set -o vi

in their .bashrc.

Pressing v will then let you edit the current command in $EDITOR.

Linux is NOT Windows | The Rootless Root
Toshiba Satellite i5-3230M 2.6GHz CPUs, 4Gb RAM, ArchLinux, wmii, nVidia GeForce GT 740M.


Board footer

Powered by FluxBB