# Copyright (c) 2018 Chris West. Licensed under MIT OR Apache-2.0 # Parts Copyright (c) 2009 rupa deadwyler. Licensed under the WTFPL license, Version 2 # maintains a jump-list of the directories you actually use # # INSTALL: # * put something like this in your .bashrc/.zshrc: # . /path/to/z.sh # * cd around for a while to build up the db # * PROFIT!! # * optionally: # set $_Z_CMD in .bashrc/.zshrc to change the command (default z). # set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z). # TODO: set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself. # TODO: set $_Z_EXCLUDE_DIRS to an array of directories to exclude. # TODO: set $_Z_OWNER to your username if you want use z while sudo with $HOME kept # # USE: # * z foo # cd to most frecent dir matching foo # * z foo bar # cd to most frecent dir matching foo and bar # * z -r foo # cd to highest ranked dir matching foo # * z -t foo # cd to most recently accessed dir matching foo # * z -l foo # list matches instead of cd # * z -e foo # echo the best match, don't cd # * z -c foo # restrict matches to subdirs of $PWD [ -d "${_Z_DATA:-$HOME/.z}" ] && { echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory." } _z() { # if symlink, dereference #[ -h "$datafile" ] && datafile=$(readlink "$datafile") # bail if we don't own ~/.z and $_Z_OWNER not set #[ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return local output ret output="$(zrs "$@")" ret=$? case ${ret} in 69) # 69: DoCd builtin cd "${output}" ;; 70) # 70: NoOutput ;; 0) # 0: Success echo "${output}" ;; *) echo "zrs failed: ${ret}" ;; esac } alias ${_Z_CMD:-z}='_z 2>&1' [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P" if type compctl >/dev/null 2>&1; then # zsh [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list, avoid clobbering any other precmds. if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then _z_precmd() { (_z --add "${PWD:a}" &) } else _z_precmd() { (_z --add "${PWD:A}" &) } fi [[ -n "${precmd_functions[(r)_z_precmd]}" ]] || { precmd_functions[$(($#precmd_functions+1))]=_z_precmd } } _z_zsh_tab_completion() { # tab completion local compl read -l compl reply=(${(f)"$(zrs --complete "$compl")"}) } compctl -U -K _z_zsh_tab_completion _z elif type complete >/dev/null 2>&1; then # bash # tab completion complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z} [ "$_Z_NO_PROMPT_COMMAND" ] || { # populate directory list. avoid clobbering other PROMPT_COMMANDs. grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || { PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''(_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &);' } } fi