Skip to content

Commit fa45f91

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 fa45f91

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

zinit-autoload.zsh

+27-1
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,11 @@ ZINIT[EXTENDED_GLOB]=""
27022702
# FUNCTION: .zinit-cd [[[
27032703
# Jumps to plugin's directory (in Zinit's home directory).
27042704
#
2705+
# Remembers which directories you have navigated to, so you can "jump" to them in
2706+
# just a few keystrokes. When n not given ($1, $2 are empty) or if a negative number
2707+
# will be entered to point to that number distance entry from the top of the stack. -1
2708+
# (the default) points to the last, most recent dir, and so on. By default, the
2709+
# last 1500 directories are remembered.
27052710
# User-action entry point.
27062711
#
27072712
# $1 - plugin spec (4 formats: user---plugin, user/plugin, user, plugin)
@@ -2710,9 +2715,30 @@ ZINIT[EXTENDED_GLOB]=""
27102715
builtin emulate -LR zsh ${=${options[xtrace]:#off}:+-o xtrace}
27112716
builtin setopt extendedglob warncreateglobal typesetsilent rcquotes
27122717

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

0 commit comments

Comments
 (0)