Saving the current directory in zsh history

I wanted to achieve the same as here, Save the current directory to bash history , but in the zsh shell. I have not made any zsh attempts yet, 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  
}  

Now I save the command annotated with the directory name to a log file. This works great for me. I just thought that there might be a way to make a replacement in the history buffer itself.

+5
source share
2 answers
function _-accept-line() {
    [[ -z "${BUFFER" ]] || [[ "${BUFFER}" =~ "### ${(q)PWD}\$" ]] || BUFFER="${BUFFER} ### ${PWD}"
    zle .accept-line
}
zle -N accept-line _-accept-line

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
}
+6
source

, , precmd(), :

    if [ "$LAST_DIR" != "$PWD" ]
    then
            print -s "##dir## $PWD"
            LAST_DIR=$PWD
    fi

'## dir ## dir name' .

+3

All Articles