Shell bookmarks - Kore Nordmann

Shell bookmarks

Developing in lots of different projects with different directory structures and navigating mostly on the shell sometimes makes it hard to keep track on where you currently are. A simple script for shell bookmarks made navigating much easier for me.

Who does not know orgies of cd ../../../foo/bar to get back to some other directory? Wouldn't it be much easier, to just type go blog to go to your blog directory to write some new blog post? At least I thought so and wanted such a bookmark script, and since some people saw me using it and asked for it, here it is:

$ go -l Stored bookmarks: downloads: /home/kore/downloads/ ezc: /home/kore/devel/ezcomponents/trunk/ texts: /home/kore/devel/personal/documents/textual [...] $ go -a blog /home/kore/devel/kore/content/blog/ $ go blog $ pwd /home/kore/devel/kore/content/blog

I use the same for maintaining my SSH accounts on the various servers, with the very similar command s, so s blog is enough to access my website.


In the comments some people point to similar sollutions, which partly already come with your shell:

How does it work?

It took me a few minutes to figure out how to get such a thing working, because shell scripts normally open a sub shell, in which the commands are executed and is terminated afterwards. No way to change the current working directory. But, shell functions work just fine:

# Function wrapping the go call, to change the working directory in the current # shell and not only in some child processes without dotting the script. function go() { # Call original script with same parameters RESULT=`go.script $@` # Check if exit code is 128, then change directory. Otherwise just dispatch # the results if [ $? = 128 ] ; then cd "$RESULT" else echo "$RESULT" return $? fi }

This small shell script just wraps around another (PHP) script, which does the parameter parsing, storing of the configuration, etc.

This function needs to be defined in your .bashrc (or similar, depending on the shell you use), or a file file defining this functions needs to be source'd from your .bashrc.

Why PHP you ask? Why not, I have it installed anywhere anyhow. And it trivial to write for me.

Reusing our work

If you do not want to write that trivial script yourself, you may just check out the repository, jakob and me use to maintain some random trivial shell scripts, and add these lines to your .bashrc:

# Add local CLI tools to path TOOLSROOT="/path/to/clitools" export PATH="$PATH:$TOOLSROOT/bin" source "$TOOLSROOT/bashrc"

Where /path/to/clitools points to your checkout of the repository. And there are even some more gems in this repository you'd need to discover yourself. :)