ano.malo.us pretty typical actually..

Posts tagged shell

Directory Bookmarks

After typing cd /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages/ for the hundredth time, I decided there had to be a better way. Even with tab completion, it’s a pain to access deeply-nested directories. I know about cdargs but it’s not very flexible and doesn’t work with regular bash commands like cd, cp, mv, etc..

I wrote up a couple of bash functions that save directory bookmarks to a file and make them available as environment variables. To install, download dirmarks.sh and source it from .bashrc or .bash_profile.

Once installed, use mark bookmark_name to save a bookmark for the current directory and use lsmarks search_term to see/grep the list of saved bookmarks. You can then use the bookmark like any other environment variable: cd $bookmark or cat $bookmark/file.txt. The bookmarks are saved in ~/.dirmarks.

The following example usage should clarify what my words have confused:

# lsmarks with any arguments
$ lsmarks
bubble  "/Users/shahad/projects/tg_apps/bubble"
docs    "/Users/shahad/projects/docs"
pebl    "/Users/shahad/projects/pebl"
py      "/Library/Frameworks/Python.framework/Versions/Current"
pybin   "/Library/Frameworks/Python.framework/Versions/Current/bin"
pylibs  "/Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages"
 
# lsmarks with search term
$ lsmarks py
py      "/Library/Frameworks/Python.framework/Versions/Current"
pybin   "/Library/Frameworks/Python.framework/Versions/Current/bin"
pylibs  "/Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages"
 
# using a directory bookmark
$ cd $bubble
$ pwd
/Users/shahad/projects/tg_apps/bubble
 
# adding a directory bookmark
$ cd bubble/static/images
$ mark bubble_img
Adding bookmark bubble_img --> "/Users/shahad/projects/tg_apps/bubble/bubble/static/images"
 
# look ma, the new bookmark has been added!
$ lsmarks bubble
bubble      "/Users/shahad/projects/tg_apps/bubble"
bubble_img  "/Users/shahad/projects/tg_apps/bubble/bubble/static/images"
 
# directories with spaces in the name
$ cd /Users/shahad/Library/Application\ Support/
$ mark appsu
Adding bookmark appsu --> "/Users/shahad/Library/Application Support"
 
# trying to use bookmark as normal.. fails :(
$ cd ~
$ cd $appsu
-bash: cd: /Users/shahad/Library/Application: Not a directory
 
# we have to enclose the dir bookmark in quotes.
$ cd "$appsu"
$ pwd 
/Users/shahad/Library/Application Support

Note that if a directory has spaces in its name, you have to enclose the bookmark name in quotes. I can’t find a way around this requirement that works on my mac. If anyone has suggestions, I would appreciate them.