Skip to content

Commit 158fe11

Browse files
committed
Make zi cd with no arguments or with a -[[:digit:]] argument jump to
last or -2, or -3, etc. last visited directory with `zi cd …`. This stack essentially works as `cd -[[:digit:]]` except that the last dir on the stack is -1, not -0 (i.e.: just like Zsh array indices).
1 parent a4dc13f commit 158fe11

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

zinit-autoload.zsh

+28-1
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,12 @@ ZINIT[EXTENDED_GLOB]=""
27022702
# FUNCTION: .zinit-cd [[[
27032703
# Jumps to plugin's directory (in Zinit's home directory).
27042704
#
2705+
# Remembers the visited dirs in a backlog and allows to automatically cd
2706+
# to them when no argument is given ($1, $2 are empty) or if a negative
2707+
# number will be given to point to that number distance entry from the top
2708+
# of the stack. -1 (the default) points to the last, most recent dir, and
2709+
# so on. Remembers ~1500 last directories.
2710+
#
27052711
# User-action entry point.
27062712
#
27072713
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
@@ -2710,9 +2716,30 @@ ZINIT[EXTENDED_GLOB]=""
27102716
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
27112717
builtin setopt extendedglob warncreateglobal typesetsilent rcquotes
27122718

2719+
local datp=$ZINIT[HOME_DIR]/last-cd.dat
2720+
command touch -- "$datp"
2721+
if [[ ( -z $1 || $1 == -[[:digit:]]##* ) && -z $2 ]]; then
2722+
1=${(M)${1#-}##<->##}
2723+
local -a lines=( ${(@f)"$(<$datp)"} )
2724+
if [[ $lines[-1] = [[:space:]]# ]]; then
2725+
lines[-1]=()
2726+
fi
2727+
integer count=$#lines-${1:-0}+0${${(M)1:#0##}:-${1:+1}}
2728+
while (( count > 0 )); do
2729+
if [[ -e $lines[count] ]]; then
2730+
builtin pushd "$lines[count]" && return 0 || +zinit-message "Failed to change dir to one of the last CWDs[{num}-$(($#lines-count+1)){rst}]: {pid}$lines[count]:t{rst}, skipping…"
2731+
else
2732+
+zinit-message "One of the last CWDs[{num}-$(($#lines-count+1)){rst}] ({pid}$lines[count]:t{rst}) doesn't exist, skipping…"
2733+
fi
2734+
(( count -- ))
2735+
done
2736+
if (( $#lines > 1500 )); then
2737+
print -rl -- $lines[-1150,-1] >>! $datp
2738+
fi
2739+
fi
27132740
.zinit-get-path "$1" "$2" && {
27142741
if [[ -e $REPLY ]]; then
2715-
builtin pushd $REPLY
2742+
builtin pushd $REPLY && print $REPLY >>! $datp
27162743
else
27172744
+zinit-message "No such plugin or snippet"
27182745
return 1

0 commit comments

Comments
 (0)