56

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/
TPS
  • 2,483
  • 5
  • 27
  • 45
SkaveRat
  • 3,807
  • 4
  • 21
  • 17

5 Answers5

40

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

fedorqui
  • 7,603
  • 7
  • 35
  • 71
33

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

mkm
  • 431
  • 4
  • 4
  • 5
    It 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) – 0fnt Jul 13 '15 at 16:58
27

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.

Not Now
  • 2,562
  • 2
  • 19
  • 18
  • 1
    thanks, 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. – SkaveRat Jan 16 '12 at 23:37
  • I 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 comman `stty werase undef`, does then bash works as expected? – eppesuig Dec 14 '12 at 13:19
7

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.

  • I 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 – SkaveRat Dec 30 '11 at 13:28
  • Using 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? –  Dec 30 '11 at 15:34
  • My system uses `^F` - but using `"\^F"` in the script above doesn't work – SkaveRat Jan 16 '12 at 18:53
  • 1
    The `Ctrl` character is written `\C` in the script and not `\^`. –  Jan 16 '12 at 23:41
2

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

Big McLargeHuge
  • 3,044
  • 11
  • 35
  • 49