Maximizing Productivity with Shell History: Tips and Configurations #809
Labels
CLI-UX
Command Line Interface user experience and best practices
linux
Linux notes tools links
shell-script
shell scripting in Bash, ZSH, POSIX etc
software-engineering
Best practice for software engineering
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 an infinite amount of command history. It also uses
HISTORY_IGNORE
to list commands likels
,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 the following ZSH options:
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 beforeexport
) 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:
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 usingag
:And the last configuration option I want to mention is history timestamp formatting - by default when you view history, you will get output like:
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: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 usinghistory
will dump the whole history file which isn't practical, instead we can use:We can use the
-<N>
flag to specify the number of lines for bothhistory
andfc
command. Additionally, the-E
and-i
will give different date formats.fc
- without arguments - can 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!!
), andr <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):
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:
If you prefer the classic reverse search, instead of context-based history scrolling, then you can bind the
history-incremental-search-backward
andhistory-incremental-search-forward
events:You can also simply start typing a command, and press up/down arrow to search through history, taking into account what's typed on the command line:
There are also more variations in docs, e.g.:
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 syntax highlighting for this classic reverse-search, just install
zsh-syntax-highlighting
with: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 the following gist.Copyright © 2023 Martin Heinz
The text was updated successfully, but these errors were encountered: