I wanted to achieve the same as asked here Saving current directory to bash history but within Zsh shell. I haven't done any Zsh trickery before but so far I have:
function precmd {
hpwd=$history[$((HISTCMD-1))]
if [[ $hpwd == "cd" ]]; then
cwd=$OLDPWD
else
cwd=$PWD
fi
hpwd="${hpwd% ### *} ### $cwd"
echo "$hpwd" >>~/.hist_log
}
Right now I save the command annotated with the directory name to a log file. This works fine for me. Just thought there might be a way to make the same replacement in the history buffer itself.
To view all the commands stored in your ZSH history file, use the history command. In most cases, the history command will display an extensive list of all your executed commands. You can pipe the output to commands such as grep to search for a specific command or less to navigate it easily.
If I can quickly change directories ( cd ) to anywhere in my computer's file system, how can I go back if I mistakenly navigate away from somewhere I was working? Turns out that Bash (and Zsh) allow a quick return by using $ cd - .
Increasing the History File Size Unfortunately, zsh's default history file size is limited to 10000 lines by default and will truncate the history to this length by deduplicating entries and removing old data. Adding the following lines to . zshrc will remove the limits and deduplication of the history file.
Zsh is more interactive and customizable than Bash. Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh.
function _-accept-line() {
[[ -z "${BUFFER" ]] || [[ "${BUFFER}" =~ "### ${(q)PWD}\$" ]] || BUFFER="${BUFFER} ### ${PWD}"
zle .accept-line
}
zle -N accept-line _-accept-line
Will add ### ${PWD}
to your command line. Not the best solution you could use, but it works.
UPD: Answer based on @Dennis Williamson's comment:
function zshaddhistory() {
print -sr "${1%%$'\n'} ### ${PWD}"
fc -p
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With