Revisited history
Introducing a revisted version of the Bash history, with additional features.
Limitless history
By default, the bash history is truncated at a given size. Therefore, you might loose your old commands over time.
Why is it so? Well, I can think of a few reason: memory used to be expansive, and a longer history requires more time to handle. Both of them are (in my opinion) no longer relevant today.
Solutions can be found online to disable size restrictions. For example, as proposed here, simply add the following lines in your ~/.bashrc:
export HISTFILESIZE=
export HISTSIZE=
Timestamp
It is possible to include a time stamp with each history entry. Therefore, one can know when a given command was executed.
To enjoy it, simply add export HISTTIMEFORMAT="[%F %T] " to your ~/.bashrc config file.
Working directory
One could also benefit from being able to retrieve the working directory associated to each history entry. In other words, what was the working directory when a given command was executed. Example: the command cp data.out ./ requires to know what was the working directory, in order to know where is your data now!
Unfortunately, the GNU history library is not designed to include such information in an entry.
Nevertheless, I have designed a hack to enjoy such feature, without the need to rebuild the whole library! Simply add the following code into your ~/.bashrc:
# Store working directory with commands (based on a remark from Paul Clabaut, inspired by Dennis Williamson)
PROMPT_COMMAND='history -a;[ -n "$MY_HISTORY_PWD" ] && tail -n 1 "$HISTFILE"|grep -P -q -v "^#0 \x1b" && echo -e "#0 \033[0;36m$MY_HISTORY_PWD\033[0m" >> "$HISTFILE";export MY_HISTORY_PWD="$PWD"'";$PROMPT_COMMAND"
# Define new history command displaying also the working directory (inspired by JJoao and not2qubit)
rhistory () { (TMP_FILE="$(mktemp)";sed -z -u 's/\n#0 \x1b/ \x1b/g' "$HISTFILE" > "$TMP_FILE";history -c;history -r "$TMP_FILE";rm "$TMP_FILE";history); }
This will define a new history command that includes the working directory of each displayed entry: rhistory.
Disclaimer:
Please not that this is a hack, it may have bugs, but it is still better than nothing...
In its current version, there is a race condition that can happen when two commands are being executed at the same time, leading to a mixing/incorrect working directories. It is therefore not garanteed that the working directory stored is correct, but it should be the case most of the time.
Full script
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Store working directory with commands (based on a remark from Paul Clabaut, inspired by Dennis Williamson)
PROMPT_COMMAND='history -a;[ -n "$MY_HISTORY_PWD" ] && tail -n 1 "$HISTFILE"|grep -P -q -v "^#0 \x1b" && echo -e "#0 \033[0;36m$MY_HISTORY_PWD\033[0m" >> "$HISTFILE";export MY_HISTORY_PWD="$PWD"'";$PROMPT_COMMAND"
# Define new history command displaying also the working directory (inspired by JJoao and not2qubit)
rhistory () { (TMP_FILE="$(mktemp)";sed -z -u 's/\n#0 \x1b/ \x1b/g' "$HISTFILE" > "$TMP_FILE";history -c;history -r "$TMP_FILE";rm "$TMP_FILE";history); }