Skip to content

Instantly share code, notes, and snippets.

@stecman
Last active August 29, 2015 14:01
Show Gist options
  • Save stecman/de105938bc968ca750c8 to your computer and use it in GitHub Desktop.
Save stecman/de105938bc968ca750c8 to your computer and use it in GitHub Desktop.
Project folder switcher BASH function
# Change to a site directory
#
# A site might be anything, but this was written to jump to web roots.
# Minimum config is to change the WEB_DIR variable. Adapted for drop-in
# use from the `site` function originally by @pieterv for @heyday
#
# Usage: site <dir> [path-within-dir]
WEB_DIR=/var/www/
function site()
{
# Colour configs
local negativebg='\e[41m' # BG Red
local negativetxt='\e[0;31m' # Text Red
local pathtxt='\e[0;32m' # Text Green
local txtrst='\e[0m' # Text Reset
# Find dir
cd ~ && cd "$WEB_DIR"
if [ $1 ]; then
if [ -d "./$1" ]; then
cd "./$1"
name=$(basename $1)
printf "\n${negativebg}** $name${txtrst}\n"
if [ -d "./htdocs" ]; then
cd "./htdocs"
fi
if [ $2 ]; then
if [ -d "./$2" ]; then
cd "./$2"
fi
fi
else
printf "${pathtxt}~/Sites/$1${txtrst} is not a valid project folder.\n"
printf "${negativetxt}Projects:${txtrst}\n" && ls -d */
fi
else
printf "\n${negativebg}** $WEB_DIR${txtrst}\n"
fi
tput sgr0
}
# Bash completion for site function
function _list_sites()
{
local CUR_WORD=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( `cd $WEB_DIR; find . -maxdepth 1 -type d -name "$CUR_WORD*" | sed 's/^\.\///' | sed 's/^\.$//'` )
}
complete -F _list_sites site
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment