54

Possible Duplicate:
How to delete part of a path in an interactive shell?

Is there a short-cut in bash that lets you delete the last part of a path?

Example: /usr/local/bin should become /usr/local/ (or /usr/local)

I know of Ctrl+w but it deletes the complete last word and I'd like to retain that functionality, too.

2
  • 13
    <Esc> <BackSpace> on prompt commandline of the shell from the end. Commented Jan 10, 2012 at 15:40
  • @Siggy... You've mentioned Ctrl-w.. this is a key-combination which has nothing to do with bash. Key-combinations like this are for a user interface, like the terminal or text-editor or menus.. They are pretty much meaningless in a bash script... The command-line is not itself bash; it belongs to the terminal which passes the commands on to bash (or whatever shell you are running)... Your question is, at a glance, ambiguous; that is why you have two different types of answers...
    – Peter.O
    Commented Jan 10, 2012 at 16:41

2 Answers 2

65

In a path, it's quite easy, dirname takes off the last component of the path. And since it's a program (as opposed to a builtin) it's completely portable between shells.

$ dirname /usr/local/bin
/usr/local

It appears you mean while editing an active line at the prompt. In that case Nikhil's comment of esc backspace (consecutively, not both at the same time) is correct.

3
  • 1
    The question is about command line edition, not about string manipulation. Commented Jan 10, 2012 at 23:45
  • I see. @nikhil you should post that comment as an answer so I can delete this. Or at least so Siggy can accept it.
    – Kevin
    Commented Jan 11, 2012 at 0:01
  • 1
    Do not know, maybe does not answer the question but I Googled for string manipulation, found this and I am happy so upvoting.
    – h22
    Commented Nov 17, 2020 at 15:06
11

Assuming, you are using emacs bindings, you can type Alt+Backspace to delete the previous word.

5
  • This was what I was looking. But can you tell me where this shortcut comes from? I can't find it in the output of bind -P.
    – user14181
    Commented Jan 10, 2012 at 22:33
  • 1
    @Siggy it should be named backward-kill-word.
    – user13742
    Commented Jan 10, 2012 at 23:29
  • 1
    @Siggy Backspace appears as \C-?, because it's ASCII character number 127, which is a control character (a non-printable character). Commented Jan 11, 2012 at 0:12
  • This doesn't work for me.
    – Kevin
    Commented Jan 11, 2012 at 0:24
  • $ bind -P |grep backward-kill-word says backward-kill-word can be found on "\M-\C-h", "\M-\C-?". Commented Mar 12, 2015 at 17:53

Not the answer you're looking for? Browse other questions tagged .