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?

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

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

  • 1
    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

A single shortcut: M-backspace

Alt + ←      

  • 1
    Wow, that's so obvious! I guess I always assumed this would be the same as C-w for some reason. – davidtbernal Jan 5 '12 at 17:12
  • 1
    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/

  • 1
    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.

  • 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
  • 1
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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