Shell History Is Your Best Productivity Tool

If you work in shell/terminal often enough, then over time the history will become your personal knowledge vault, documentation and command reference. Being able to use this personal documentation efficiently can hugely boost your productivity. So, here are a couple of tips on how to optimize your shell history configuration and usage to get the most out of it.

Configuration

I use ZSH and Oh My Zsh, so tips in this article will focus on ZSH, but similar results can be achieved for Bash or other shells.

If you're using Bash and would like to switch to ZSH, or try out the examples shown below, then check out this article.

With that out of the way, let's start with configuration options that will help us get the most out of the saved history:


# ~/.zshrc
# ...

HISTFILE="$HOME/.zsh_history"
HISTSIZE=10000000
SAVEHIST=10000000

HISTORY_IGNORE="(ls|cd|pwd|exit|cd)*"

We can't search and use history efficiently, unless we actually save/keep the data. The above config makes it so that we store pretty much infinite amount of command history. It also makes it uses HISTORY_IGNORE to list commands like ls, cd etc. that won't be stored in history, you can adjust this ignore list to your liking.

To further optimize the configuration, we can add following ZSH options:


# ~/.zshrc
# https://zsh.sourceforge.io/Doc/Release/Options.html (16.2.4 History)

setopt EXTENDED_HISTORY      # Write the history file in the ':start:elapsed;command' format.
setopt INC_APPEND_HISTORY    # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY         # Share history between all sessions.
setopt HIST_IGNORE_DUPS      # Do not record an event that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS  # Delete an old recorded event if a new event is a duplicate.
setopt HIST_IGNORE_SPACE     # Do not record an event starting with a space.
setopt HIST_SAVE_NO_DUPS     # Do not write a duplicate event to the history file.
setopt HIST_VERIFY           # Do not execute immediately upon history expansion.
setopt APPEND_HISTORY        # append to history file (Default)
setopt HIST_NO_STORE         # Don't store history commands
setopt HIST_REDUCE_BLANKS    # Remove superfluous blanks from each command line being added to the history.

The above mainly configures ZSH to ignore certain commands to avoid cluttering history, e.g. duplicates or history commands. It also adds timestamps to each command which will be handy when searching (see next section).

One particular option I would highlight here is HIST_IGNORE_SPACE which can be useful if you want to avoid storing secrets in history - simply prefix any command that includes a secret/password with space, e.g. export AWS_ACCESS_KEY_ID=... (notice the space before export) and it won't appear in history.

The previous options configured how the history is stored, now let's talk about configuring how the history is searched and presented.

By default, you can only search by exact match, but ZSH includes a plugin, that makes it possible to search using fuzzy-search. To enable it, we just need to add the plugin:


# ~/.zshrc
# ...

plugins=(git fzf)

With that, when you search history, ZSH will automatically use FZF for fuzzy search. This however might use find command in the background which isn't very fast. So, to improve performance (for very large history files) I recommend using ag:


sudo apt-get install silversearcher-ag

# add to # ~/.zshrc
export FZF_DEFAULT_COMMAND='ag --hidden -g ""'

And last configuration option I want to mention is history timestamp formatting - by default when you view history, you will get output like:


10206  echo hello
10207  head some-file.txt
10208  curl google.com
10209  cat some-file.txt

That is - line number from history file and the command itself. However, by adding HIST_STAMPS="yyyy-mm-dd" to ~/.zshrc, we can get the following:


10206  2024-03-30 12:29  echo hello
10207  2024-03-30 12:51  head some-file.txt
10208  2024-03-30 13:21  curl google.com
10209  2024-03-30 14:15  cat some-file.txt

Searching

Now that we've configured everything, let's see how we can efficiently search all the history.

The simplest way - without keybindings - is to use the history command. However, just using history will dump whole history file which isn't practical, instead we can use:


\history -E -10

10206  30.3.2024 12:29  echo hello
10207  30.3.2024 12:51  head some-file.txt
10208  30.3.2024 13:21  curl google.com
10209  30.3.2024 14:15  cat some-file.txt
...

\history -i

10206  2024-03-30 12:29  echo hello
10207  2024-03-30 12:51  head some-file.txt
10208  2024-03-30 13:21  curl google.com
10209  2024-03-30 14:15  cat some-file.txt
...

fc -E -l -10

10206  30.3.2024 12:29  echo hello
10207  30.3.2024 12:51  head some-file.txt
10208  30.3.2024 13:21  curl google.com
10209  30.3.2024 14:15  cat some-file.txt
...

We can use the -<N> flag to specify number of lines for both history and fc command. Additionally, the -E and -i will give different date formats.

fc - without arguments - can be also be used if you want to edit and re-execute the last command - it opens the default editor, where you can edit the command, and when you close it, the command gets executed.

Another useful command is r, which re-executes the last command without editing (like !!), and r <WORD>, which executes the last command that contains the string WORD.

Most of the time, though, we want to search history with keybind - in ZSH, the default CTRL+R will give you the context-based history scrolling (the history widget):

ZSH FZF Demo

As you can see that also includes the FZF fuzzy search.

If - for whatever reason - this doesn't work on your machine check the keybindings:


bindkey | grep fzf-history-widget

"^R" fzf-history-widget  # ^R is CTRL+R

If you prefer the classic reverse search, instead of context-based history scrolling, then you can bind the history-incremental-search-backward and history-incremental-search-forward events:


# Add to ~/.zshrc
bindkey "^E" history-incremental-search-backward  # CTRL+E
bindkey "^S" history-incremental-search-forward   # CTRL+S

You can also simply start typing a command, and press up/down arrow to search through history, taking into account what's typed on commandline:


bindkey "^[[A" up-line-or-beginning-search    # Arrow up
bindkey "^[[B" down-line-or-beginning-search  # Arrow down
ZSH bck-i-search Demo

There are also more variations in docs, e.g.:


history-beginning-search-backward
history-beginning-search-backward-end
history-search-backward
...

Be aware, that the keycodes may differ based on your OS, e.g. up arrow can be \e[A, ^[OA or ^[[A on different systems.

As you probably noticed from the above GIF, you can have a syntax highlighting for this classic reverse-search, just install zsh-syntax-highlighting with:


sudo apt-get install zsh-syntax-highlighting
echo "source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc

Synchronization

To take it to the next level, and to have your shell history always with you on different workstations, you can additionally use ZSH history-sync plugin, which uses Git to sync history across workstations - see plugin docs for explanation of how it works and how to set it up.

A more modern alternative to the above sync functionality is atuin.sh, which uses SQLite as a storage and supports ZSH - see the website for features and details.

Conclusion

At this point, I'm pretty sure, that if I lost my .zsh_history I probably wouldn't be able to do my job anymore. Most of the commands I run are really just CTRL+R plus minor edits.

So, with that said, hopefully, with the above tips and configs your shell history can become your personal knowledge vault and can help you be a bit more efficient in shell.

Note: My complete ~/.zshrc (stripped of comments) can be found in following gist.

Subscribe: