Which command did you run 1731 days in the past?
Professor Henry Jones: Properly, he who finds the Grail should face the ultimate problem.
Indiana Jones: What last problem?
Professor Henry Jones: Three units of such deadly crafty.
Indiana Jones: Booby traps?
Professor Henry Jones: Oh, sure. However I discovered the clues that can safely take us by means of them within the Chronicles of St. Anselm.
Indiana Jones: [pleased] Properly, what are they?
[short pause as Henry tries to recall]Indiana Jones: Can’t you keep in mind?
Professor Henry Jones: I wrote them down in my diary in order that I wouldn’t have to recollect.
***
Recipe for residing life within the shell:
-
Make sure that it’s quick.
-
Make sure that its historical past can develop almost infinitely and you may fuzzy-search by means of it.
The primary one–quick shells–we talked about last week, so this time, let’s speak about shell historical past.
On considered one of my machines the ~/.zsh_history
file comprises 26278 instructions. Its oldest entry was recorded on April 25, 2019. After I hit Ctrl-r
in ZSH fzf pops up and lets me fuzzy-search by means of all 26278 instructions, by means of the final 5 years of my shell historical past. It’s superb.
That one curl
command with these 4 unusual HTTP headers that I needed to ship to verify I may speak to my native dev env accurately by means of the reverse-proxy – that’s in there, a couple of keystrokes and a few hazy recollections away. That one rsync
that makes use of the proper SSH shopper binary on the opposite facet and that preserves timestamps and permission – additionally in there. The ugly bastard of a command that mixed git grep
with discover
and eight ugly incantations of AWK chained collectively – locked in there too. The check command I ran yesterday is as reachable because the one command with 12 &&
in it that I ran 3 years in the past, all due to a protracted shell historical past file.
Actually: ~/.zsh_history
is among the most vital recordsdata on my machine. I make use of it tens of occasions on daily basis, every time I hit Ctrl-r
. If I wasn’t such a doofus it could include much more than 26278 instructions, as a result of I’ve been preserving a protracted shell historical past for almost a decade. However since I am such a doofus, I forgot to again it up (or deleted the backup too early) a couple of occasions and therefore the ~/.zsh_history
on the machine I’m typing this solely comprises 1034 instructions.
Somebody may say: “effectively, if the command was that vital, why don’t you reserve it to a script?” And to that I say: look, man, if I knew prematurely which command would develop into vital, then we wouldn’t be right here, as a result of I’d have already got ascended to the astral realm.
Additionally: why would I file it in a script if I can simply configure my shell to maintain observe of all of these instructions for me? It’s low-cost, it’s quick, there’s basically no downsides to it: the most important ~/.zsh_history
I’ve is 2MB and I can fuzzy-search by means of it in milliseconds.
***
So, to repeat: your shell – ZSH, Bash, Fish, or others – can file each command you execute in it and I’m begging you to verify it’s configured to do this.
Make sure that it retains observe of lots of instructions. So many instructions that once you put the quantity in your rc-file you assume: that’s ridiculous, why would I have to file that many instructions? Double that quantity. As the following and last step you should get a fuzzy-finder fuzzy-finder to look by means of it.
Right here’s the ZSH configuration I used for a lot of, a few years:
##########
# HISTORY
##########
HISTFILE=$HOME/.zsh_history
HISTSIZE=50000
SAVEHIST=50000
# Instantly append to historical past file:
setopt INC_APPEND_HISTORY
# Document timestamp in historical past:
setopt EXTENDED_HISTORY
# Expire duplicate entries first when trimming historical past:
setopt HIST_EXPIRE_DUPS_FIRST
# Dont file an entry that was simply recorded once more:
setopt HIST_IGNORE_DUPS
# Delete outdated recorded entry if new entry is a replica:
setopt HIST_IGNORE_ALL_DUPS
# Don't show a line beforehand discovered:
setopt HIST_FIND_NO_DUPS
# Dont file an entry beginning with an area:
setopt HIST_IGNORE_SPACE
# Dont write duplicate entries within the historical past file:
setopt HIST_SAVE_NO_DUPS
# Share historical past between all classes:
setopt SHARE_HISTORY
# Execute instructions utilizing historical past (e.g.: utilizing !$) immediatel:
unsetopt HIST_VERIFY
That was mixed with fzf to fuzzy-search by means of the historical past on ctrl-r
.
Configure your shell roughly like that and there you might be: residing the great shell life.
Or you should utilize Atuin, which I’ve been making an attempt out for the previous couple of weeks and have come to like. It’s a drop-in enhancement of your shell’s historical past performance and never solely provides you every part I described above – extremely lengthy shell historical past and fuzzy-search by means of it – but additionally syncing and backup (yup, want that) of shell historical past throughout a number of machines, filtering instructions by machine, working-directory-specific historical past and doubtless 13 different issues I forgot. It’s very good. Extremely advocate it.
No matter you utilize: make certain your shell historical past is recorded, make certain your shell historical past can develop ridiculously lengthy, and get a fuzzy-search for it.