Sunday, September 26, 2010

Using cdargs in Cygwin

The cdargs utility is a very powerful directory bookmarking in *nix shells, including Cygwin. There are many wonderful pages on how to use and extend cdargs, some of which I reference at the end of this post.

Unfortunately, these pages talk about some utility functions (mark, cdb, cv, ca) that you get from sourcing cdargs-bash.sh - which I don't have in my copy of Cygwin. After reading man cdargs though, I found that the two most important functions were easy to create for myself: mark to create a cdargs bookmark and cdb to cd into the directory referenced by the bookmark.

Put these in your .bashrc or .bash_profile.

# cd to a cdargs bookmark.
function mark() {
   if [ $# -ne 1 ] ; then
     echo Usage: mark cdargs-bookmark-name
     return 2
   fi
   cdargs --add=:$1:` cygpath -u -a .`
}

# Create a bookmarks in cdargs for the current directory.
function cdb() {
   if [ $# -ne 1 ] ; then
     echo Usage: cdb cdargs-bookmark-name
     return 2
   fi
   cdargs "$1" && cd "`cat "$HOME/.cdargsresult"`" ;
}

Using them is easy enough too. Use mark to create a cdargs entry (stored in the plain text file ~/.cdargs - which you can even edit with a text editor). Then use mark to go to (cd) a bookmarked directory. If you ever need to be reminded what your bookmarks are, look at the ~/.cdargs file or just run cdargs.

Example usage.

cd /c/really/long/path/that/is/hard/to/type/and/remember
mark bigPath

Now you have bookmarked the directory against the key bigPath. To get back to that directory easily, use the command cdb bigPath.

The following pages have helped me learn more about how to use cdargs.