Skip to content

Instantly share code, notes, and snippets.

@fathergoose
Last active October 18, 2022 18:53
Show Gist options
  • Save fathergoose/3a33080f42df896bd4928a536876e284 to your computer and use it in GitHub Desktop.
Save fathergoose/3a33080f42df896bd4928a536876e284 to your computer and use it in GitHub Desktop.
Save "project" directories and navigate to them quickly
function pm() {
local pfile=$HOME/.projects
if [[ ! -f $pfile ]]; then
touch $pfile
fi
local usage="Usage: pm [-a|-d|-l|-h] [key] [path]"
# List projects
if [[ $1 == "-l" ]]; then
echo -e "\e[4m\e[1mKey\e[0m\t\e[4m\e[1mPath\e[0m"
awk '{print " " $1 "\t" $2}' $pfile
# Add project
elif [[ $1 == "-a" && -d $3 && -n $2 ]]; then
pkey=$2
ppath=$3
echo "$pkey $ppath" >> $pfile
# Remove project
elif [[ $1 == "-d" ]]; then
# sed -i '/^'$2'\s/d' $pfile # GNU sed
sed -i "" "/^$2 /d" $pfile # BSD (macOS) sed
elif [[ $1 == "-h" ]]; then
echo $usage
echo " -l: list projects"
echo " -a: add project"
echo " -d: delete project"
echo " -h: help"
echo "Examples:"
echo " pm <key> - cd to project"
echo " pm -a <key> <path> - add project"
elif [[ ${1:0:1} == '-' ]]; then
echo "Invalid option: $1\n"
echo $usage
elif [[ -n $1 ]]; then
# Jump to project
ppath=$(awk '$1 == "'$1'" {print $2}' $pfile)
if [[ -n $ppath ]]; then
cd $ppath
else
echo "Project not found: $1"
fi
else
echo $usage
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment