Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've become pretty proficient with a number of bash shortcut keys that make my bash-ing faster: C-a/C-e, C-u, C-w, M-f/M-b, C-r etc. One common task that I haven't found a good shortcut for though is when I want to delete the last segment of a path:

Say I have

ls ~/projects/arcaneweb/libraries

and I realize I actually meant

ls ~/projects/arcaneweb/sources

Is there a way to just delete libraries, saving a load of keystrokes?

share|improve this question

migrated from serverfault.com Dec 28 '11 at 21:15

This question came from our site for system and network administrators.

You're asking about bash, so I'm posting this as a comment instead of an answer: In zsh you can accomplish what you want by setting the WORDCHARS env variable: WORDCHARS=${WORDCHARS//[-\/]} (removes - and / from being part of a "word"). I find this very useful so thought I'd share. – James O'Gorman Dec 28 '11 at 19:23
I usually just hit M-Backspace repeatedly until desired effect is reached. – jw013 Dec 29 '11 at 2:52
up vote 15 down vote accepted

A single shortcut: M-backspace

Alt + ←      

share|improve this answer
Wow, that's so obvious! I guess I always assumed this would be the same as C-w for some reason. – notJim Jan 5 '12 at 17:12
In zsh, you may need to add bindkey "^[^?" vi-backward-kill-word to your .zshrc. – Juliano Jan 5 '12 at 20:03

There is a replace option but not sure it would really save keystrokes unless the path was long.


This will replace the first occurrence of abc with xyz.

[root~]# ls -l /var/log/messages
-rw-------  1 root root 850312 Dec 28 14:08 /var/log/messages
[root ~]# ^messages^secure
ls -l /var/log/secure
-rw-------  1 root root 652614 Dec 28 14:08 /var/log/secure

Bash Shortcut reference: http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/

share|improve this answer
Oh, that's awesome. Not great for this particular use case, but definitely will be useful elsewhere. – notJim Dec 28 '11 at 19:19

M-b,C-k is the quickest I've found, made a bit less so due to needing to use ESC as Meta. If you have Alt/Option bound as Meta, this would be reasonably fast once you develop the muscle memory for it.

share|improve this answer
Binding option to meta is the first thing I do when setting up my terminal :). M-b, C-k is pretty good, I think I can get used to that. – notJim Dec 28 '11 at 19:17
there's also M-b,M-d but all those are for the emacs keybindings, there's also vi-mode – Samus_ Dec 29 '11 at 0:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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