Skip to content

Instantly share code, notes, and snippets.

@dmukhg
Forked from DHowett/.zshrc
Created March 29, 2012 07:32
Show Gist options
  • Save dmukhg/2234557 to your computer and use it in GitHub Desktop.
Save dmukhg/2234557 to your computer and use it in GitHub Desktop.
Shell Bookmarks for ZSH
# Instructions
# << command >> : << what it does >>
# l : lists all bookmarks
# s <name> : save current directory as <name>
# g <name> : go to bookmark <name>
# d <name> : delete bookmark <name>
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
function m() {
# mark this directory to be opened each time a new terminal is
# spawned.
printf `pwd` > ~/.mark_dir
echo 'Directory marked'
}
function u() {
# unmark directory and open the ~ directory each time a new terminal
# is spawned.
printf ~ > ~/.mark_dir
echo 'Directory Mark reset to home'
cd ~
}
# The real cd function.
read MARKDIR < ~/.mark_dir
cd $MARKDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment