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.

marked as duplicate by Michael Mrozek Jan 12 '12 at 17:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 12
    <Esc> <BackSpace> on prompt commandline of the shell from the end. – Nikhil Mulley Jan 10 '12 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 Jan 10 '12 at 16:41

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

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.

  • 1
    The question is about command line edition, not about string manipulation. – Gilles Jan 10 '12 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 Jan 11 '12 at 0:01

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

  • 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 Jan 10 '12 at 22:33
  • 1
    @Siggy it should be named backward-kill-word. – user13742 Jan 10 '12 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). – Gilles Jan 11 '12 at 0:12
  • This doesn't work for me. – Kevin Jan 11 '12 at 0:24
  • $ bind -P |grep backward-kill-word says backward-kill-word can be found on "\M-\C-h", "\M-\C-?". – Colin D Bennett Mar 12 '15 at 17:53

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