Can't comment yet (and this went beyond a simple correction), so I'll add this as an answer.
This correction to the accepted answer doesn't quite work when, for example, the last command took quite a bit of time to execute - you'll get stray numbers and ;
in your command, like this:
2017-07-22 19:02:42 | 3;micro ~/.zshrc && . ~/.zshrc
This can be fixed by replacing the sed -re '1s/.{15}//'
in command_part
with a slightly longer gawk
, which also avoids us a pipeline:
local command_part="$(gawk "
NR == $line_num_last {
pivot = match(\$0, \";\");
print substr(\$0, pivot+1);
}
NR > $line_num_last {
print;
}" ~/.zsh_history)"
It also has problems when dealing with multiline commands where one of the lines begin with :
. This can be (mostly) fixed by replacing grep -ane '^:' ~/.zsh_history
in line_num_last
with grep -anE '^: [0-9]{10}:[0-9]*?;' ~/.zsh_history
- I say mostly because a command could conceivably contain a string matching that expression. Say,
% naughty "multiline
> command
> : 0123456789:123;but a command I'm not
> "
Which will result in a clobbered record in ~/.persistent_history
.
To fix this we need, in turn, to check whether the previous redord ends with \
(there might be other conditions but I'm not familiar yet with this history format), and if so try the previous match.
_get_line_num_last () {
local attempts=0
local line=0
while true; do
# Greps the last two lines that can be considered history records
local lines="$(grep -anE '^: [0-9]{10}:[0-9]*?;' ~/.zsh_history | \
tail -n $((2 + attempts)) | head -2)"
local previous_line="$(echo "$lines" | head -1)"
# Gets the line number of the line being tested
local line_attempt=$(echo "$lines" | tail -1 | cut -d':' -f1 | tr -d '\n')
# If the previous (possible) history records ends with `\`, then the
# _current_ one is part of a multiline command; try again.
# Probably. Unless it was in turn in the middle of a multi-line
# command. And that's why the last line should be saved.
if [[ $line_attempt -ne $HISTORY_LAST_LINE ]] && \
[[ $previous_line == *"\\" ]] && [[ $attempts -eq 0 ]];
then
((attempts+=1))
else
line=$line_attempt
break
fi
done
echo "$line"
}
precmd() {
local line_num_last="$(_get_line_num_last)"
local date_part="$(gawk "NR == $line_num_last {print;}" ~/.zsh_history | cut -c 3-12)"
local fmt_date="$(date -d @${date_part} +'%Y-%m-%d %H:%M:%S')"
# I use awk itself to split the _first_ line only at the first `;`
local command_part="$(gawk "
NR == $line_num_last {
pivot = match(\$0, \";\");
print substr(\$0, pivot+1);
}
NR > $line_num_last {
print;
}" ~/.zsh_history)"
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
echo "${fmt_date} | ${command_part}" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
export HISTORY_LAST_LINE=$((1 + $(wc -l < ~/.zsh_history)))
fi
}
PROMPT_COMMAND
. Replacing the[[
usage should be doable withgrep -o
orcut
or similar but depends on the exact output ofhistory
in zsh.PROMPT_COMMAND
, the link should be helpful. For[[
part, I just found with the commandhistory
, bash will give the newest one (in this case, it'shistory
) at the last line. But under Zsh,history
command won't return the newest one, it returns the command used beforehistory
at the last line. Any ideas? :-)HISTFILE
and setHISTSIZE
andSAVEHIST
to some ridiculously large size (mine are 100,000, and I see no reason to make them larger, since I log all my terminal sessions anyway in iTerm2 — that's all commands + output, with time down to seconds in my prompt). The default history format has POSIX timestamps associated, which is more accurate than yours since yours don't have tzinfo.