By default most shells don’t log your history in a detailed durable
way, which gives up one of the big advantages of working on the
command line. Good history lets you look back at things you did
months or years ago, in a searchable and skimmable fashion, so you can
answer questions like “how did I generate this number?”, “what was
that trick I used?”, or “if I’m doing something similar now what
should I recall from last time?”
I was recommending this to folks at work, but they use
zsh. In zsh you can get a lot of the way
here by setting INC_APPEND_HISTORY (append to history
immediately instead of waiting for the shell to exit),
SAVEHIST=1000000000 (effectively don’t limit the history
size on disk), and EXTENDED_HISTORY (to store timestamps
with history entries). But you risk losing most of your history if you
ever accidentally invoke zsh without these set, and it
doesn’t write the directory you ran the command in (which is metadata
I reference a lot).
The two changes are that zsh uses precmd
instead of PROMPT_COMMAND, and that you need
history -1 instead of history 1.
You can still use the same histgrep command on both:
function histgrep {
local n_lines=10
if [[ “$1″ =~ ^[0-9]*$ ]]; then
n_lines=”$1″
shift
fi
grep “$@” ~/.full_history | tail -n “$n_lines”
}
Note that this doesn’t replace your shell’s built-in history tooling,
and I wouldn’t recommend turning that off. This just a very cheap
additional layer of logging with additional metadata and less risk of
accidental deletion.
Logging Shell History in Zsh
Link post
By default most shells don’t log your history in a detailed durable way, which gives up one of the big advantages of working on the command line. Good history lets you look back at things you did months or years ago, in a searchable and skimmable fashion, so you can answer questions like “how did I generate this number?”, “what was that trick I used?”, or “if I’m doing something similar now what should I recall from last time?”
I use
bash
as my shell and have long used:I was recommending this to folks at work, but they use
zsh
. Inzsh
you can get a lot of the way here by settingINC_APPEND_HISTORY
(append to history immediately instead of waiting for the shell to exit),SAVEHIST=1000000000
(effectively don’t limit the history size on disk), andEXTENDED_HISTORY
(to store timestamps with history entries). But you risk losing most of your history if you ever accidentally invokezsh
without these set, and it doesn’t write the directory you ran the command in (which is metadata I reference a lot).Mike tweaked my snippet to run in
zsh
:The two changes are that
zsh
usesprecmd
instead ofPROMPT_COMMAND
, and that you needhistory -1
instead ofhistory 1
.You can still use the same
histgrep
command on both:Note that this doesn’t replace your shell’s built-in history tooling, and I wouldn’t recommend turning that off. This just a very cheap additional layer of logging with additional metadata and less risk of accidental deletion.