Skip to content

Instantly share code, notes, and snippets.

@RRethy
Last active May 17, 2021 16:16
Show Gist options
  • Save RRethy/77cf1ff213ec33a3effcb76c85c97cfc to your computer and use it in GitHub Desktop.
Save RRethy/77cf1ff213ec33a3effcb76c85c97cfc to your computer and use it in GitHub Desktop.
A small ZSH script to replace autojump
# Usage: `jd `
# Make after typing j, d, and a space, fzy will pop up offering a fuzzy selector.
# Setup some data a data file to store visited directories
mkdir -p "$XDG_DATA_HOME/zshrc"
JD_DATA_DIR="$XDG_DATA_HOME/zshrc/chpwd.txt"
touch $JD_DATA_DIR
local tmp=$(mktemp)
cat $JD_DATA_DIR | while read dir ; do [[ -d $dir ]] && echo $dir ; done > $tmp
cat $tmp > $JD_DATA_DIR
# Track visited directories
chpwd_functions+=(on_chpwd)
function on_chpwd {
local tmp=$(mktemp)
{ echo $PWD ; cat $JD_DATA_DIR } | sort | uniq 1> $tmp
cat $tmp > $JD_DATA_DIR
}
# zle widget function
function fzy_jd {
# check if `jd ` was triggered in the middle of another command
# e.g. $ aaaaaaajd
# If so, we manually input the `jd `
if [[ ! -z $BUFFER ]]; then
# Append `jd ` to the prompt
BUFFER=$BUFFER"jd "
# move the cursor to the end of the line
zle end-of-line
return 0
fi
# ask the user to select a directory to jump to
local dir=$({ echo $HOME ; cat $JD_DATA_DIR } | fzy)
if [[ -z $dir ]]; then
# no directory was selected, reset the prompt to what it was before
zle reset-prompt
return 0
fi
# Setup the command to change the directory
BUFFER="cd $dir"
# Accepts the cd we setup above
zle accept-line
local ret=$?
# force the prompt to redraw to mimic what would occur with a normal cd
zle reset-prompt
return $ret
}
# define the new widget function
zle -N fzy_jd
# bind the widget function to `jd `
bindkey 'jd ' fzy_jd
# a nicety so that executing just jd will mimic the behaviour of just executing
# cd, that is, change the pwd to $HOME
alias jd=cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment