TExcelTAD
Home › Terminal & Command Line › History, Completion & Shortcuts

History, Completion & Shortcuts

Fast terminal users don't type faster, they type less. Three features do most of the work in every shell:

  1. History: every command you run is remembered. Recall, edit and rerun instead of retyping.
  2. Tab completion: type the first few letters of a command, file or option and press Tab.
  3. Line editing shortcuts: jump to the start of the line, delete a word, cancel a command.

Who provides them differs: in CMD it is the Windows console itself (with doskey), in PowerShell the PSReadLine module, in bash the Readline library and in zsh its own line editor, ZLE. The keys that work everywhere are worth learning first.

The keys that work (almost) everywhere

Key Does
↑ / ↓ previous / next command from history
Tab complete a command, path or option
Ctrl+C cancel the running command, or abandon the line you are typing
Home / End start / end of the line
Ctrl+← / Ctrl+→ jump a word left / right (on macOS: Option+arrow)
Ctrl+L clear the screen (CMD: type cls)

Tip: Ctrl+C copies in Windows Terminal only when text is selected; otherwise it stops the command. On macOS, copy is Cmd+C, so Ctrl+C always means "stop".

Windows (CMD)

CMD's history lives in the console window and is lost when you close it.

Key / command Does
F7 pop-up list of this window's history; pick one with the arrows
F8 cycle through earlier commands that start with what you typed
F3 retype the previous command
Tab cycle through matching files and folders (Shift+Tab goes back)
Esc clear the current line
doskey /history print the history
cmd
C:\Users\ada> doskey /history
cd projects
dir /b
git status
doskey /history

(sample output). CMD does not complete command options or program arguments, only file and folder names.

Windows (PowerShell)

PowerShell 7 ships PSReadLine, which adds a saved, searchable history (kept across sessions in a text file) and predictions shown in grey as you type.

Key Does
Ctrl+R search history backwards; press again for older matches
→ accept the grey prediction
F2 switch predictions between inline and a list view
Tab complete cmdlets, parameters, paths, even property names
Ctrl+Space show all completions as a menu
Ctrl+Backspace delete the word before the cursor

The completions Tab would offer can also be listed from a script, which shows how much PowerShell knows about its own commands:

powershell · try it yourself
(TabExpansion2 'Get-ChildI' 10).CompletionMatches.CompletionText
(TabExpansion2 'Get-ChildItem -Rec' 18).CompletionMatches.CompletionText
(TabExpansion2 'Sort-Object -Desc' 17).CompletionMatches.CompletionText
Get-ChildItem
-Recurse
-Descending

Useful history commands: Get-History (this session), Invoke-History 3 (r 3) and (Get-PSReadLineOption).HistorySavePath, the file that holds your history across sessions.

Linux & macOS (bash/zsh)

bash (Readline) and zsh (ZLE) both use Emacs-style keys by default:

Key Does
Ctrl+A / Ctrl+E start / end of line
Ctrl+U delete to the start of the line (zsh: the whole line)
Ctrl+K delete to the end of the line
Ctrl+W delete the word before the cursor
Ctrl+R search history backwards
Alt+. insert the last argument of the previous command
Tab Tab bash: list all completions; zsh: a menu you move through with Tab
Ctrl+D on an empty line: exit the shell

History expansion is the other speed trick: !! is the whole previous command (sudo !! reruns it as root), !$ its last argument, !git the most recent command starting with git.

bash
ada@ubuntu:~$ mkdir -p projects/site
ada@ubuntu:~$ cd !$
cd projects/site
ada@ubuntu:~/projects/site$ apt update
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
ada@ubuntu:~/projects/site$ sudo !!
sudo apt update

Completion in bash is programmable: a completion script tells bash which words fit after a command. compgen shows what Tab would offer:

bash · try it yourself
compgen -W "start stop status restart" -- st
echo "---"
mkdir -p demo && cd demo && touch report.txt readme.md run.sh
compgen -f -- re | sort
echo "---"
compgen -b -- hi
start
stop
status
---
readme.md
report.txt
---
history

history lists the saved commands with numbers; !42 reruns number 42. bash saves to ~/.bash_history when the shell exits, zsh to the file named by HISTFILE (on macOS, ~/.zsh_history). Useful settings for ~/.bashrc:

bash
HISTSIZE=10000              # commands kept in memory
HISTFILESIZE=20000          # lines kept in ~/.bash_history
HISTCONTROL=ignoreboth      # skip duplicates and lines starting with a space
shopt -s histappend         # append instead of overwriting when several terminals close

zsh users enable similar behaviour with setopt SHARE_HISTORY HIST_IGNORE_DUPS in ~/.zshrc. Starting a command with a space (with ignoreboth, or setopt HIST_IGNORE_SPACE in zsh) keeps secrets like tokens out of history.

Comparison

Task CMD PowerShell bash zsh
Search history F8 (prefix) Ctrl+R Ctrl+R Ctrl+R
List history doskey /history Get-History history history
Rerun last command F3 ↑ Enter !! !!
History survives closing? no yes yes yes (if HISTFILE set)
Completes options? no yes with completion scripts yes, rich by default

Learn more: Aliases & Profiles, bashrc & Shell Config, zsh Essentials.

Test yourself

Which key searches your command history backwards in PowerShell, bash and zsh?

  1. Ctrl+F
  2. Ctrl+R
  3. F7
  4. Ctrl+H

In bash, what does sudo !! do?

  1. Runs sudo twice
  2. Reruns the previous command with sudo
  3. Opens the root shell history
  4. Repeats the last argument

Which CMD key shows a pop-up list of the commands typed in the current window?

  1. F3
  2. F7
  3. F8
  4. Tab