zsh-goto/goto.plugin.zsh

50 lines
935 B
Bash
Raw Normal View History

2016-09-30 17:19:41 +00:00
# goto.zsh
2017-04-18 00:29:17 +00:00
function _awk {
which awk &>/dev/null && awk $@ || \
which gawk &>/dev/null && gawk $@
}
2016-09-30 17:19:41 +00:00
function _gotofile {
2017-04-18 00:29:17 +00:00
echo $GOTO_FILE "$HOME/.labels.tsv" | _awk "{print \$1}"
2016-09-30 17:19:41 +00:00
}
function _makeLabel {
printf "%s %s\n" $1 $2 >> `_gotofile`
}
function label {
if [ $# -eq 0 ]
then
echo "Usage: $ label <name> [dir]"
echo " creates a label which goto can cd to"
elif [ -d "$2" ]
then
_makeLabel $1 $2
else
_makeLabel $1 $PWD
fi
}
function goto {
if [ $# -eq 0 ]
then
echo "Usage: $ goto <name>"
echo " jumps to a record set by label"
elif [[ "$1" == "ls" ]]
then
awk "{ print \$1 }" `_gotofile` | column -t
else
cd $(awk "/^$1\s/ {print \$2;exit;}" `_gotofile`)
fi
}
function _goto {
2017-04-18 00:29:17 +00:00
for label in $(_awk '{print $1}' `_gotofile`)
2016-09-30 17:19:41 +00:00
do
compadd "$@" $label
done
};
compdef _goto goto