Skip to content

Instantly share code, notes, and snippets.

@josepanguera
Created October 26, 2019 06:25
Show Gist options
  • Save josepanguera/2f4c114fbed4eea9f9a5807d63b6702e to your computer and use it in GitHub Desktop.
Save josepanguera/2f4c114fbed4eea9f9a5807d63b6702e to your computer and use it in GitHub Desktop.
function cdb() {
USAGE="Usage: cdb [-c|-d|-l] [bookmark]" ;
DIR="$HOME/.config/cd_bookmarks"
if [ ! -e $DIR ] ; then
mkdir $DIR
fi
case $1 in
-c | --create) shift
if [ ! -f $DIR/$1 ] ; then
echo "cd `pwd`" > $DIR/"$1" ;
else
echo "Try again! Looks like there is already a bookmark '$1'"
fi
;;
-d | --delete) shift
if [ -f $DIR/$1 ] ; then
rm $DIR/"$1" ;
else
echo "Oops, forgot to specify the bookmark" ;
fi
;;
-l | --list) shift
ls -1 $DIR ;
;;
# goto bookmark
*)
if [ -f $DIR/$1 ] ; then
source $DIR/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted." ;
fi
;;
esac
}
_cdb_completion () {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
-*)
COMPREPLY=( $(compgen -W "--create --delete --list" -- ${cur}) )
;;
*)
COMPREPLY=( $(compgen -W "`cdb -l`" -- ${cur}) )
;;
esac
}
complete -o default -F _cdb_completion cdb
# http://www.linuxjournal.com/content/more-using-bash-complete-command
# http://www.serverwatch.com/server-tutorials/a-look-at-the-compgen-bash-builtin.html
# https://sixohthree.com/867/bash-completion
# http://www.gnu.org/software/bash/manual/bashref.html#Programmable-Completion
# /usr/bin/virtualenvwrapper.sh:235
# /etc/bash_completion.d/git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment