52

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.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Siggy
  • 529
  • 1
  • 4
  • 3
  • 12
    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

2 Answers2

61

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.

Kevin
  • 40,087
  • 16
  • 88
  • 112
11

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

AdminBee
  • 21,637
  • 21
  • 47
  • 71
dogbane
  • 29,087
  • 16
  • 80
  • 60