Skip to content

Instantly share code, notes, and snippets.

@nkcmr

nkcmr/goto.sh

Last active Mar 6, 2019
Embed
What would you like to do?
goto.sh
#!/bin/bash
goto() {
# create the shortcuts dir if necessary
if [ ! -d "$HOME/.shortcuts" ] ; then
mkdir -p "$HOME/.shortcuts"
fi
if [[ "$1" == "--list" ]] ; then
for f in "$HOME/.shortcuts/"* ; do
echo "$(basename "$f") -> $(tr -d '\n' < "$f")"
done
return 1
elif [[ "$1" == "--define" ]] ; then
if [[ "$2" == "" ]] ; then
echo "usage: goto --define [shortcut name] [target directory]"
return 1
else
rm -f "$HOME/.shortcuts/$2"
echo -n "$3" > "$HOME/.shortcuts/$2"
return 0
fi
elif [[ "$1" == "--rm" ]] ; then
rm -f "$HOME/.shortcuts/$2"
return 0
else
if [ -s "$HOME/.shortcuts/$1" ] ; then
GTDIR=$(cat "$HOME/.shortcuts/$1")
cd "$GTDIR" || return 1
else
echo "goto: unknown shortcut '$1'"
return 1
fi
fi
return 0
}
_goto_completions() {
# create the shortcuts dir if necessary
if [ ! -d "$HOME/.shortcuts" ] ; then
mkdir -p "$HOME/.shortcuts"
fi
for f in "$HOME/.shortcuts/"* ; do
COMPREPLY+=("$(basename "$f")")
done
}
complete -F _goto_completions goto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment