Skip to content

Instantly share code, notes, and snippets.

@nkcmr
Last active March 6, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkcmr/6d4e5c21d73c433d79547de7ba188815 to your computer and use it in GitHub Desktop.
Save nkcmr/6d4e5c21d73c433d79547de7ba188815 to your computer and use it in GitHub Desktop.
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