How to View Past Terminal Commands — from Simple to Robust
Suppose you want to re-run some shell command you used ten days ago. It is a complex one; you do not remember exact flags and argument values, and it would take a long time to recall an exact text. What can you do?
1. The upwards arrow
Majority of devs working with command line knows it. Press “up” to see a previous command, press “down” for a next command, press “Ctrl+C” to drop whatever is in the prompt and start fresh.
This approach works, but gets very tedious when you need to find a command you used last week or last month. Once more than ten or twenty commands have passed, scrolling through them becomes tedious.
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 search more flexibly.
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)
So, if your command was run a really long time ago, history may not find it, but direct inspection of .bash_history can do.
4. fc -l command
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 (see 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 (press “Ctrl+Q” to resume). To make it work for reverse-i-search, add stty -ixon to your shell config. It will disable “Ctrl+S” / “Ctrl+Q” shortcuts for terminal flow control:
echo "stty -ixon" >> ~/.bashrc
source ~/.bashrcHappy command line manipulation!
💡 This post has a second part: Shell Configs for Better Command History Search