Linux and Mac terminal keyboard shortcuts
The command line looks unfriendly until you learn a dozen keys. Then it becomes quick: you stop retyping commands, stop holding the arrow key to reach the start of a line, and know how to stop a program that has run away. These shortcuts work in Bash and Zsh, the shells used by Linux, by the Mac's Terminal, by Git Bash on Windows and by the Windows Subsystem for Linux.
They use the Ctrl key on every system, the Mac included.
The eight to learn first
- Tab completes the command or file name you have started typing. Press it twice to see the choices.
- Up arrow brings back the previous command.
- Ctrl + R searches your history: type a few letters and the last matching command appears.
- Ctrl + C stops the program that is running.
- Ctrl + A and Ctrl + E jump to the start and the end of the line.
- Ctrl + U clears everything you have typed on the line.
- Ctrl + L clears the screen.
- Ctrl + D closes the shell, the same as typing exit.
| Keys | What it does |
|---|---|
| Ctrl + A | Go to the start of the line |
| Ctrl + E | Go to the end of the line |
| Alt + B | Go back one word |
| Alt + F | Go forward one word |
| Ctrl + B or Left arrow | Go back one character |
| Ctrl + F or Right arrow | Go forward one character |
| Ctrl + X then Ctrl + X | Jump between the start of the line and where you were |
| Keys | What it does |
|---|---|
| Ctrl + U | Delete from the cursor back to the start of the line |
| Ctrl + K | Delete from the cursor to the end of the line |
| Ctrl + W | Delete the word before the cursor |
| Alt + D | Delete the word after the cursor |
| Ctrl + H or Backspace | Delete the character before the cursor |
| Ctrl + D or Delete | Delete the character under the cursor |
| Ctrl + Y | Paste back what was last deleted with Ctrl + U, Ctrl + K or Ctrl + W |
| Ctrl + _ | Undo the last change to the line |
| Ctrl + T | Swap the two characters before the cursor |
| Keys | What it does |
|---|---|
| Up arrow or Ctrl + P | Show the previous command |
| Down arrow or Ctrl + N | Show the next command |
| Ctrl + R | Search back through the history as you type |
| Ctrl + R again | Go to the next older match |
| Ctrl + G | Leave the history search without running anything |
| Alt + . | Insert the last word of the previous command |
| !! then Enter | Run the previous command again |
| Keys | What it does |
|---|---|
| Ctrl + C | Stop the running program |
| Ctrl + Z | Pause the running program and return to the prompt |
| Ctrl + D | Send "end of input"; at an empty prompt, close the shell |
| Ctrl + L | Clear the screen |
| Ctrl + S | Freeze the output on screen |
| Ctrl + Q | Unfreeze the output |
| Tab | Complete a command, file or folder name |
| Tab twice | List all the possible completions |
| Keys | What it does |
|---|---|
| Ctrl + Shift + C | Copy the selected text, in most Linux terminals |
| Ctrl + Shift + V | Paste, in most Linux terminals |
| Ctrl + Shift + T | Open a new tab, in most Linux terminals |
| Ctrl + Shift + W | Close the tab |
| Ctrl + Alt + T | Open a terminal, on Ubuntu and several other systems |
On the Mac, the window shortcuts use Command: Cmd + C, Cmd + V, Cmd + T and Cmd + W.
Ctrl + C does not copy here
In a terminal, Ctrl + C means "stop". It interrupts whatever is running. That is why Linux terminals use Ctrl + Shift + C and Ctrl + Shift + V for copy and paste, and why pressing Ctrl + C out of habit to copy can end a long job. On the Mac there is no clash, since copy is Cmd + C.
The screen has frozen
If the terminal stops showing anything you type, you have probably pressed Ctrl + S, which pauses the output. Nothing is broken. Press Ctrl + Q to release it.
Paused is not stopped
Ctrl + Z does not end a program. It suspends it, and the program waits in the background. Type fg to bring it back, or jobs to see what is waiting. To end a program, use Ctrl + C.
History search is the biggest time saver
Press Ctrl + R and type part of a command you ran earlier, such as "ssh" or a file name. The most recent match appears. Press Ctrl + R again for older ones, Enter to run the command, or the right arrow to put it on the line for editing. Long commands need typing only once.
Tab completion prevents typing mistakes
Type the first letters of a file or folder name and press Tab. If only one thing matches, the shell finishes the name for you, with the spelling and the capital letters right. If several match, a second Tab lists them. Experienced users press Tab constantly, as much to check that a file exists as to save typing.
The Alt key on a Mac
In the Mac's Terminal, the Option key does not act as Alt until you tick "Use Option as Meta key" in the Terminal settings, under Profiles, Keyboard. Without that, press Esc and then the letter: Esc then B goes back a word.
Where these come from
Most of these keys belong to a library called Readline, which Bash uses for editing the line. Their letters come from the Emacs editor: A for the start, E for end, K for kill. The same keys work in many other command-line programs, including the Python prompt. For the commands themselves, see our Commands site.
This is an independent reference site. It is not connected with Microsoft, Apple, Google or any other maker of the programs named here. Product names belong to their owners and are used only to say where a shortcut works. Programs change; if a shortcut does not work for you, check the program's own help.