5 local RUNNING_AS_COMMAND=
7 if [[ $(whence -w $0) == *:' 'command ]]; then
12 local DOC='scd -- smart change to a recently used directory
13 usage: scd [options] [pattern1 pattern2 ...]
14 Go to a directory path that matches all patterns. Prefer recent or
15 frequently visited directories as found in the directory index.
16 Display a selection menu in case of multiple matches.
19 ^PAT match at the path root, "^/home"
20 PAT$ match paths ending with PAT, "man$"
21 ./ match paths under the current directory
22 :PAT require PAT to span the tail, ":doc", ":re/doc"
25 -a, --add add current or specified directories to the index.
26 --unindex remove current or specified directories from the index.
27 -r, --recursive apply options --add or --unindex recursively.
28 --alias=ALIAS create alias for the current or specified directory and
29 store it in ~/.scdalias.zsh.
30 --unalias remove ALIAS definition for the current or specified
31 directory from ~/.scdalias.zsh.
32 Use "OLD" to purge aliases to non-existent directories.
33 -A, --all display all directories even those excluded by patterns
34 in ~/.scdignore. Disregard unique match for a directory
35 alias and filtering of less likely paths.
36 -p, --push use "pushd" to change to the target directory.
37 --list show matching directories and exit.
38 -v, --verbose display directory rank in the selection menu.
39 -h, --help display this message and exit.
42 local SCD_HISTFILE=${SCD_HISTFILE:-${HOME}/.scdhistory}
43 local SCD_HISTSIZE=${SCD_HISTSIZE:-5000}
44 local SCD_MENUSIZE=${SCD_MENUSIZE:-20}
45 local SCD_MEANLIFE=${SCD_MEANLIFE:-86400}
46 local SCD_THRESHOLD=${SCD_THRESHOLD:-0.005}
47 local SCD_SCRIPT=${RUNNING_AS_COMMAND:+$SCD_SCRIPT}
48 local SCD_ALIAS=~/.scdalias.zsh
49 local SCD_IGNORE=~/.scdignore
51 # Minimum logarithm of probability. Avoids out of range warning in exp().
52 local -r MINLOGPROB=-15
54 # When false, use case-insensitive globbing to fix PWD capitalization.
55 local PWDCASECORRECT=true
56 if [[ ${OSTYPE} == darwin* ]]; then
60 local a d m p i maxrank threshold
61 local opt_help opt_add opt_unindex opt_recursive opt_verbose
62 local opt_alias opt_unalias opt_all opt_push opt_list
63 local -A drank dalias scdignore
67 setopt extendedglob noautonamedirs brace_ccl
69 # If SCD_SCRIPT is defined make sure that that file exists and is empty.
70 # This removes any old previous commands from the SCD_SCRIPT file.
71 [[ -n "$SCD_SCRIPT" ]] && [[ -s $SCD_SCRIPT || ! -f $SCD_SCRIPT ]] && (
76 # process command line options
78 zmodload -i zsh/datetime
79 zmodload -i zsh/parameter
80 zparseopts -D -E -- a=opt_add -add=opt_add -unindex=opt_unindex \
81 r=opt_recursive -recursive=opt_recursive \
82 -alias:=opt_alias -unalias=opt_unalias \
83 A=opt_all -all=opt_all p=opt_push -push=opt_push -list=opt_list \
84 v=opt_verbose -verbose=opt_verbose h=opt_help -help=opt_help \
87 # remove the first instance of "--" from positional arguments
90 if [[ -n $opt_help ]]; then
95 # load directory aliases if they exist
96 [[ -r $SCD_ALIAS ]] && source $SCD_ALIAS
98 # load scd-ignore patterns if available
99 if [[ -s $SCD_IGNORE ]]; then
103 [[ $p != [\#]* ]] || continue
104 [[ -n $p ]] || continue
105 # expand leading tilde if it has valid expansion
106 if [[ $p == [~]* ]] && ( : ${~p} ) 2>/dev/null; then
114 # Private internal functions are prefixed with _scd_Y19oug_.
115 # Clean them up when the scd function returns.
117 trap 'unfunction -m "_scd_Y19oug_*"' EXIT
119 # works faster than the (:a) modifier and is compatible with zsh 4.2.6
120 _scd_Y19oug_abspath() {
121 set -A $1 ${(ps:\0:)"$(
131 print -Nr -- (#i)$PWD
137 # define directory alias
138 if [[ -n $opt_alias ]]; then
139 if [[ -n $1 && ! -d $1 ]]; then
140 print -u2 "'$1' is not a directory."
144 _scd_Y19oug_abspath d ${1:-$PWD}
145 # alias in the current shell, update alias file if successful
150 [[ -r $SCD_ALIAS ]] && source $SCD_ALIAS
152 hash -dL >| $SCD_ALIAS
157 # undefine one or more directory aliases
158 if [[ -n $opt_unalias ]]; then
162 if (( ${uu[(I)OLD]} && ${+nameddirs[OLD]} == 0 )); then
163 uu=( ${uu:#OLD} ${(ps:\0:)"$(
165 if [[ -r $SCD_ALIAS ]]; then
168 for a d in ${(kv)nameddirs}; do
169 [[ -d $d ]] || print -Nr -- $a
177 if [[ ${+nameddirs[$d]} == 0 && -d $d ]]; then
178 _scd_Y19oug_abspath d $d
180 a=${(k)nameddirs[$d]:-${(k)nameddirs[(r)$d]}}
183 print -u2 "'$p' is neither a directory alias nor an aliased path."
186 # unalias in the current shell and remember to update the alias file
187 if unhash -d -- $a 2>/dev/null; then
191 if [[ $#m != 0 && -r $SCD_ALIAS ]]; then
197 unhash -d -- $a 2>/dev/null
199 hash -dL >| $SCD_ALIAS
205 # The "compress" function collapses repeated directories into
206 # a single entry with a time-stamp yielding an equivalent probability.
207 _scd_Y19oug_compress() {
208 awk -v epochseconds=$EPOCHSECONDS \
209 -v meanlife=$SCD_MEANLIFE \
210 -v minlogprob=$MINLOGPROB \
214 pmin = exp(minlogprob);
216 /^: deleted:0;/ { next; }
217 length($0) < 4096 && $2 > 1000 {
219 sub("^[^;]*;", "", df);
221 tau = 1.0 * ($2 - epochseconds) / meanlife;
222 prob = (tau < minlogprob) ? pmin : exp(tau);
223 dlist[last[df]] = "";
229 for (i = 1; i <= NR; ++i) {
232 ts = log(ptot[d]) * meanlife + epochseconds;
233 printf(": %.0f:0;%s\n", ts, d);
240 # Rewrite directory index if it is at least 20% oversized.
242 if [[ -z $opt_unindex && -s $SCD_HISTFILE ]] && \
243 curhistsize=$(wc -l <$SCD_HISTFILE) && \
244 (( $curhistsize > 1.2 * $SCD_HISTSIZE )); then
245 # Compress repeated entries in a background process.
247 m=( ${(f)"$(_scd_Y19oug_compress $SCD_HISTFILE)"} )
248 # purge non-existent and ignored directories
252 [[ -z ${scdignore[(k)$d]} ]] || continue
253 [[ -d $d ]] || continue
254 $PWDCASECORRECT || d=( (#i)${d} )
256 print -r -- "${t};${d}"
260 # cut old entries if still oversized
261 if [[ $#m -gt $SCD_HISTSIZE ]]; then
262 m=( ${m[-$SCD_HISTSIZE,-1]} )
264 # Checking existence of many directories could have taken a while.
265 # Append any index entries added in meantime.
266 m+=( ${(f)"$(sed "1,${curhistsize}d" $SCD_HISTFILE)"} )
267 print -lr -- $m >| ${SCD_HISTFILE}
271 # Determine the last recorded directory
272 if [[ -s ${SCD_HISTFILE} ]]; then
273 last_directory=${"$(tail -n 1 ${SCD_HISTFILE})"#*;}
276 # The "record" function adds its arguments to the directory index.
277 _scd_Y19oug_record() {
278 while [[ -n $last_directory && $1 == $last_directory ]]; do
281 if [[ $# -gt 0 ]]; then
283 p=": ${EPOCHSECONDS}:0;"
284 print -lr -- ${p}${^*} >>| $SCD_HISTFILE )
288 if [[ -n $opt_add ]]; then
289 m=( ${^${argv:-$PWD}}(N-/) )
290 _scd_Y19oug_abspath m ${m}
291 _scd_Y19oug_record $m
292 if [[ -n $opt_recursive ]]; then
294 print -n "scanning ${d} ... "
295 _scd_Y19oug_record ${d}/**/*(-/N)
302 # take care of removing entries from the directory index
303 if [[ -n $opt_unindex ]]; then
304 if [[ ! -s $SCD_HISTFILE ]]; then
307 argv=( ${argv:-$PWD} )
308 # expand existing directories in the argument list
310 if [[ -d ${argv[i]} ]]; then
311 _scd_Y19oug_abspath d ${argv[i]}
315 # strip trailing slashes, but preserve the root path
316 argv=( ${argv/(#m)?\/##(#e)/${MATCH[1]}} )
317 m="$(awk -v recursive=${opt_recursive} '
319 for (i = 2; i < ARGC; ++i) {
323 unindex_root = ("/" in argset);
326 d = $0; sub(/^[^;]*;/, "", d);
327 if (d in argset) next;
330 if (unindex_root) exit;
332 if (substr(d, 1, length(a) + 1) == a"/") next;
336 ' $SCD_HISTFILE $* )" || $EXIT $?
338 [[ ${#m} == 0 ]] || print -r -- $m >> ${SCD_HISTFILE}
342 # The "action" function is called when there is just one target directory.
343 _scd_Y19oug_action() {
345 [[ -z ${opt_push} ]] || cdcmd=pushd
346 builtin $cdcmd $1 || return $?
347 if [[ -z $SCD_SCRIPT && -n $RUNNING_AS_COMMAND ]]; then
348 print -u2 "Warning: running as command with SCD_SCRIPT undefined."
350 if [[ -n $SCD_SCRIPT ]]; then
352 if [[ $OSTYPE == cygwin && ${(L)SCD_SCRIPT} == *.bat ]]; then
355 print -r "${cdcmd} ${(qqq)d}" >| $SCD_SCRIPT
359 # Select and order indexed directories by matching command-line patterns.
360 # Set global arrays dmatching and drank.
361 _scd_Y19oug_match() {
362 ## single argument that is an existing directory or directory alias
363 if [[ -z $opt_all && $# == 1 ]] && \
364 [[ -d ${d::=${nameddirs[$1]}} || -d ${d::=$1} ]] && [[ -x $d ]];
366 _scd_Y19oug_abspath dmatching $d
367 drank[${dmatching[1]}]=1
371 # quote brackets when PWD is /Volumes/[C]/
372 local qpwd=${PWD//(#m)[][]/\\${MATCH}}
374 # support "./" as an alias for $PWD to match only subdirectories.
375 argv=( ${argv/(#s).\/(#e)/(#s)${qpwd}(|/*)(#e)} )
377 # support "./pat" as an alias for $PWD/pat.
378 argv=( ${argv/(#m)(#s).\/?*/(#s)${qpwd}${MATCH#.}} )
380 # support "^" as an anchor for the root directory, e.g., "^$HOME".
381 argv=( ${argv/(#m)(#s)\^?*/(#s)${${~MATCH[2,-1]}}} )
383 # support "$" as an anchor at the end of directory name.
384 argv=( ${argv/(#m)?[$](#e)/${MATCH[1]}(#e)} )
386 # support prefix ":" to match over the tail component.
387 argv=( ${argv/(#m)(#s):?*/${MATCH[2,-1]}[^/]#(#e)} )
389 # calculate rank of all directories in SCD_HISTFILE and store it in drank.
390 # include a dummy entry to avoid issues with splitting an empty string.
391 [[ -s $SCD_HISTFILE ]] && drank=( ${(f)"$(
392 print -l /dev/null -10
394 awk -v epochseconds=$EPOCHSECONDS \
395 -v meanlife=$SCD_MEANLIFE \
396 -v minlogprob=$MINLOGPROB \
400 pmin = exp(minlogprob);
404 sub("^[^;]*;", "", df);
408 length($0) < 4096 && $2 > 0 {
410 sub("^[^;]*;", "", df);
413 while (!(dp in ptot)) {
415 sub("//*[^/]*$", "", dp);
418 if ($2 <= 1000) next;
419 tau = 1.0 * ($2 - epochseconds) / meanlife;
420 prob = (tau < minlogprob) ? pmin : exp(tau);
423 END { for (di in ptot) { print di; print ptot[di]; } }
427 unset "drank[/dev/null]"
429 # filter drank to the entries that match all arguments
432 drank=( ${(kv)drank[(I)${~p}]} )
434 # require that at least one argument matches in directory tail name.
435 p="(#l)*(${(j:|:)argv})[^/]#"
436 drank=( ${(kv)drank[(I)${~p}]} )
438 # discard ignored directories
439 if [[ -z ${opt_all} ]]; then
440 for d in ${(k)drank}; do
441 [[ -z ${scdignore[(k)$d]} ]] || unset "drank[$d]"
445 # build a list of matching directories reverse-sorted by their probabilities
447 builtin printf "%s %s\n" ${(Oakv)drank} |
448 /usr/bin/sort -grk1 )"}
450 dmatching=( ${dmatching#*[[:blank:]]} )
452 # do not match $HOME or $PWD when run without arguments
453 if [[ $# == 0 ]]; then
454 dmatching=( ${dmatching:#(${HOME}|${PWD})} )
457 # keep at most SCD_MENUSIZE of matching and valid directories
458 # mark up any deleted entries in the index
462 for d in $dmatching; do
463 [[ ${#m} == $SCD_MENUSIZE ]] && break
464 (( ${+isdeleted[$d]} == 0 )) || continue
465 [[ -d $d ]] || { isdeleted[$d]=1; continue }
469 if [[ -n ${isdeleted} ]]; then
470 print -lr -- ": deleted:0;"${^${(k)isdeleted}} >> $SCD_HISTFILE
473 # find the maximum rank
475 for d in $dmatching; do
476 [[ ${drank[$d]} -lt maxrank ]] || maxrank=${drank[$d]}
479 # discard all directories below the rank threshold
480 threshold=$(( maxrank * SCD_THRESHOLD ))
481 if [[ -n ${opt_all} ]]; then
484 dmatching=( ${^dmatching}(Ne:'(( ${drank[$REPLY]} >= threshold ))':) )
489 ## process matching directories.
490 if [[ ${#dmatching} == 0 ]]; then
491 print -u2 "No matching directory."
495 ## build formatted directory aliases for selection menu or list display
496 for d in $dmatching; do
497 if [[ -n ${opt_verbose} ]]; then
498 dalias[$d]=$(printf "%.3g %s" ${drank[$d]} $d)
500 dalias[$d]=$(print -Dr -- $d)
504 ## process the --list option
505 if [[ -n $opt_list ]]; then
506 for d in $dmatching; do
507 print -r -- "# ${dalias[$d]}"
513 ## handle a single matching directory here.
514 if [[ ${#dmatching} == 1 ]]; then
515 _scd_Y19oug_action $dmatching
519 ## Here we have multiple matches. Let's use the selection menu.
521 a=( ${a[1,${#dmatching}]} )
523 for i in {1..${#dmatching}}; do
524 [[ -n ${a[i]} ]] || break
525 p+="${a[i]}) ${dalias[${dmatching[i]}]}"
530 if read -s -k 1 d && [[ ${i::=${a[(I)$d]}} -gt 0 ]]; then
531 _scd_Y19oug_action ${dmatching[i]}