EDIT 2010-07-01 : I’ve packaged up a shell script to allow you to save and jump to commonly used directories. It’s called bashmarks and it has tab completion functionality built-in. Learn more about bashmarks here.
Before I wrote this script, It felt like I spent half of my time in terminal cd-ing around to various directories. If you’re like me, placing this snippet into your .bashrc file will save you tons of time each and every single day:
# Bash Directory Bookmarks alias m1='alias g1="cd `pwd`"' alias m2='alias g2="cd `pwd`"' alias m3='alias g3="cd `pwd`"' alias m4='alias g4="cd `pwd`"' alias m5='alias g5="cd `pwd`"' alias m6='alias g6="cd `pwd`"' alias m7='alias g7="cd `pwd`"' alias m8='alias g8="cd `pwd`"' alias m9='alias g9="cd `pwd`"' alias mdump='alias|grep -e "alias g[0-9]"|grep -v "alias m" > ~/.bookmarks' alias lma='alias | grep -e "alias g[0-9]"|grep -v "alias m"|sed "s/alias //"' touch ~/.bookmarks source ~/.bookmarks
Directory Bookmark Usage
With this in place, your bash shell will have the ability to set and retrieve directory bookmarks. Let’s say you’re in a folder that you visit a hundreds of times per day. Run one of the “m” (a.k.a mark) commands inside the directory to create a bookmark. Here’s an example:
# This will create a bookmark for the /var/www directory user@host[/var/www/] : m1
Now whenever you want to cd into that directory, you can run the corresponding “g” (a.k.a goto mark) command.
# This will cd into /var/www user@host[/etc/apache2] : g1
In other words the m1 command will set the g1 bookmark, the m2 command will set the g2 bookmark, and so on … If you don’t want to keep track of these bookmarks in your head, you’ll be glad to hear that the “lma” (a.k.a “list marks “) command can show you all of your current bookmarks like so:
user@host[/usr/local/] : lma g1='cd /var/www/' g2='cd /etc/'
Persisting the Bookmarks
If you want to preserve your bookmarks for the next time you log in, execute the mdump command which will store the bookmarks into a file called .bookmarks under your HOME directory. Keep in mind that if you do not run this command your bookmarks will be forgotten once you log out of the shell.
13 responses so far
Hey, that’s very nice! thanks!
Great tip, thanks!
Very clever. I like it.
Add ‘mdump’ to your .bash_logout to avoid the inevitable problem of faulty memory (yours).
I created a similar .bat script for windows.
Mine is d.bat
To add an alias to the current directory you type ‘d ‘.
And then to go to that directory it is the same command.
An additional feature I have is it adds each alias as an environment variable prefixed with ‘d-’. You can then use those as parameters for other commands.
eg.
C:\Windows> d win
d-win=C:\Windows
C:\Windows> cd /d F:\test
F:\test> d test
d-test=F:\test
F:\test> d win
C:\Windows> copy *.* %d-test%
Good idea. I just added it to my bash_logout
Hi
You could also find useful listing your bookmarks:
alias favs=’alias|grep -e “alias g[0-9]“|grep -v “alias m”
Very nice! Think I will use this today!
ubersoldat, yes I have a similar alias “lma” that is used for listing the bookmarks.
I did the following to give descriptive bookmark names:
function s {
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
mv ~/.sdirs1 ~/.sdirs
echo "export DIR_$1=$PWD" >> ~/.sdirs
}
function g {
source ~/.sdirs
cd $(eval $(echo echo $(echo \$DIR_$1)))
}
function l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*="
}
Mnemonics:
s – Save directory (argument is a shorthand bookmark name)
g – Go to directory
l – List saved directories
Usage:
$ cd /foo/bar/some/dir
$ s mybookmarkname
…
In an other shell:
$ l
mybookmarkname=/foo/bar/some/dir
$ g mybookmarkname
should take you to /foo/bar/some/dir
Obviously you’ll use far less characters than the 14 used in “mybookmarkname”..
Incidentally, it is also ok to leave the bookmark name..
$ cd /foo/bar/some/dir
$ s
…
In an other shell:
$ l
=/foo/bar/some/dir
$ g
should take you to /foo/bar/some/dir
Btw, it is also possible to use TAB completion on bookmark names using compgen/complete in bash.. so doing something like:
$ g my
should complete to
$ g mybookmarkname
Karthick,
Amazing ! This is way better than my implementation. The snippet below adds tab completion to your script.
# enable custom tab completion
shopt -s progcomp
# jump to bookmark
function g {
source ~/.sdirs
cd $(eval $(echo echo $(echo \$DIR_$1)))
}
# list bookmarks without dirname
function _l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
}
# completion command for g
function _gcomp {
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W '`_l`' -- $curw))
return 0
}
# bind completion command for g to _gcomp
complete -F _gcomp g
# Usage: g [TAB]
In zsh alias does not print alias g1 but just g1. Ugly hack to get it to print this (i am new to grep, awk, zsh) so please fix the ugly ugly mdump and lma lines!
#ZSH Directory Bookmarks
alias m1=’alias g1=”cd `pwd`”‘
alias m2=’alias g2=”cd `pwd`”‘
alias m3=’alias g3=”cd `pwd`”‘
alias m4=’alias g4=”cd `pwd`”‘
alias m5=’alias g5=”cd `pwd`”‘
alias m6=’alias g6=”cd `pwd`”‘
alias m7=’alias g7=”cd `pwd`”‘
alias m8=’alias g8=”cd `pwd`”‘
alias m9=’alias g9=”cd `pwd`”‘
alias mdump=’alias | grep -e “g[0-9]=” | grep -v “m[0-9]” | awk ‘\”{ print “alias ” $1 ” ” $2 }’\” | cat > ~/.bookmarks’
alias lma=’alias | grep -e “g[0-9]=” | grep -v “m[0-9]” | awk ‘\”{ print “alias ” $1 ” ” $2 }’\”’
touch ~/.bookmarks
source ~/.bookmarks
Great Job!
Putting “mdump” or its source code in ~/.bash_logout did not work.
So after little testing adding mdump to the m1-9 commands solves it.
Enjoy.
I like the simplicity of it – with the numbers from 1-9.
INSTALL instructions:
- put into ~/bin/bookmarks or sth.
- source it from ~/.bashrc – add “. ~/bin/bookmarks”
USAGE:
- use m1-9, g1-9 and lma.
- sing/hum some nice melody
#!/bin/bash
# Bash Directory Bookmarks
alias mdump=’alias|grep -e “alias g[0-9]“|grep -v “alias m” > ~/.bookmarks’
alias lma=’alias | grep -e “alias g[0-9]“|grep -v “alias m”|sed “s/alias //”‘
alias m1=’alias g1=”cd `pwd`”; mdump’
alias m2=’alias g2=”cd `pwd`”; mdump’
alias m3=’alias g3=”cd `pwd`”; mdump’
alias m4=’alias g4=”cd `pwd`”; mdump’
alias m5=’alias g5=”cd `pwd`”; mdump’
alias m6=’alias g6=”cd `pwd`”; mdump’
alias m7=’alias g7=”cd `pwd`”; mdump’
alias m8=’alias g8=”cd `pwd`”; mdump’
alias m9=’alias g9=”cd `pwd`”; mdump’
touch ~/.bookmarks
source ~/.bookmarks