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.

Is there a shortcut in bash and zsh to delete one component of a path? For example, if I type ls ~/local/color/, and the cursor is at the end of line, is there a shortcut to delete the color/ at the end? Ideally I want solutions in both vi-mode and emacs-mode

share|improve this question
    
Don't you have seen Alt-BkSpc in my answer? –  enzotib Sep 30 '11 at 17:49
    
@enzotib I want to delete partial path, not whole path (one word) with one shortcut. –  Vayn Sep 30 '11 at 17:52
1  
From bash man page: "Words are composed of alphanumeric characters (letters and digits)." so it stops at /, and it works for me. I suggest you to give it at least a quick test. –  enzotib Sep 30 '11 at 17:56
    
@enzotib It's my fault, the shortcut also works fine in my bash shell. Thank you :D. Now I have another problem, which answer should be accepted... –  Vayn Sep 30 '11 at 18:07
    
me! me! me! Kidding. In all fairness enzotib's answer is more complete and more relevant to the subject. I would accept him :D –  rahmu Sep 30 '11 at 18:45
show 1 more comment

3 Answers

up vote 15 down vote accepted

The most commonly used commands in the default bash emacs mode, for most commonly used keyboards:

Movement

  • Ctrl-p, or Up: previous command
  • Ctrl-n, or Down: next command
  • Ctrl-b, or Left: previous character
  • Ctrl-f, or Right: next character
  • Alt-b: previous word
  • Alt-f: next word
  • Ctrl-a, or Home: begin of command
  • Ctrl-e, or End: end of command

Editing

  • BkSpc: delete previous character
  • Ctrl-d, or Del: delete current character
  • Alt-BkSpc: delete word to left
  • Alt-d: delete word to right
  • Ctrl-u: delete to start of command
  • Ctrl-k: delete to end of command
  • Ctrl-y: paste last cut

Miscellanea

  • Cltr-/: undo
  • Cltr-r: incremental backward history search
share|improve this answer
    
Complement: set a shortcut for this purpose in zsh bindkey [key] vi-backward-kill-word –  Vayn Sep 30 '11 at 19:36
add comment

By default bash (and I'm guessing zsh) will be in emacs-mode. You could try something like this:

Esc + b will put the cursor back one word. Ctrl + k will delete until the end of the line.

Most modern shells (like bash) will implement advanced command line editing features. Those commands are either close to emacs editing (Ctrl +A for line beginning, Ctrl + E for line end, ...).

If you're familar with vi-like editors, you could try to allow vi-mode.

set -o vi

It gives your shell vi-like modes (command mode/insert mode), and you get access to the standard commands (d for delete, r for replace, ...)

In Vi Mode, here's how you would do what you described:

Esc (command mode); d; b.
share|improve this answer
1  
I would not say "Command line editing is even more powerful and complete in vi-mode", emacs mode has commands to do everything you can do in vi mode. –  enzotib Sep 30 '11 at 16:35
    
I admit I got a bit carried away. I'll rephrase this. –  rahmu Sep 30 '11 at 16:36
    
Thanks for reminding me there is vi-mode in bash, so I can use db in this scenario in bash. I also found vi-backward-kill-word in zsh is what I want, and it is more convenient than set vi-mode in bash. But I still want to find an emacs mode shortcut for this purpose in bash. –  Vayn Sep 30 '11 at 17:07
add comment

There's also unix-filename-rubout for Readline!

# in ~/.inputrc
# press ctrl-b to delete unix filename parts
# see: man bash | less -p 'unix-filename-rubout' and
#      http://www.calmar.ws/vim/vi-bash.html
set editing-mode vi
set keymap vi
"\C-b": unix-filename-rubout
share|improve this answer
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.