Is it possible to configure ctrl-w (delete word)?
Solution 1
For this specific issue, you can also use:
Alt + Backspace
$ cd /home/me/test/a_dir/ # Alt + Backspace
$ cd /home/me/test/ # Alt + Backspace
$ cd /home/me/ # ...
Good reference: Adventures with bash's word erase
Solution 2
What worked for me was to add the following lines to my .bashrc
stty werase undef
bind '\C-w:unix-filename-rubout'
You need the undef line otherwise bash ignores your new binding for C-w
Solution 3
You should be able to use Esc, then backspace to delete words delimited by slashes.
You can change this by putting this in you .bashrc:
bind '\C-f:unix-filename-rubout'
Now use Ctrl+f
to do what you want.
Solution 4
With bash you can get the desired effect, putting the following in your ~/.bashrc file:
bind '"\C-w":backward-kill-word'
Hit CTRL+V
and the your key combination to see what it looks like for your terminal emulator. For instance CTRL+bksp
can be interpreted different on different terminals e.g. ^H
or ^?
. The ^
character is the same as CTRL
.
Solution 5
I'm not sure if this is specific to Mac OS, but I couldn't do a thing with C-w without also setting this readline option:
set bind-tty-special-chars Off
I added that to my ~/.inputrc, along with:
C-w: backward-kill-word
Now I'm able to delete backwards one word at a time.
Source: https://shallowsky.com/blog/linux/bash-word-erase.html
Related videos on Youtube
SkaveRat
Updated on September 18, 2022Comments
-
SkaveRat over 1 year
Normally, Ctrl+W deletes back to the last whitespace.
Is it possible to configure it to use additional characters, such as
/
?Edit: To be more clear: I don't want to configure the key for it, I want to have the deletion stop on
/
as well.Example:
vim /foo/bar^W vim /foo/
-
SkaveRat over 12 yearsI think I wasn't specific enough with my question. I wanted to have
/
as an additional stop-character, not a way to bind the delete-word-command on an other key. I edited my question -
Admin over 12 yearsUsing backward-kill-word binded to \C-w, it will erase backwards to the last given forward slash if present in a word and it will erase words delimited by whitespace. This is not what you wanted?
-
SkaveRat over 12 yearsMy system uses
^F
- but using"\^F"
in the script above doesn't work -
SkaveRat over 12 yearsthanks, that's the command that works. is there a way to overwrite the command of C-w? If I change it to
C-w:...
, the default behaviour of deleting to the next space still kicks in. On a different key (like F) if works fine. -
Admin over 12 yearsThe
Ctrl
character is written\C
in the script and not\^
. -
eppesuig over 11 yearsI don't think ctrl-w is managed by the shell alone. It may be managed by the tty also. What are your tty setting shown by
stty -a
? do you have control-w as default char for werase? If you remove that setting from tty, with commanstty werase undef
, does then bash works as expected? -
0fnt almost 9 yearsIt should be noted that
stty werase undef
will stop you from using C-w in any program (bind will allow you to use in readline) -
fedorqui over 7 years@ethanjyx oh. I am working on GNU bash. Are you also in this flavour?