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:

$ set -o vi

To go back to Emacs mode:

$ 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:

# ~/.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

ActionEmacs modeVi mode
Beginning of the lineCtrl + a0
End of the lineCtrl + e$
Previous wordAlt + b (*)b
Next wordAlt + f (*)w
Previous characterCtrl + bh
Next characterCtrl + fl

#Option + Click

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

#Deleting

ActionEmacs modeVi mode
Character under the cursorCtrl + dx
Character before the cursorCtrl + hX
From the current position to the beginning of the lineCtrl + ud0
From the current position to the end of the lineCtrl + kD
LineCtrl + e, then Ctrl + udd
From the current position to the beginning of the wordCtrl + wdb
From the current position to the end of the wordAlt + d (*)dw
Word around current positionAlt + b, then Alt + d (*)bdw

#Other editing actions

ActionEmacs modeVi mode
UndoCtrl + _u
Clear screen (leaves the prompt and current command)Ctrl + lCtrl + l
AutocompletionTabTab

#Command history

ActionEmacs modeVi mode
Previous commandCtrl + pj
Next commandCtrl + nk
SearchCtrl + rCtrl + 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

ActionEmacs modeVi mode
Send SIGINT to current processCtrl + cCtrl + c
Send EOT character to current processCtrl + dCtrl + d
Send SIGSTP to current processCtrl + zCtrl + 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.