Bash Keyboard Shortcuts: Move, Edit and Search the Command Line Faster
On this page
If you fix typos by holding down the arrow keys, this guide will save you a lot of time. Bash uses a library called Readline to read what you type, and Readline comes with dozens of keyboard shortcuts for moving the cursor, deleting words and searching your history. You only need a handful to feel much faster.
The shortcuts below are the default "emacs mode" bindings that every Bash installation starts with. They also work in many other programs that use Readline, such as python3, mysql and gdb.
A note about the Alt key
Shortcuts written as Alt+B use what Readline calls the Meta key. On most Linux terminals that is Alt. On macOS, enable "Use Option as Meta key" in Terminal's profile settings (in iTerm2, set the Option key to "Esc+"). If Alt does not work anywhere, press and release Esc, then press the letter: Esc B does the same as Alt+B.
Moving the cursor
- Ctrl+A: jump to the start of the line.
- Ctrl+E: jump to the end of the line.
- Alt+B: move back one word.
- Alt+F: move forward one word.
- Ctrl+B and Ctrl+F: move back or forward one character (the same as the arrow keys).
A typical use: you typed a long command and realize it needs sudo in front. Press Ctrl+A, type sudo , press Enter. No arrow keys involved.
Cutting and pasting
Readline calls cutting "killing" and pasting "yanking". Killed text goes into a kill ring, separate from your desktop clipboard.
- Ctrl+U: cut from the cursor back to the start of the line.
- Ctrl+K: cut from the cursor to the end of the line.
- Ctrl+W: cut the word before the cursor (words are separated by spaces).
- Alt+Backspace: cut the word before the cursor, stopping at punctuation such as
/or.. - Alt+D: cut the word after the cursor.
- Ctrl+Y: paste the most recently cut text.
- Alt+Y: right after Ctrl+Y, replace the pasted text with the previous item in the kill ring.
The difference between Ctrl+W and Alt+Backspace is handy with paths. With the cursor at the end of cd /var/log/nginx, Ctrl+W removes the whole path, while Alt+Backspace removes only nginx.
Fixing typos
- Ctrl+_ (Ctrl+Shift+minus on most keyboards): undo the last edit. Press again to keep undoing.
- Ctrl+T: swap the two characters around the cursor, so
slbecomesls. - Alt+T: swap the current word with the previous one.
- Alt+U, Alt+L, Alt+C: uppercase, lowercase or capitalize from the cursor to the end of the word.
Searching your history
Ctrl+R is the single most useful shortcut in Bash. Press it and start typing any part of an earlier command. Bash shows the most recent match:
(reverse-i-search)`rsync': rsync -avz ./site/ web01:/var/www/site/
- Press Ctrl+R again to jump to older matches.
- Press Enter to run the match as it is.
- Press Ctrl+E or an arrow key to stop searching and edit the match first.
- Press Ctrl+G to cancel and restore the line you had.
Ctrl+S searches forward, but many terminals use it for flow control and the screen appears to freeze instead. If that happens, Ctrl+Q unfreezes it. Running stty -ixon (add it to ~/.bashrc to keep it) frees Ctrl+S for searching.
Ctrl+P and Ctrl+N step through previous and next commands, just like the up and down arrows. The Bash history guide covers history settings and searching in much more depth.
Reusing arguments and commands
Alt+. inserts the last argument of the previous command. Press it repeatedly to cycle through the last arguments of older commands. It is perfect for this kind of sequence:
$ mkdir -p ~/projects/new-site
$ cd
After typing cd , press Alt+. and Bash fills in ~/projects/new-site.
History expansion does similar things, but by typing rather than with a key. These are not Readline shortcuts; Bash expands them when you press Enter, and it prints the expanded command before running it:
!!: the entire previous command. The classic issudo !!after a "Permission denied".!$: the last argument of the previous command, like Alt+..
$ apt update
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
$ sudo !!
sudo apt update
Screen and process control
- Ctrl+L: clear the screen but keep what you have typed on the current line.
- Ctrl+C: interrupt the running program, or throw away the current line and get a fresh prompt.
- Ctrl+D: on an empty line, send "end of input", which logs you out of the shell. On a line with text, it deletes the character under the cursor.
- Ctrl+Z: suspend the running program. Bring it back with
fg; see the process management guide for jobs. - Tab: complete a command, file name or variable. Press it twice to list all possibilities.
set -o ignoreeof makes Bash ignore it (type exit instead).Editing long commands in your editor
For a long pipeline or a multi-line loop, the command line gets cramped. Press Ctrl+X then Ctrl+E, and Bash opens the current line in your editor. Edit it, save and quit, and Bash runs the result. It uses the editor in $VISUAL or $EDITOR:
export EDITOR=nano
Put that in ~/.bashrc to make it permanent. To cancel, delete everything in the editor before saving, and nothing runs.
Customizing with ~/.inputrc
Readline reads its settings from ~/.inputrc. Two popular additions: make the up and down arrows search history for lines that start with what you have already typed, and make tab completion ignore case.
$include /etc/inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
set completion-ignore-case on
The first line keeps your system defaults. Open a new terminal (or press Ctrl+X Ctrl+R to reload). To see every current binding, run bind -P. If you prefer vi-style editing, set -o vi switches modes, though the shortcuts in this guide then no longer apply. The Command Line Editing chapter of the Bash manual lists every command.
Quick reference
| Shortcut | Action |
|---|---|
| Ctrl+A / Ctrl+E | Start / end of line |
| Alt+B / Alt+F | Back / forward one word |
| Ctrl+U | Cut to start of line |
| Ctrl+K | Cut to end of line |
| Ctrl+W | Cut previous word |
| Alt+D | Cut next word |
| Ctrl+Y | Paste last cut text |
| Ctrl+_ | Undo |
| Ctrl+R | Search history backward |
| Ctrl+G | Cancel search |
| Alt+. | Insert last argument of previous command |
!! / !$ | Previous command / its last argument |
| Ctrl+L | Clear screen |
| Ctrl+C | Interrupt / discard line |
| Ctrl+D | Delete character, or exit on empty line |
| Ctrl+Z | Suspend running program |
| Ctrl+X Ctrl+E | Edit command in your editor |