Skip to content

Instantly share code, notes, and snippets.

@weakish
Created April 18, 2011 12:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weakish/925202 to your computer and use it in GitHub Desktop.
Save weakish/925202 to your computer and use it in GitHub Desktop.
Bookmark directories in #Bash.

Bookmark directories in Bash.

Install

Source bmark.sh in your ~/.bashrc. (Enableing cdable_vars in Bash makes this script better.)

Usage

Bookmark current directories:

$ bmark name

Recommended name format: [-A-Za-z0-9_]+. Avoid special characters.

Use basename as the default bookmark name:

$ bmark

List all bookmarks:

$ bmarks

Go to one bookmark:

$ cd $name

If you've shopt -s cdable_vars in ~/.bashrc, you can omit the $ symbol:

$ cd name 
$ pushd name

Pushd and popd also work, since bookmarks are simple variables.

Organize the bookmark:

 $ editor ~/.config/bmark/rc

Similar projects

AFAIK, my implementation (this script) is the only one which

  • integrates with bash builtin cd, pushd, popd, etc, instead of a seperate goto command
  • avoids duplicated bookmarks
  • uses basename as the default bookmark name

Repo

https://gist.github.com/925202

Flattr this

# Bookmark directories in Bash.
# by Jakukyo Friel <weakish@gmail.com> under GPL v2
# repo: https://gist.github.com/925202
# bookmark storage
bmarkdb=$HOME/dotfiles/bmark/rc
mkdir -p $HOME/dotfiles/bmark
touch $bmarkdb
source $bmarkdb
bmark () {
# check if already bookmarked
if grep -m1 $PWD\" $bmarkdb; then
echo 'Already bookmarked!'
else # use basename as the default bookmark name
if [ -z $1 ]; then
bmarkname=`basename $PWD`
else
bmarkname=$1
fi
# check if bookmark name is already used
if grep -m1 -E "$bmarkname=" $bmarkdb; then
echo 'Bookmark name already used! Choose a different one.'
else # add bookmark
echo "$bmarkname=\"$PWD\"" >> $bmarkdb
source $bmarkdb
fi
fi
}
bmarks () {
cat $bmarkdb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment