Command line keyboard shortcuts

You’re about to run a command only to notice a typo at the beginning. Looks familiar?

Navigating to a typo without shortcuts
Navigating to a typo without shortcuts.

There is a better way!

Navigating to a typo with shortcuts.
Navigating to a typo with shortcuts.

Had it not been for Learn Enough Command Line to Be Dangerous by Michael Hartl (I highly recommend this book if you feel uncomfortable working with the command line), I still wouldn’t know that there are any command line keyboard shortcuts other than Ctrl + c, Ctrl + d and Tab.

Please note that this post is about Bash. If you’re using some other shell, I have no idea if any of this will work for you. If you don’t know what your shell is, it’s probably Bash (at least on Mac OS X and Ubuntu). Keep reading (and check by running ps -p $$).

Emacs mode vs Vi mode

By default, Bash uses Emacs shortcuts. If you prefer using Vi shortcuts, switch to Vi mode:

1
$ set -o vi

To go back to Emacs mode:

1
$ set -o emacs

Setting editing mode this way works only for the current shell session, so if you close the Terminal window, it will reset. To persist this setting, add it to your .inputrc. There are many things concerning both Emacs and Vi mode you can set in this file, but it’s a topic for another blog post. I will only mention the one I can’t imagine using Vi without:

1
2
3
4
5
6
7
8
9
# ~/.inputrc
# use Vi mode
set editing-mode vi

$if mode=vi
  set keymap vi-insert
  # use "jk" to exit insert mode
  "jk": vi-movement-mode 
$endif

If you have no previous experience with neither Emacs nor Vi, I suggest you stick to the default Emacs shortcuts for Bash, simply because it’s more probable that if you have to work on someone else’s computer, it will use the default settings. Vi shortcuts might seem more appealing, because they’re shorter and easier to remember, but keep in mind that using a Vi shortcut when typing a command requires you to go into Vi’s command mode, using the shortcut and then going back to Vi’s insert mode to keep typing. If you have no idea what I’m talking about, make sure you learn the basics of Vi(m) before trying out Vi mode for command line navigation.

Cursor movement

Action Emacs mode Vi mode
Beginning of the line Ctrl + a 0
End of the line Ctrl + e $
Previous word Alt + b (*) b
Next word Alt + f (*) w
Previous character Ctrl + b h
Next character Ctrl + f l

Option + Click

Move cursor anywhere inside the command by clicking. Works only on OS X.

Deleting

Action Emacs mode Vi mode
Character under the cursor Ctrl + d x
Character before the cursor Ctrl + h X
From the current position to the beginning of the line Ctrl + u d0
From the current position to the end of the line Ctrl + k D
Line Ctrl + e, then Ctrl + u dd
From the current position to the beginning of the word Ctrl + w db
From the current position to the end of the word Alt + d (*) dw
Word around current position Alt + b, then Alt + d (*) bdw

Other editing actions

Action Emacs mode Vi mode
Undo Ctrl + _ u
Clear screen (leaves the prompt and current command) Ctrl + l Ctrl + l
Autocompletion Tab Tab

Command history

Action Emacs mode Vi mode
Previous command Ctrl + p j
Next command Ctrl + n k
Search Ctrl + r Ctrl + r

Command history is shell-specific and can be found in ~/.bash_history. It means that if you’re using more than one terminal emulator (like Terminal and iTerm), they share the same command history.

Process control

Action Emacs mode Vi mode
Send SIGINT to current process Ctrl + c Ctrl + c
Send EOT character to current process Ctrl + d Ctrl + d
Send SIGSTP to current process Ctrl + z Ctrl + z

(*) Alt key

Some Emacs shortcuts using the Alt key are already mapped to something else on many operating systems and might not work as expected. To enable those shortcuts in OS X’s Terminal, look for ‘use Option as Meta key’ in its settings.