This is a pretty cool little utility I just devised for use in the bash shell to improve navigation. If you use bash a lot, you probably bounce around in and out of directories. Lots of typing and remembering where you've been recently, etc. So what this does is tracks your cd's and lets you trace your path backwards, like the "back" button in your web browser.
The way it works is to use an environment variable called CDHIST (current directory history) which is set up similar to PATH, a colon (:) separated list of paths. A command alias makes the cd command prepend the current directory to this list before actually changing directories. Then there's a little shell script called back.sh which pops the first element off this list, cd's to the path in the removed element (a real cd, not the aliased version), and updates the env variable to not include the removed element.
It could pretty easily be expanded to include moving foreward again, too. I'll probably do this once I get annoyed that I haven't already. I'll post when I do
back.sh
put this on your path somewhere, would probably be good.
#!/bin/sh #Make sure you source it, or the cd won't work (and probably not the # environment variable) #Pops the beginning off of the environment variable CDHIST, which # is a list of paths separated by colons, : #CDs to the popped path, and exports the new list back to CDHIST # #Good Idea: alias back='source back.sh' # alias cd='export CDHIST=$PWD:$CDHIST; cd' # # So everytime you cd to a new dir, it will prepend the # one you're leaving to the CDHIST variable, and you can use # back to trace your path backwards. Note that it's a per # terminal thing, which is good. better than writing to a file pop=`echo $CDHIST | sed 's/^\([^:]*\).*/\1/g'` if [ -n $pop ] then hist=`echo $CDHIST | sed 's/^[^:]*//g'` hist=`echo $hist | sed 's/^://g'` cd $pop export CDHIST=$hist fi
"Installation"
To make this useful, just takes a few aliases, which you can put in you ~/.bashrc, or the system wide /etc/bashrc.
alias cd='export CDHIST=$PWD:$CDHIST; cd' #make sure when you cd it updates the history alias back="source back.sh" #use full path to back.sh if necc. Has to be sourced for the script's cd to work alias lkbk='echo $CDHIST | sed s/^\([^:]*\).*/\1/g' #just lets you peek at the first entry, where you go if you back alias clrhist='export CDHIST=' #clear the history, if you ever need to for somereason alias cdhist='echo $CDHIST' #view the history alias cdk="cd" #if for some reason you want to change dir without doing any of this stuff, do cdk
Don't forget to source ~/.bashrc to implement changes.
use
Easy enough, just cd like you normally do, and to go back in the list, just use the command back. bug:if the list is empty, it sends you home (~), it's not supposed to, but I didn't feel like putting more than a few minutes into this so, it'll have to wait till it annoys me enough to fix it. Or feel free to send me the fix. Project:Contact
The great thing about this is that it's terminal local, because it uses environment variables. So if you're like me, you've usually got half a dozen virtual terminals open at once, and you're hopping back and forth between them. I originally though about saving the cdhistory to a file, but this would be global, and cd in one terminal would affect the back path in every other terminal for that user. I think this way makes a lot more sense.
