Created
April 11, 2022 20:40
-
-
Save thingsiplay/1095055393bb2903c0ed2c8c5f840844 to your computer and use it in GitHub Desktop.
cdd - jump to a directory you visited previously (ZSH function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cdd - jump to a directory you visited previously | |
# A Function for ZSH config file. | |
# | |
# Usage: | |
# $ cdd | |
# $ cdd "regex" | |
# | |
# Don't forget to enable "setopt auto_pushd", so that each "cd" command will | |
# add a folder to the list automatically. | |
# | |
cdd() { | |
# * If no argument is given, list visited directories. Jump to the | |
# directory by choosing a number. | |
# * If an argument was given, then filter list by regex and jump directly | |
# to first matching directory. | |
if [ $# -eq 0 ] | |
then | |
dirs -v \ | |
| awk '!v[$2]++' \ | |
&& read index \ | |
&& let "index=$index+0" \ | |
&& builtin cd ~"$index" \ | |
&& let "index=$index+1" \ | |
&& popd -q +"$index" | |
else | |
index=$( \ | |
dirs -v \ | |
| grep -m 1 -E "$1" \ | |
| awk '{print $1}' | |
) \ | |
&& let "index=$index+0" \ | |
&& builtin cd ~"$index" \ | |
&& let "index=$index+1" \ | |
&& popd -q +"$index" | |
fi | |
} | |
# cdd - jump to a directory you visited previously | |
# One liner version without arguments for use as an alias. | |
# | |
# Show list of current directories and switch by choosing a number. | |
#alias cdd='dirs -v && read index && let "index=$index+0" && cd ~"$index" && let "index=$index+1" && popd -q +"$index"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is intended to be added to the ZSH config file. As an alternative, there is a one liner alias, which has no regex argument to it and will also not exclude duplicate entries. It was the previous version before making a function. So use only one of them, either alias or function.