Skip to content

Bash

Summary

  • Nearly every shortcut on this page isn't actually a "Bash" feature — it comes from GNU Readline, the line-editing library Bash links against.1
  • That's why the same shortcuts work in plenty of other places too: the Python REPL, psql, mysql, irb, and anything else built on Readline.
  • Notation follows the standard Readline convention: C- means hold Ctrl, M- means Meta — which on virtually every real keyboard is the Alt key, or Esc pressed first and released if there's no dedicated Alt/Meta key.1

Alt and Esc are (usually) the same thing

If your terminal doesn't pass Alt through directly, pressing Esc then a key does the identical thing as holding Alt and pressing that key. This is why you'll sometimes see the same shortcut written both ways — they're not two different bindings, just two notations for one keystroke.

Bash cursor movement cheat sheet

Figure 1. Bash cursor movement, visualized2

Moving the Cursor

Shortcut Description
ctrl + a Go to the beginning of the line
ctrl + e Go to the end of the line
ctrl + b Move back one character
ctrl + f Move forward one character
alt + b Move back one word
alt + f Move forward one word
ctrl + x ctrl + x Toggle between the start of the line and the current cursor position
ctrl + ] then x Move forward to the next occurrence of character x
alt + ctrl + ] then x Move backward to the previous occurrence of character x

Table 1. Moving the Cursor

Editing

Shortcut Description
ctrl + d Delete the character under the cursor
ctrl + h Delete the character before the cursor
ctrl + u Cut everything before the cursor
ctrl + k Cut everything after the cursor
ctrl + w Delete the word before the cursor
alt + [Backspace] Also delete the word before the cursor
alt + d Delete the word from the cursor forward
ctrl + y Paste the most recently cut text
ctrl + i Command completion (this is literally the same keystroke as [TAB])
ctrl + l Clear the screen (same as running clear)
ctrl + c Kill whatever's currently running
ctrl + z Suspend the current process to the background
ctrl + _ or ctrl + x ctrl + u Undo
alt + r Undo all changes to the current line
ctrl + t Swap the two characters before the cursor
esc + t / alt + t Swap the two words before the cursor
alt + c Capitalize from the cursor to the end of the word
alt + u Uppercase from the cursor to the end of the word
alt + l Lowercase from the cursor to the end of the word
esc + . / alt + . / esc + _ Insert the last argument of the previous command (vim a.txt b.txt → inserts b.txt)
alt + < Jump to the first line of history
alt + > Jump to the end of history (the line currently being typed)
alt + ? List files/folders in the current path, as help
alt + * Insert all matching completions as parameters
alt + ctrl + e Expand the command line (aliases, history, and shell expansions)
~ [TAB] [TAB] List all users
$ [TAB] [TAB] List all shell variables
@ [TAB] [TAB] List all entries in /etc/hosts
[TAB] Auto-complete

Table 2. Editing

cd - isn't a keybinding

Unlike everything above, cd - isn't a Readline shortcut at all — it's shell state: cd to your previous working directory. Worth knowing, just a different category of "shortcut."

Searching History

Shortcut Description
ctrl + r Search backward through history, starting from the current line
ctrl + s Search forward through history, starting from the current line
ctrl + p Previous command in history (same as ↑)
ctrl + n Next command in history (same as ↓)
alt + p Non-incremental reverse search of history
alt + n Non-incremental forward search of history
ctrl + o Execute the command found via ctrl + r/ctrl + s
ctrl + g Abort history search mode
!! Run the previous command (e.g. sudo !!)
!vi Run the previous command that begins with vi
!vi:p Print (don't run) the previous command beginning with vi
!n Execute the nth command in history
!$ Last argument of the last command
!^ First argument of the last command
^abc^xyz Replace the first occurrence of abc with xyz in the last command, and run it

Table 3. Searching History

Incremental vs. non-incremental search — what's the difference?

ctrl + r/ctrl + s are incremental — the match updates live as you type each character. alt + p/alt + n are non-incremental — you type the whole search term first, then press Enter, and only then does it search.

Kill a Job

Given a job number n (list your jobs with jobs):

kill %n

Example:

kill %1

My Bash Alias

alias hg='history | grep'

Useful Resources


  1. Ramey, C. (n.d.). GNU Readline Library. http://cnswww.cns.cwru.edu/php/chet/readline/readline.html ↩↩

  2. fliptheweb. (n.d.). bash-shortcuts-cheat-sheet [GitHub repository]. https://github.com/fliptheweb/bash-shortcuts-cheat-sheet ↩