Last active
March 6, 2019 15:54
-
-
Save nkcmr/6d4e5c21d73c433d79547de7ba188815 to your computer and use it in GitHub Desktop.
goto.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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