Bash Command Line Editing Shortcuts

Originally Posted on with tags: linux / bash
Last Update on

I recently spent sometime reading two Linux command line books. The first is William Shotts’ The Linux Command Line (2e), and the second one is Learning the Bash Shell by Cameron Newbam and Bill Rosenblatt. Both books are good. The first book is written in a tutorial style and easy to understand. The second one is on Bash shell only and is more thorough.

bash-books

The Learning the Bash Shell book is in its third edition, and the book was published in April 2005. The first edition was published in 1995, so it is an old book. The third edition covers Bash 3.0. Bash 5.0 has already been released. However, most contents in the book still apply to current release.

Chapter 2 of the book describes that Bash has two editing mode: emacs and vi. The default is emacs mode. When you open a Terminal window (Ctrl + Alt + T) in Ubuntu, the command line is in emacs editing mode. You can change it to vi mode by command set -o vi and change it back by set -o emacs. The emacs mode is adequate for most situations. The mode has some useful shortcuts (listed below) in addition to common ones like Tab, Arrow Up, Arrow Down.

  1. Ctrl + a : Move cursor to the beginning of the line
  2. Ctrl + e : Move cursor to the end
  3. Ctrl + r : Search command history (Reverse incremental search)
  4. Ctrl + j : Work with Ctrl + r, copy command to command line
  5. Ctrl + k : Delete from cursor to the end of the line
  6. Ctrl + u : Delete from the beginning of the line to cursor
  7. Ctrl + l : Clear the screen
  8. Ctrl + t : Transpose two character

A reddit post “do you use bash vi mode” has some interesting discussion on whether to use vi mode. I personally think emacs mode with the above shortcuts is good enough.