1.1 View History Commands
1.1.1 Basic Viewing
- View all
- Command line:
history - Shortcut to view all:
Command+Shift+H
- Command line:
- Fuzzy matching
- Command line:
history | grep python - Shortcut:
Ctrl+R
- Command line:
1.2 Using History Completion in iTerm
1.2.1 Built-in Shortcut Command + ;
- Enter command prefix:
- In iTerm's command line, enter a command prefix, for example,
gcloud
- In iTerm's command line, enter a command prefix, for example,
- Press shortcut:
Command+;, iTerm will pop up a window listing all history commands starting with your entered prefix in the current session.- Example:

- Example:
- Select and complete:
- Choose the command you need from the popup list, and it will auto-complete. Then you can modify it yourself.
Downside: Display is not complete
Note:
To solve the cross-session history saving issue with Command + ; completion in iTerm2 and ensure Shell correctly saves history:
vi ~/.zshrc edit the file and add at the end:
# Set history file size and save location
HISTFILE=~/.zsh_history # History file path
HISTSIZE=10000 # Number of history entries saved in memory
SAVEHIST=10000 # Number of entries saved in history file
# Append history (instead of overwriting)
setopt appendhistory # Merge history from multiple sessions
setopt inc_append_history # Append history in real-time (no need to restart session)
setopt share_history # Share history across sessions
setopt extended_history # Record timestamps
Apply changes: source ~/.zshrc
1.2.2 Advanced Completion Feature
If you want to achieve the following effect, showing gray suggestions while typing:

Configuration Method
Using zsh and oh-my-zsh:
- Install oh-my-zsh (if not already installed): Run
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)". - Install zsh-autosuggestions plugin:
- Clone the plugin to oh-my-zsh's plugin directory:
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions - Edit .zshrc file, add zsh-autosuggestions to the plugin list:
plugins=(zsh-autosuggestions)(add a new line) - Save and restart iTerm2, or run
source ~/.zshrcto apply the configuration.
...
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)
plugins=(zsh-autosuggestions)
...
Usage Method
Similar to before, enter a prefix, and matching history commands will automatically appear in gray. If you accept it, press -> arrow key. If not, use up/down arrows to navigate.