Bash – How to delete part of a path in an interactive shell

bashshellzsh

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

Best Answer

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
Related Question