Bash readline deleting till the previous slash

Posted on

Problem :

In bash emacs mode, is there anyway 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 comman scenario for me in bash.
Something like dF<char> in vi?

Solution :

bind -P |grep unix-filename-rubout

To test out the keybinding with eg. Ctrlb:

bind \C-b:unix-filename-rubout

For permanent usage, add it to ~/.inputrc

AltBackspace and Ctrlw 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.

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

C-b:unix-filename-rubout

Ctrlb now erases backwards to the next slash.

Nirvana!

Don’t forget CtrlXCtrlE will launch your editor so you can edit a complicated command line comfortably.

Leave a Reply

Your email address will not be published. Required fields are marked *