I rely heavily on shell usage. For over 15 years, I was a devoted user of Bash until I discovered Zsh and made the switch. One thing that remained constant throughout my transition was my configuration to maintain an extensive commands history. This setup is replicated across all my systems. When you spend a lot of time coding, you often find yourself repeating commands or running variations of them. Remembering all these commands can be challenging, but with resources like Google and ChatGPT, it’s not always necessary. However, if you find yourself frequently searching for specific commands, it can be time-consuming and distracting. That’s where having parts of the command at your fingertips becomes invaluable. With a quick press of Ctrl
+r
and a little scanning, you can find what you need.
Let’s consider an example: Suppose you need to rename all *.md
files in the current directory to *.en.md
because you have to add multilingual translations to your 100 articles.
I recall that I used the rename
command for this task, but I don’t quite remember how. So, I press Ctrl
+r
, start typing rename
, and there it is:
rename 's/\.md$/\.en\.md/' *.md
It took me just a few seconds.
While Bash allows you to recall only the latest command and requires multiple Ctrl
+r
presses to eventually find what you need, you can use fzf
, which provides a fuzzy-find algorithm to search through the entire history. It allows you to find the desired command quickly using the ⬆️ and ⬇️ arrow keys. It’s even faster.
My extensive history configuration for Zsh
# Set the maximum number of lines to keep in the history file
export HISTSIZE=1000000
# Set the number of history entries to save to the history file
export SAVEHIST=2000000
# Records the timestamp along with the history entry
setopt EXTENDED_HISTORY
# Causes duplicates to be removed in the order they appear in the history file
setopt HIST_EXPIRE_DUPS_FIRST
# Prevents duplicate commands from being recorded in the history
setopt HIST_IGNORE_DUPS
# Deletes the old recorded entry if a new entry is a duplicate
setopt HIST_IGNORE_ALL_DUPS
# When searching the history, it won't display a previously found line
setopt HIST_FIND_NO_DUPS
# Commands starting with a space won't be recorded in the history
setopt HIST_IGNORE_SPACE
# Appends each new history line to the history file immediately
setopt INC_APPEND_HISTORY
# When saving the history to a file, duplicate entries won't be written
setopt HIST_SAVE_NO_DUPS
# Shares command history between all sessions
setopt SHARE_HISTORY
# Prevents commands from being immediately executed when retrieved from the history using event designators
unsetopt HIST_VERIFY
My extensive history configuration for Bash (when I still used it)
# Set the control behavior for the history file to ignore and erase duplicates
export HISTCONTROL=ignoredups:erasedups
# Append to the history file, don't overwrite it
shopt -s histappend
# Set the maximum number of lines to keep in the history file
export HISTSIZE=1000000
# Set the maximum number of lines in the history file to keep on disk
export HISTFILESIZE=2000000
Enhance performance with fzf
brew install fzf
/opt/homebrew/opt/fzf/install
By installing fzf
, your default Ctrl
+r
shortcut will be replaced with fzf
. You can also use other shortcuts like Alt
+c
or Ctrl
+t
. For tips on fzf
usage, check out this article .