I’ve finally gotten around to setting this up, and I tweaked it a bit more:
function precmd() {
previous_status=$?
if [[ -n "$preexec_waiting" ]]; then
echo -E "$preexec_time $(date +%Y-%m-%dT%H:%M:%S) : $previous_status : $$ : $preexec_pwd : $(history -n -1)" \
>> ~/.full_history
preexec_waiting=''
fi
}
function preexec() {
# $1, $2 and $3 all have variants of the command to be executed, but none of
# them is what I want. $1 and $3 can contain newlines. $2 and $3 are
# alias-expanded. `history -1` in precmd is better.
preexec_time=$(date +%Y-%m-%dT%H:%M:%S)
preexec_pwd=$(print -Pn %~)
preexec_waiting=1
}
Differences from Mike’s version:
You get both time of start and time of finish for every command (I’d prefer time of start and duration, but oh well)
You also get exit status
Correct pwd in case the command is cd or similar, and pwd uses ~ for concision
The pid makes it easier to separate commands from two different shell instances
If you type a command with a newline, it gets saved as \n instead of getting split over multiple lines in the file
I think the : separators make it a bit easier to read
Doesn’t include the number of the history entry (e.g.history -1 for me is 5785 cat ~/.full_history with a space before 5785 and two spaces after, where history -n -1 would be just cat ~/.full_history)
I think I’d rather have this in a sqlite db than a text file, but that would be more effort.
I’ve finally gotten around to setting this up, and I tweaked it a bit more:
Differences from Mike’s version:
You get both time of start and time of finish for every command (I’d prefer time of start and duration, but oh well)
You also get exit status
Correct pwd in case the command is
cd
or similar, and pwd uses ~ for concisionThe pid makes it easier to separate commands from two different shell instances
If you type a command with a newline, it gets saved as
\n
instead of getting split over multiple lines in the fileI think the
:
separators make it a bit easier to readDoesn’t include the number of the history entry (e.g.
history -1
for me is5785 cat ~/.full_history
with a space before 5785 and two spaces after, wherehistory -n -1
would be justcat ~/.full_history
)I think I’d rather have this in a sqlite db than a text file, but that would be more effort.
(edit: made slight improvements. https://github.com/larkery/zsh-histdb is probably worth looking into for sqlite history.)