Skip to content

Instantly share code, notes, and snippets.

@victorfsf
Last active January 20, 2017 16:21
Show Gist options
  • Save victorfsf/3d2a875f1f96d6da3cc1f1a36ed905b9 to your computer and use it in GitHub Desktop.
Save victorfsf/3d2a875f1f96d6da3cc1f1a36ed905b9 to your computer and use it in GitHub Desktop.
# @victorfsf - custom cd v0.1
function cd() {
if [ "$1" == "-i" ]
then
shift 1
builtin cd "$@"
return 0
fi
builtin cd "$@" && gital
# For python's virtualenvwrapper "workon" command
type workon 2>/dev/null >/dev/null
if [ -d "$HOME/.virtualenvs" ] && [ $? -eq 0 ]
then
for fname in $HOME/.virtualenvs/*/.project
do
fname=${fname//\\\n/}
venvpath=`cat $fname`
if [ "$venvpath" == "`pwd`" ]
then
venv=`dirname $fname`
python -c 'import sys; print (sys.real_prefix)' >/dev/null 2>/dev/null
if [ ! $? -eq 0 ] || [ "$venv" != "$VIRTUAL_ENV" ]
then
workon `basename $venv`
fi
break
fi
done
fi
}
# @victorfsf - cdpath v0.5
function cdpath() {
local bold=`tput bold`
local green=`tput setaf 2`
local red=`tput setaf 1`
local reset=`tput sgr0`
local cdfile=$HOME/.cdpath
local shname=$(basename `/bin/ps -p $$ -oargs=`)
local shell="$HOME/."$shname"rc"
local usage="usage: cdpath [-h] [-r] [-u] [-l] [-i] <name> <path>\nSee \"cdpath -h\" for help."
local expstring="export CDPATH=\".\`cat \"\$HOME/.cdpath\" | sed ':a;N;\$!ba;s/\\\n//g'\`\""
if [ ! -f $shell ]
then
local shell="$HOME/.bashrc"
local shname="bash"
fi
if [[ "$1" =~ ^\-.* ]]
then
case ${1} in
-h)
echo "cdpath basic usage: \"cdpath <name> <path>\""
echo " name The path's shortcut, called with \"cd\""
echo -e " path The path to link the name with\n"
echo "cdpath options:"
echo " -h Shows help"
echo " -r Removes a path from cdpath" \
"(e.g. \"cdpath -r <name>\")"
echo " -l Lists all mapped paths"
echo " -i Installs cdpath"
echo " -u Uninstalls cdpath (use [-y] to skip input)"
;;
-u)
if [ "$2" != "-y" ]
then
echo -e -n "Are you sure you want to remove cdpath?\nAll" \
"your mapped paths will be lost! (y/N): "
read -r choice
if [ "$shname" == "zsh" ]
then
local choice=$choice:l
else
local choice=${choice,,}
fi
else
local choice="y"
fi
if [ "$choice" == "y" ]
then
echo "Uninstalling cdpath..."
rm -rf $cdfile
sed -i 's/source "$HOME\/.cdpath"//' $shell
sed -i '/^\s*$/d' $shell
echo "Done."
fi
;;
-r)
shift
if [ -z "$1" ]
then
echo "cdpath -r takes at least 1 argument (0 given)"
echo -e "\n$usage"
return 1
else
for var in "$@"
do
local remove=":$var.*"
cat $cdfile | grep "$remove" >/dev/null
if [ ! $? -eq 0 ]
then
echo "There's no path mapped to \"$var\""
return 1
else
sed -i "s/$remove//" $cdfile
sed -i '/^\s*$/d' $cdfile
export CDPATH=".:$var:"
echo "${bold}Successfully removed \"$var\""
fi
done
fi
;;
-l)
local paths=`sed 's/^://' $cdfile`
echo "Mapped paths:"
if [ -z $paths ]
then
echo " ${bold}${red}Nothind to show here${reset}"
echo -e "\n$usage"
else
local paths=(`echo ${paths//\"/}`)
for item in $paths
do
local item=(`echo ${item//:/ }`)
if [ -z "${item[0]}" ]
then
local i=1
else
local i=0
fi
echo " ${green}${bold}${item[$i]}" \
"${reset}->" \
"${bold}${item[`expr $i + 1`]}${item[$i]}/${reset}"
done
fi
;;
-i)
cat $shell | grep $expstring 2>/dev/null >/dev/null
if [ $? -eq 1 ]
then
echo 'Installing cdpath...'
if [ ! -f $cdfile ]
then
touch $cdfile
fi
echo $expstring >> $shell
echo "cdpath successfully installed!"
fi
;;
*)
echo "Unknown option: $1"
echo -e "\n$usage"
return 1
;;
esac
elif [ $# == 0 ]
then
echo -e $usage
elif [ $# != 2 ]
then
echo "cdpath takes exactly 2 arguments ($# given)"
echo -e "\n$usage"
return 1
else
if [ ! -f "$cdfile" ]
then
touch "$cdfile"
fi
if [ "$2" == "." ]
then
local dest="`pwd`/"
elif [[ "$2" == *"/" ]]
then
local dest="$2"
else
local dest="$2/"
fi
local baseDestDir=`basename $dest`
if [ "$baseDestDir" == "$1" ]
then
local dest="`dirname $dest`/"
fi
if [[ "$dest" =~ "^[^/].*" ]]
then
local dest="`pwd`/$dest"
fi
if [ ! -d "$dest$1" ]
then
echo "No such directory: $dest$1"
return 1
fi
sed -i 's/:'$1'.*//' $cdfile
sed -i '/^\s*$/d' $cdfile
if [ -z "$CDPATH" ]
then
export CDPATH=".:$1:$dest"
else
export CDPATH="$CDPATH:$1:$dest"
fi
echo ":$1:$dest" >> $cdfile
sed -i '/^\s*$/d' $cdfile
cat $shell | grep $expstring 2>/dev/null >/dev/null
if [ $? -eq 1 ]
then
echo $expstring >> $shell
fi
fi
}
# @victorfsf - django's manage.py alias/finder v0.3
function dj() {
workdir=`pwd`
while [ ! -f "$workdir/manage.py" ] && [ "$workdir" != "/" ]
do
workdir=`dirname "$workdir"`
done
if [ -f "$workdir/manage.py" ]
then
if [[ "$1" == "@"* ]]
then
project=`basename $workdir`
settings="$project.settings.${1:1}"
shift 1
python $workdir/manage.py $@ --settings=$settings
else
python $workdir/manage.py $@
fi
else
echo "fatal: Not a django project" \
"(or any of the parent directories): manage.py"
return 128
fi
}
# @victorfsf - gital v0.6
function gital() {
declare -A aliasmap
aliasmap=(
"add" "git add"
"am" "git am"
"apply" "git apply"
"archimport" "git archimport"
"archive" "git archive"
"bisect" "git bisect"
"blame" "git blame "
"branch" "git branch"
"catf" "git cat-file"
"check-attr" "git check-attr"
"check-ignore" "git check-ignore"
"check-mailmap" "git check-mailmap"
"check-ref-format" "git check-ref-format"
"checkout-index" "git checkout-index"
"checkout" "git checkout"
"cherry-pick" "git cherry-pick"
"cherry" "git cherry"
"citool" "git citool"
"clean" "git clean"
"clone" "git clone"
"commit-tree" "git commit-tree"
"commit" "git commit"
"config" "git config"
"count-objects" "git count-objects"
"cvsexportcommit" "git cvsexportcommit"
"cvsimport" "git cvsimport"
"cvsserver" "git cvsserver"
"daemon" "git daemon"
"describe" "git describe"
"diff-files" "git diff-files"
"diff-index" "git diff-index"
"diff-tree" "git diff-tree"
"difftool" "git difftool"
"fast-export" "git fast-export"
"fast-import" "git fast-import"
"fetch-pack" "git fetch-pack"
"fetch" "git fetch"
"filter-branch" "git filter-branch"
"fmt-merge-msg" "git fmt-merge-msg"
"for-each-ref" "git for-each-ref"
"format-patch" "git format-patch"
"fsck" "git fsck"
"gbdl" "git bundle"
"gdf" "git diff"
"get-tar-commit-id" "git get-tar-commit-id"
"ggc" "git gc"
"ggrep" "git grep"
"glog" "git log"
"gmv" "git mv"
"gnit" "git init"
"grm" "git rm"
"grt" "git reset"
"gst" "git status"
"gui" "git gui"
"hash-object" "git hash-object"
"help" "git help"
"http-backend" "git http-backend"
"http-fetch" "git http-fetch"
"http-push" "git http-push"
"imap-send" "git imap-send"
"index-pack" "git index-pack"
"instaweb" "git instaweb"
"ls-files" "git ls-files"
"ls-remote" "git ls-remote"
"ls-tree" "git ls-tree"
"mailinfo" "git mailinfo"
"mailsplit" "git mailsplit"
"merge-base" "git merge-base"
"merge-file" "git merge-file"
"merge-index" "git merge-index"
"merge-one-file" "git merge-one-file"
"merge-tree" "git merge-tree"
"merge" "git merge"
"mergetool" "git mergetool"
"mktag" "git mktag"
"mktree" "git mktree"
"name-rev" "git name-rev"
"notes" "git notes"
"pack-objects" "git pack-objects"
"pack-redundant" "git pack-redundant"
"pack-refs" "git pack-refs"
"parse-remote" "git parse-remote"
"patch-id" "git patch-id"
"prune-packed" "git prune-packed"
"prune" "git prune"
"pull" "git pull origin"
"push" "git push origin"
"quiltimport" "git quiltimport"
"read-tree" "git read-tree"
"rebase" "git rebase"
"receive-pack" "git receive-pack"
"reflog" "git reflog"
"relink" "git relink"
"remote" "git remote"
"repack" "git repack"
"replace" "git replace"
"request-pull" "git request-pull"
"rerere" "git rerere"
"rev-list" "git rev-list"
"rev-parse" "git rev-parse"
"revert" "git revert"
"send-email" "git send-email"
"send-pack" "git send-pack"
"shell" "git shell"
"shortlog" "git shortlog"
"show-branch" "git show-branch"
"show-index" "git show-index"
"show-ref" "git show-ref"
"show" "git show"
"stash" "git stash"
"stripspace" "git stripspace"
"submodule" "git submodule"
"svn" "git svn"
"symbolic-ref" "git symbolic-ref"
"tag" "git tag"
"unpack-file" "git unpack-file"
"unpack-objects" "git unpack-objects"
"update-index" "git update-index"
"update-ref" "git update-ref"
"update-server-info" "git update-server-info"
"upload-archive" "git upload-archive"
"upload-pack" "git upload-pack"
"var" "git var"
"verify-commit" "git verify-commit"
"verify-pack" "git verify-pack"
"verify-tag" "git verify-tag"
"whatchanged" "git whatchanged"
"write-tree" "git write-tree"
)
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
bold=`tput bold`
git status 2>/dev/null >/dev/null
if [ $? -eq 0 ]
then
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
for k in "${(@k)aliasmap}"
do
echo "${bold}${green}$k ${reset}-> ${bold}$aliasmap[$k]"
done
return
fi
if [ "$1" == "-v" ]
then
echo "${bold}Setting up git aliases:\n"
for k in "${(@k)aliasmap}"
do
echo " ${bold}${green}$k ${reset}-> ${bold}$aliasmap[$k]"
alias $k="$aliasmap[$k]"
done
echo "\n${bold}Done."
else
for k in "${(@k)aliasmap}"
do
alias $k="$aliasmap[$k]"
done
fi
else
if [ "$1" == "-v" ]
then
echo "${bold}Removing git aliases:\n"
for k in "${(@k)aliasmap}"
do
echo " ${bold}${red}$k ${reset}-\> ${bold}$aliasmap[$k]"
unalias $k 2>/dev/null >/dev/null
done
echo "\n${bold}Done."
else
for k in "${(@k)aliasmap}"
do
unalias $k 2>/dev/null >/dev/null
done
fi
fi
}
gital
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment