up: fuzzy navigation up a directory tree

[Tweet : ADN : nvALT]

Here’s a (relatively) simple shell function for navigating up the tree from your current working directory. It was inspired by bd, but I got frustrated with some aspects (and the fact that it’s not really OS X-compatible anymore).

up allows fuzzy matching as long as the characters are in order. So, if you’re in /Users/ttscoff/Dropbox/Code/popclipextensions/Blockquote.popclipext and you type up dbox, you’ll jump up to your Dropbox folder.

There’s a helper function called _up that you can call directly to integrate into other commands. It simply echoes out the match with no newline, so you can do things like ls -1 $(_up dbox). Mix it into other functions along with something like sentaku and have some fun.

up is designed to be sourced in your login profile. This is partly because I prefer funtions to scripts scattered everywhere, but primarily because scripts run in a child process and the cd command doesn’t affect your current shell unless you source it with . up args. That’s an inconvenience.

If you’re interested in fuzzy Bash completion for up, see this Gist. It will let you type up dbx⇥ (or even up x) and expand it to up Dropbox using standard Bash completion.

Just as an aside, my target directory is very often the root level of my current git repository. This alias will jump you straight to the top level folder of any git-based project (with an update from Bernd Goldschmidt):

# gt: Go Top
alias gt='cd $(git rev-parse --show-toplevel 2>/dev/null || (echo "."; echo "Not within a git repository" >&2))'

I love z and bashmarks, too. Anything that makes moving around the system as fast and as intuitive as possible. I often jump to Terminal from Finder, move to the folder I need and type f, which I have aliased to open -a Finder .. It’s just faster, even with ⌘⇧G available. Have fun!

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.