Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
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
add comment

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

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

3 Answers

up vote 12 down vote accepted

A single shortcut: M-backspace

share|improve this answer
1  
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
1  
In zsh, you may need to add bindkey "^[^?" vi-backward-kill-word to your .zshrc. –  Juliano Jan 5 '12 at 20:03
add comment

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

^abc^xyz

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
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
add comment

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
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
add comment

Your Answer

 
discard

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.