51

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/

5 Answers 5

37

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

3
  • 3
    Awesome! Most other answers suggest rebinding Ctrl+w or Ctrl+b or something to unix-filename-rubout, but it's so much better that there's a built-in binding: M-backspace (Alt+backspace or Esc backspace) Mar 12, 2015 at 17:51
  • 1
    Did not work for me
    – ethanjyx
    Aug 1, 2016 at 22:21
  • @ethanjyx oh. I am working on GNU bash. Are you also in this flavour?
    – fedorqui
    Aug 22, 2016 at 9:12
30

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

1
  • 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, 2015 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.

2
  • 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, 2012 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, 2012 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.

4
  • 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, 2011 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?
    – user13742
    Dec 30, 2011 at 15:34
  • My system uses ^F - but using "\^F" in the script above doesn't work
    – SkaveRat
    Jan 16, 2012 at 18:53
  • 1
    The Ctrl character is written \C in the script and not \^.
    – user13742
    Jan 16, 2012 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.