30

In Bash Emacs mode, is there any way to delete till the previous slash character?

For example, if I entered the command cp /usr/local/bin/reallylongincorrectfolder /home/myname/reallylong_and_correct_path and want to just delete the reallylongincorrectfolder, is there any shortcut?

This is a very common scenario for me in Bash.

Something like dF<char> in vi?

woodstok
  • 991

3 Answers3

19

Alt-Backspace and Ctrl-w are commonly mapped to backward-kill-word, which does that. If you want to find out what it's mapped to on your system (if anything), run bind -P | grep '^backward-kill-word'.

As explained by @Barmar, this is different from unix-word-rubout, which removes to the previous space boundary.

l0b0
  • 7,453
17
bind -P |grep unix-filename-rubout

To test out the keybinding with eg. Ctrl-b:

bind \\C-b:unix-filename-rubout

For permanent usage, add it to ~/.inputrc

12

Put this in your .inputrc and start a new shell:

C-b:unix-filename-rubout

Ctrl-b now erases backwards to the next slash.

Nirvana!

Don't forget Ctrl-XCtrl-E will launch your editor so you can edit a complicated command line comfortably.

Greg Bell
  • 495