Skip to content

Instantly share code, notes, and snippets.

@delitescere
Last active November 12, 2018 04:57
Show Gist options
  • Save delitescere/f20461025d4e4e663e19 to your computer and use it in GitHub Desktop.
Save delitescere/f20461025d4e4e663e19 to your computer and use it in GitHub Desktop.
cd .. with less fuss
# cd .. with multiple jumps or jump up to name
..() {
if [ "-" = "$1" ]; then cd -; return; fi; # return to previous directory
if [ "/" = "$1" ]; then cd /; pwd; return; fi; # jump to root
if [ -z "$1" ]; then cd ../; pwd; return; fi; # jump up one
declare -i count=$1; # get a jump count
if [ $count -eq 0 ]; then # wasn't a number, look for name
local go=$(while [ "/" != "$PWD" ] && [ "$(basename $PWD)" != "$1" ]; do cd ..; done; pwd);
# jump up to named directory, or don't move if name wasn't found
if [ "/" != "$go" ]; then cd $go; else return; fi;
else
if [ $count -gt 255 ]; then count=1; fi; # sensible limits
# jump up count directories
cd $(for i in $(eval echo {1..$count}); do echo -n "../"; done);
fi;
pwd; # behave like "cd -" by echoing current directory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment