Skip to content

Instantly share code, notes, and snippets.

@Rob--W
Last active July 1, 2022 14:52
Show Gist options
  • Save Rob--W/5888648 to your computer and use it in GitHub Desktop.
Save Rob--W/5888648 to your computer and use it in GitHub Desktop.
upto / jd to quickly navigate between directories in bash.
# Based on http://unix.stackexchange.com/questions/14303/bash-cd-up-until-in-certain-folder/14311#14311
# Jump to closest matching parent directory
upto () {
if [ -z "$1" ]; then
return
fi
local upto=$@
cd "${PWD/\/$upto\/*//$upto}"
}
# Auto-completion
_upto() {
local cur=${COMP_WORDS[COMP_CWORD]}
local d=${PWD//\//\ }
COMPREPLY=( $( compgen -W "$d" -- "$cur" ) )
}
complete -F _upto upto
# Jump the firs matching subdirectory
jd() {
if [ -z "$1" ]; then
echo "Usage: jd [directory]";
return 1
else
local restore_globstar=$(shopt -p globstar)
shopt -s globstar
cd **"/$@"
$restore_globstar
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment