# Terminal Shortcuts Cheat Sheet: Bash, Zsh and tmux

Source: https://srvscripts.com/guides/terminal-cheat-sheet/
Updated: 2026-10-09
Publisher: srvScripts (https://srvscripts.com/)

**Short answer:** The shortcuts that save the most time in Bash and Zsh are Ctrl+A and Ctrl+E to jump along the line, and Ctrl+U, Ctrl+K and Ctrl+W to delete. Ctrl+R searches history, Alt+. inserts the last argument and `!!` repeats the last command. For jobs, Ctrl+Z suspends and `bg` resumes in the background. Learn redirection (`2>&1`, `<<<`, `<(...)`), brace expansion and `tmux` for sessions that survive a dropped SSH connection.

The examples in this terminal cheat sheet were run with GNU Bash 5.2.21 and tmux 3.4 on 9 October 2026. Readline bindings were read from `bind -p`, and the tmux keys from `tmux list-keys`. Zsh, screen and the terminal apps were not available for testing, so those rows were checked against their documentation.

## Editing the command line

Bash uses the GNU Readline library in emacs mode by default; Zsh’s line editor uses the same keys. Alt is the Meta key: if Alt does nothing on a Mac, see the terminal app section below.

| Keys | Action |
| --- | --- |
| Ctrl+A / Ctrl+E | Start / end of the line |
| Alt+B / Alt+F | Back / forward one word |
| Ctrl+U | Delete from the cursor to the start of the line |
| Ctrl+K | Delete from the cursor to the end of the line |
| Ctrl+W / Alt+D | Delete the word before / after the cursor |
| Ctrl+Y / Alt+Y | Paste the last deleted text / cycle through older deletions |
| Ctrl+_ (or Ctrl+X Ctrl+U) | Undo |
| Ctrl+T / Alt+T | Swap two characters / two words |
| Alt+U / Alt+L / Alt+C | Uppercase / lowercase / capitalise the next word |
| Ctrl+L | Clear the screen, keep the current line |
| Ctrl+X Ctrl+E | Open the line in $VISUAL or $EDITOR; it runs when you save and quit |
| Alt+# | Comment out the line and save it to history without running it |
| Tab, Tab Tab | Complete; list the possibilities |
| set -o vi / set -o emacs | Switch to vi or back to emacs keys. bind -p lists every binding |

## History search and reuse

| Keys or command | Action |
| --- | --- |
| Ctrl+R | Search history backwards as you type; Ctrl+R again for older matches |
| Ctrl+S | Search forwards. Most terminals grab it for flow control, so run stty -ixon first |
| Ctrl+G | Leave the search without running anything |
| Ctrl+P / Ctrl+N (or Up / Down) | Previous / next command |
| Alt+. (or Alt+_) | Insert the last argument of the previous command; repeat to go further back |
| Ctrl+O | Run the current history line and load the next one; good for replaying a sequence |
| history 20 | Last 20 commands with numbers |
| HISTCONTROL=ignoreboth | Skip duplicates and lines starting with a space |
| HISTTIMEFORMAT='%F %T ' | Show a date and time in history output |
| shopt -s histappend | Append to ~/.bash_history instead of overwriting it when several shells exit |

With `HISTCONTROL=ignoreboth` set, a command typed with a leading space, such as ` mysql -p'secret'`, is not saved to history. It still shows in `ps` while it runs, so keep passwords in option files.

## History expansion

| Syntax | Expands to |
| --- | --- |
| !! | The whole previous command |
| !$ / !^ | Last / first argument of the previous command |
| !:2, !* | Second argument; all arguments |
| !42, !-2 | Command number 42; the command two back |
| !ssh | The most recent command starting with ssh |
| !ssh:p | Print that command without running it (it is added to history, so !! then runs it) |
| ^gamma^delta | Rerun the previous command with the first gamma replaced by delta |
| !!:gs/alpha/omega/ | Rerun with every alpha replaced |

```
$ cp /etc/nginx/nginx.conf /root/nginx.conf.bak
$ ls -l !$
ls -l /root/nginx.conf.bak
```

`sudo !!` repeats the whole previous line with `sudo` in front, but only the first command of a list gets root. On our test, `ls -d /tmp && echo ok` became `sudo ls -d /tmp && echo ok`, where `echo` ran as the normal user. History expansion works only in interactive shells, not in scripts.

## Job control and signals

| Keys or command | Action |
| --- | --- |
| Ctrl+C | Send SIGINT: interrupt the foreground program |
| Ctrl+Z | Send SIGTSTP: suspend it and get the prompt back |
| Ctrl+D | End of input. On an empty prompt it logs you out |
| command & | Start in the background; $! holds its PID |
| jobs -l | Background and stopped jobs with PIDs |
| fg %1 / bg %1 | Bring job 1 to the foreground / resume it in the background |
| wait | Wait for all background jobs to finish |
| disown %1 | Remove a job from the shell’s table so it is not sent SIGHUP when you log out |
| nohup long-task > task.log 2>&1 & | Start a job that ignores hang-ups from the start |

For server admins: to leave a long job running after you log out of SSH, press Ctrl+Z, then `bg`, then `disown`. For planned work, start it inside tmux or screen.

## Redirection and pipes

| Syntax | What it does |
| --- | --- |
| cmd > file / cmd >> file | Write stdout to a file / append |
| cmd 2> err.log | Write stderr to a file |
| cmd > all.log 2>&1 | Both streams to one file. Order matters: 2>&1 > file sends stderr to the terminal |
| cmd &> all.log | Bash and Zsh shorthand for the line above |
| cmd |& less | Pipe stdout and stderr (same as 2>&1 |) |
| set -o noclobber and >| | Refuse to overwrite files with >; >| overrides it once |
| cmd
