Among the many useful keyboard shortcuts available in the bash shell, there is Ctrl–W to delete the word to the left of the cursor. Let's suppose my command line looks like the following:
cp some-file /foo/bar/baz/copy
Now I'd expect to be able to press Ctrl–W and end up with the following:
cp some-file /foo/bar/baz/
In Vim's command line it actually works this way: Only alphanumeric characters are treated as "word", whereas special characters (like /
) serve as delimiters marking the start of a new "word".
But unfortunately it doesn't work like that in all shells I've used so far. Only spaces will delimit a "word", so pressing the shortcut with the command line shown above will give me:
cp some-file
Is there a way to make Bash behave like Vim? Some configuration I can put into my .bashrc
?
Best Answer
This has nothing to do with Vim, all editors behave that way (including emacs), they treat non-word characters as delimiters. Anyway, the behavior you are talking about is controlled by
readline
and its manual lists quite a few commands you can assign shortcuts to. I am pasting a few relevant ones here but I recommend you readman readline
for more info:So, the one you want is
backward-kill-word
, which uses non alphanumeric characters as word boundaries. By default, it is assigned to Alt+Backspace but you can change that by using either the global/etc/inputrc
if you want them to apply to all users or (better) your own local$HOME/.inputrc
.As far as I can tell, Ctrl+W seems to be reserved and you can't use that one but you can choose another shortcut, Ctrl+J for example. Create a
$HOME/.inputrc
file if it doesn't exist and add this line to it:That should be enough for most modern terminal emulators. However, some older terminals use different codes. If you're using
xterm
, for example, the line above should be written as: