Skip to content

Instantly share code, notes, and snippets.

@pratik60
Created November 14, 2014 13:55
Show Gist options
  • Save pratik60/35efd4c5bca41eac3389 to your computer and use it in GitHub Desktop.
Save pratik60/35efd4c5bca41eac3389 to your computer and use it in GitHub Desktop.
bookmarker.sh
#!/bin/sh
# Set of bash functions that allows you to bookmark folders in the command-line.
# To bookmark a folder, simply go to that folder, then bookmark it like so:
# bookmark foo
#
# The bookmark will be named "foo"
#
# When you want to get back to that folder use:
# go foo
#
# To see a list of bookmarks:
# bookmarksshow
#
# Tab completion works, to go to the shoobie bookmark:
# go sho[tab]
#
# Your bookmarks are stored in the ~/.bookmarks file
bookmarks_file=~/.bookmarks
# Create bookmarks_file it if it doesn't exist
if [[ ! -f $bookmarks_file ]]; then
touch $bookmarks_file
fi
bookmark (){
bookmark_name=$1
if [[ -z $bookmark_name ]]; then
bookmark_name=${PWD##*/} #Store the current folder name as bookmark by default
fi
bookmark="`pwd`|$bookmark_name" # Store the bookmark as folder|name
if [[ -z `grep "$bookmark_name$" $bookmarks_file` ]]; then
echo $bookmark >> $bookmarks_file
echo "Bookmark '$bookmark_name' saved"
else
echo "Bookmark with '$bookmark_name' already exists"
echo 'Please provide another name for your bookmark. For example:'
echo ' bookmark foo'
fi
}
unbookmark (){
unbookmark_name=$1
if [[ -z $unbookmark_name ]]; then
echo 'Invalid name, please provide a name that has to be unbookmarked. For example:'
echo ' unbookmark foo'
else
unbookmark="$unbookmark_name$" # Store the bookmark as folder|name
if [[ -z `grep "$unbookmark" $bookmarks_file` ]]; then
echo "Bookmark not present, so can't be unbookmarked. To see a list of bookmarks:"
echo " bookmarksshow"
else
`grep -v "$unbookmark" $bookmarks_file > .bookmark_temp; mv .bookmark_temp $bookmarks_file`
echo "Bookmark '$unbookmark_name' removed"
fi
fi
}
# Show a list of the bookmarks
bookmarksshow (){
cat ~/.bookmarks | awk '{ printf "%-40s%-40s%s\n",$1,$2,$3}' FS=\|
}
go(){
bookmark_name=$1
bookmark=`grep "|$bookmark_name$" "$bookmarks_file"`
if [[ -z $bookmark ]]; then
echo 'Invalid name, please provide a valid bookmark name. For example:'
echo ' go foo'
echo
echo 'To bookmark a folder, go to the folder then do this (naming the bookmark 'foo'):'
echo ' bookmark foo'
else
dir=`echo "$bookmark" | cut -d\| -f1`
cd "$dir"
fi
}
_go_complete(){
# Get a list of bookmark names, then grep for what was entered to narrow the list
cat $bookmarks_file | cut -d\| -f2 | grep "$2.*"
}
complete -C _go_complete -o default go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment