A handful of keyboard shortcuts makes the terminal dramatically faster: no more holding the arrow key to fix a typo at the start of a long command, or retyping commands you ran five minutes ago. These work in bash (and mostly in zsh) on any Linux server, over SSH too.
Moving the cursor
| Shortcut | Action |
|---|---|
| Ctrl+A | Start of line |
| Ctrl+E | End of line |
| Alt+B / Alt+F | Back / forward one word |
| Ctrl+B / Ctrl+F | Back / forward one character |
| Ctrl+XX | Toggle between start of line and current position |
On macOS terminals, enable “Use Option as Meta key” for the Alt shortcuts.
Editing
| Shortcut | Action |
|---|---|
| Ctrl+U | Delete from cursor to start of line |
| Ctrl+K | Delete from cursor to end of line |
| Ctrl+W | Delete the word before the cursor |
| Alt+D | Delete the word after the cursor |
| Ctrl+Y | Paste (“yank”) what you last deleted with Ctrl+U/K/W |
| Ctrl+T | Swap the two characters before the cursor |
| Alt+U / Alt+L | Uppercase / lowercase the word |
| Ctrl+_ | Undo |
| Ctrl+X Ctrl+E | Open the current command in your editor ($EDITOR) |
Ctrl+X Ctrl+E is great for long one-liners: edit comfortably in vim or nano, save, and the command runs.
History
| Shortcut / command | Action |
|---|---|
| Ctrl+R | Reverse search history (type part of a command; Ctrl+R again for older matches) |
| Ctrl+G | Cancel the search |
| ↑ / ↓ | Previous / next command |
| !! | Repeat the last command (sudo !! when you forgot sudo) |
| !$ or Alt+. | Last argument of the previous command |
| !nginx | Run the most recent command starting with “nginx” |
| ^old^new | Re-run the last command, replacing “old” with “new” |
history | grep docker |
Search full history |
Example:
mkdir -p /var/www/example.com/public
cd !$ # cd /var/www/example.com/public
Make history more useful in ~/.bashrc:
HISTSIZE=50000
HISTFILESIZE=100000
HISTCONTROL=ignoreboth # skip duplicates and commands starting with a space
HISTTIMEFORMAT='%F %T '
shopt -s histappend
Starting a command with a space keeps it out of history, which is useful when a password has to appear on the command line. See history command.
Process and job control
| Shortcut | Action |
|---|---|
| Ctrl+C | Interrupt (stop) the running command |
| Ctrl+Z | Suspend it (then bg to continue in the background, fg to bring it back) |
| Ctrl+D | End of input / log out of the shell |
| Ctrl+\ | Quit forcefully (SIGQUIT) |
jobs # list background/suspended jobs
fg %1 # bring job 1 to the foreground
bg %1 # continue job 1 in the background
For long jobs over SSH, use tmux or screen, so they survive disconnects. See tmux and kill a process.
Screen and terminal
| Shortcut | Action |
|---|---|
| Ctrl+L | Clear the screen (keeps the current line) |
| Ctrl+S | Freeze output (XOFF), often pressed by accident |
| Ctrl+Q | Unfreeze output |
| Shift+PgUp/PgDn | Scroll (depends on the terminal) |
Terminal frozen? Try Ctrl+Q first. If it’s garbled after printing binary data, type reset (blindly) and press Enter.
Stuck SSH session? Press Enter, then ~. to close the connection from the client side. See SSH keepalive.
Tab completion
- Tab completes commands, paths, and (with bash-completion installed) options for git, systemctl, apt, docker and more.
- Tab Tab lists all possibilities.
sudo apt install bash-completion
Quick reference
| Goal | Keys |
|---|---|
| Line start / end | Ctrl+A / Ctrl+E |
| Delete word / line part | Ctrl+W / Ctrl+U / Ctrl+K |
| Search history | Ctrl+R |
| Last argument | Alt+. or !$ |
| Repeat with sudo | sudo !! |
| Edit in editor | Ctrl+X Ctrl+E |
| Unfreeze | Ctrl+Q |
Related: alias command and Linux prompt colors.