Shell bookmarks
First published at Wednesday 27 August 2008
Warning: This blog post is more then 16 years old – read and use with care.
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.
Update
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. :)
Subscribe to updates
There are multiple ways to stay updated with new posts on my blog:
Comments
Jan Schneider at Wednesday, 27.8. 2008
Sounds like http://micans.org/apparix/
Bill Karwin at Wednesday, 27.8. 2008
Sounds almost like $CDPATH, which is a feature built into most interactive shells like bash, ksh, csh, and tcsh (in csh and tcsh it's lower-case like $cdpath).
Thomas Koch at Thursday, 28.8. 2008
or
http://www.skamphausen.de/software/cdargs
Alexandr at Monday, 1.9. 2008
How about 'alians' usage? Like this: alias blog="cd /project_dir/blog"
after this you can type blog and will be at needed directory
hypotheek at Wednesday, 10.9. 2008
That will probably work to Alexandr!
Jean-Rene David at Tuesday, 7.10. 2008
zsh has that feature builtin. It's called "named directories".
% hash -d blog="/path/to/your/blog" % cd ~blog
jpic at Friday, 27.3. 2009
Very interresting thanks a lot for sharing!
I migrated to zsh, and then figured about the bash variable CDPATH. I've set it up this way: export CDPATH=".:$HOME:$HOME/src" because everything i use is at most 2 directories away from $HOME ... Then i can: cd foosources -> ~/src/foosources; cd var/couchdb -> ~/var/couchdb etc ... And an alias for the project, app and framework i work with: alias mm="starthacking sites/mmm" etc ...
Well, you wanted to do it in PHP ;)