Skip to content

Instantly share code, notes, and snippets.

@DHowett
Forked from rpetrich/.bash_profile
Created February 20, 2011 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DHowett/835531 to your computer and use it in GitHub Desktop.
Save DHowett/835531 to your computer and use it in GitHub Desktop.
Shell Bookmarks for ZSH
function _uh { [[ -z $DEV ]]; return $? }
function userroot {
if ! _uh; then echo $DEV;
else echo $HOME; fi
}
function _sdirname {
echo "$(userroot)/.sdirs"
}
function _sglobalname {
echo "${HOME}/.sdirs"
}
function _ss_for_arg {
local d n;
if [[ -z $1 ]]; then return; fi
if [[ "${1[0,1]}" == "+" ]]; then d="$(_sglobalname)"; n="${1[2,-1]}";
else d="$(_sdirname)"; n="$1"; fi
echo "local sdir arg; sdir=\"$d\"; arg=\"$n\";"
}
# Shell Bookmarks (inspired by rpetrich.)
function dbg {
if [[ -z $1 ]]; then return; fi
eval $(_ss_for_arg $1);
echo $sdir;
echo $arg;
}
function s {
if [[ -z $1 ]]; then return; fi
eval $(_ss_for_arg $1);
if [[ -f "$sdir" ]]; then
grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1"
mv "${sdir}1" "${sdir}"
fi
echo "export _BOOKMARK_$arg=$PWD" >> "${sdir}"
}
function l {
if [[ -s $(_sdirname) ]]; then
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1 (\2)/g' "$(_sdirname)" | sort
fi
if ! _uh; then
if [[ -s $(_sglobalname) ]]; then
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1 (\2)/g' "$(_sglobalname)" | sort
fi
fi
}
function g {
eval $(_ss_for_arg $1)
. "$sdir"
cd $(eval echo $(echo \$_BOOKMARK_$arg))
}
function d {
if [[ -z $1 ]]; then return; fi
eval $(_ss_for_arg $1)
if [[ -f "$sdir" ]]; then
grep -v "^export _BOOKMARK_$arg" "${sdir}" > "${sdir}1"
mv "${sdir}1" "${sdir}"
fi
}
function _gcomp {
local descs globals;
descs=();
globals=();
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/\1:\2/g' "$(_sdirname)" | sort | while read line; do
descs[$(($#descs+1))]="$line"
done
if ! _uh; then
sed -e 's/^export _BOOKMARK_\([^=]*\)=\(.*\)$/+\1:\2/g' "$(_sglobalname)" | sort | while read line; do
globals[$(($#globals+1))]="$line"
done
fi
_describe -t bookmarks "shell bookmark" descs; _describe -t bookmarks "global bookmark" globals
}
compdef _gcomp g
@burningTyger
Copy link

Hi, this would be a nice replacement for bashmarks but I have a strange behaviour that I don't really like. When g-ing to a dir my prompt looks like this:

me@home:~DIR_bookmarkname

So instead of giving me a path all I get is the bookmark name with a DIR prefix. Any way to get the real path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment