The history command in the terminal is very useful. We can’t, and shouldn’t, need to remember all the commands we use, some of which are voodoo.
All software engineers use
$ history and as a front-end engineer I’ve relied on it heavily every step of my career. But the history command’s defaults are inadequate, and need to be manually fixed.
Here’s what’s wrong with the default history behavior:
- Saves few commands (only 500)
- Doesn’t collect commands of current open windows (annoying!)
- Rewrites bash history
The history command’s default behavior may have been defined for computers with little hard drive space, and computers which could only have one shell open at a time. But the history of history doesn’t matter. 😛 This is legacy behavior and it is wrong.
(If anyone wants to figure out why these legacy defaults persist, and advocate for updating them, please do. 🙏)
But on the bright side, the defaults are quickly and easily corrected. We simply update our .bash_profile to store more lines our ongoing session history list (HISTSIZE), as well as store more commands in our .bash_history file (HISTFILESIZE).
To fix history without much thought, you can paste the following into .bash_profile, replacing any duplicate lines:
export HISTCONTROL=ignoredups:erasedups # no duplicate entries export HISTSIZE=100000 # many commands in ongoing session memory export HISTFILESIZE=100000 # many lines in .bash_history shopt -s histappend # append to history, don't overwrite it
OR DO THIS (step-by-step version):
Add these lines to your .bash_profile file, using your favorite text editor (I’m using vi):
$ vi ~/.bash_profile
Search “HISTSIZE”, however you search, and if there is a line for HISTSIZE replace it with:
Do the same with HISTFILESIZE:
These settings can have the same value, or whatever you want, but make it sufficiently large. I’m using 100K. I’m not messing around. 🙂
To append your bash history without overwriting it add this line:
shopt -s histappend
And to avoid duplicate entries you can add
I hope I was able to help you fix your terminal history command as simply and quickly as possible. Fixing it is the kind of thing that is never urgent, and therefore it remains broken. So for your future self’s benefit, do it now! 🙌