Skip to content

Command Line Editing for Beginners and Experts Alike

24 October 2022

Command Line Editing: -Learn how to lines quickly and efficiently, regardless of your skill level. This article covers the basics of command line editing, as well as more advanced techniques.

Command Line Editing Tricks

  • Ctrl-a moves to beginning of line
  • Ctrl-e moves to end of line
  • Ctrl-u deletes to beginning of line
  • Ctrl-k deletes to end of line
  • Ctrl-arrow moves left or right by word

Command line editing
Combining command line editing with history is great way to easily modify previously run commands. The default key bindings in bash are the same as those in the text editor emacs; vi-style bindings are also available for use. To enable them, run the command set -o vi. To make the change permanent, and that command to your .bashrc file so that it is run whenever bash starts.

The following table shows the emacs-style bindings with their corresponding vi-style bindings.

emacsvi
Ctrl-aEsc,I
Ctrl-eEsc,A
Ctrl-uEsc,d,^
Ctrl-kEsc,D
Ctrl-Left arrowEsc,b
Ctrl-Right arrowEsc,w

Using the Home and End keys work to move to the beginning or end of the line as well.

To view all key bindings, run the bind -p command. Where is shows a \C, this signifies the Ctrl Key. \M
signifies the meta key, or Alt.

Examples:

“\C-I”: clear-screen – Use Ctrl-I to clear off the screen.
“\M-I”: downcase-word – Use Alt-I to change a word from upper-case to lower-case. Useful when you didn’t realize you had the Caps Lock on.