How to View Past Terminal Commands — from Simple to Robust
Suppose you want to re-run a shell command you used ten days ago. It was complex one; you do not remember exact flags and arguments, so it is going take a long time to reconstruct an exact text. What can you do?
1. The upwards arrow
Majority of devs know it. Press “up” to see the previous command, press “down” for the next command, press “Ctrl+C” to drop whatever is in the prompt and start fresh.
This approach works, but gets tedious when you need to find a line you used last month. Once more than ten or twenty commands have passed, scrolling becomes no better than writing a command from scratch.
2. Terminal history file
All the commands you enter into a terminal get stored in .bash_history file (or .zsh_history if you are on Mac) up to a certain limit. Thus, you can run:
cat ~/.bash_history # output all into the terminal
less ~/.bash_history # or use any text viewer
tail -n 20 ~/.bash_history # or observe only the most recent n lines
cat ~/.bash_history | grep whatever # to search for specific patternsThis method gives you full access to your history file and lets you see context around commands of interest, plus now you can search for them.
3. history command
Almost the same as using the history file directly: you get a list commands, but now it is numbered.
history -20 # show the last 20 commands
history -500 | grep ssh # search for a specific patter in a command
!780 # execute command with order number 780But there is one important difference from direct usage of .bash_history:
- history command uses the last HISTSIZE history entries (default 1000)
- .bash_history file uses the last HISTFILESIZE entries (default 2000)
It means that if your command was run a really long time ago, history may not find it. The line though, is still persisted, so direct inspection of .bash_history will help.
4. fc -l
This command behaves very similar to history, with an additional ability to display ranges of command numbers::
fc -l -20 # show the last 20 commands
fc -l 100 150 # show commands 100 to 1505. Reverse-i-search
This is the most powerful approach. Press “Ctrl+R” to enter reverse incremental search mode. Initially you get no output; start writing any part of a command you remember, e. g. ssh or input.json or -n 10 — and you will see the first full command entry with that match!
From there, you can:
- Press “Enter” to execute the command immediately
- Use left/right arrow keys to move within a command to edit it, then press “Enter” to execute
- Press “Ctrl+R” again to go to the next, older match
- Press “Ctrl+S” to go to the previous, newer match (note a comment below)
- Press up-down arrows to view to nearby entries in history around the match
- Press “Ctrl+C” or “Ctrl+G” to exit the search
On many systems “Ctrl+S” shortcut will not work, as it is prioritized to pause terminal output (if that happens to you, press “Ctrl+Q” to resume). To make it work for reverse-i-search, add stty -ixon to your shell config. This incantation disables terminal flow control with “Ctrl+S” / “Ctrl+Q” shortcuts:
echo "stty -ixon" >> ~/.bashrc
source ~/.bashrcHappy command line manipulation!
💡 This post has a second part: Shell Configs for Better Command History Search