dotfiles/bash_functions
Dan Buch a8cd5a2fe9 Attempting to carve out a sensible chunk of stuff for a dotfiles repo.
Sure hope I didn't miss any super secret crap :-P
2012-02-26 14:38:01 -05:00

262 lines
5.2 KiB
Bash

function home_as_tilde(){
echo $1 | sed "s@^$HOME@~@"
}
function trunc_pwd(){
if [ "$PWD" == "$HOME" ]
then
echo "~"
else
parts=($(echo $PWD | tr " " "_" | tr "/" "\n"))
parts_len="${#parts[*]}"
max_parts="4"
if [ $parts_len -le $max_parts ]
then
home_as_tilde $PWD
else
startslice=$(expr $parts_len - $max_parts)
out=""
for p in ${parts[@]:$startslice}
do
out="$out$p/"
done
echo ">" `home_as_tilde "$out" | sed "s@/\\\$@@"`
fi
fi
}
function __available_projects(){
/bin/ls $HOME/src | tr " " "\n"
}
function __proj_activate_comp(){
cur=${COMP_WORDS[COMP_CWORD]}
if [ $COMP_CWORD -eq 1 ]
then
COMPREPLY=( $( compgen -W "$(__available_projects)" $cur ) )
fi
}
function __proj_deactivate_reactivate_comp(){
cur=${COMP_WORDS[COMP_CWORD]}
if [ -n "$VIRTUAL_ENV" ]
then
COMPREPLY=( $(basename "$VIRTUAL_ENV") )
return 0
elif [ -n "$PROJ" ]
then
COMPREPLY=( $(basename "$PROJ") )
return 0
else
COMPREPLY=( $cur )
fi
}
function a(){
source $HOME/.activate_proj "$@"
}
function d(){
source $HOME/.deactivate_proj "$@"
}
function r(){
d "$1"
a "$1"
}
complete -F __proj_activate_comp -o default a
complete -F __proj_deactivate_reactivate_comp -o default d r
_CRUFTY_CRAP=(
'.*\.pyc$'
'.*~$'
'.*\.[^.]*\.bak$'
)
function decruft(){
local here_files=( "$(/bin/ls $PWD)" )
for fname in ${here_files[*]}
do
for pattern in ${_CRUFTY_CRAP[*]}
do
if [ -n "$(echo $fname | grep -E "$pattern")" ]
then
[ -n "$DEBUG" ] && echo "removing $fname"
rm -f "$fname"
fi
done
done
}
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
command_not_found_handle(){
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
else
return 127
fi
}
fi
function add_ssh_keys()
{
if [[ "$1" == "all" ]]
then
for key in $__SSH_KEYS__
do
ssh-add $key
done
else
ssh-add
fi
}
function get_ssh_agent(){
ssh-add -l
test $? -eq 2 || return
agent_out="$HOME/.ssh/agent.out"
source $agent_out
ssh-add -l
test $? -eq 2 || return
ssh-agent > $agent_out
source $agent_out
ssh-add
}
function get_gpg_agent(){
agent_out="$HOME/.gnupg/agent.out"
tmp=$(mktemp /tmp/gnupgXXXX)
gpg-agent $* > $tmp && mv $tmp $agent_out
grep GPG_TTY $agent_out || \
cat >> $agent_out <<EOF
export GPG_TTY="$(tty)"
EOF
source $agent_out
}
function retag(){
langs="python"
if [ -n "$1" ] ; then
langs="$langs,$1"
fi
rm -f $PWD/tags
ctags --recurse=yes --languages=$langs $PWD
}
function top_commands(){
history | awk '{ print $2 }' | sort | uniq -c | sort -k1 -rn | head
}
function svn_clean(){
for f in `svn status | grep ^?` ; do
name="`echo $f | sed \"s/\? *//\"`" ;
name="`echo $name | sed \"s/ //g\"`" ;
if [ -n "$name" ] ; then
cmd="rm -rf $name ";
echo "# ---> $cmd"
eval $cmd
fi
done
}
function hg_clean(){
find -name \*.orig -exec rm -v {} \;
find -name \*.rej -exec rm -v {} \;
unknown="$(hg status | awk '/^\?/ { print $2 }')"
test -n "$unknown" && rm -v "$unknown"
}
function svn_conflicts(){
svn status | grep -E '^!? *C'
}
function rss_pid_command(){
cmd="ps -o rss,pid,command U $USER"
if [ -n "$1" ] ; then
grep_filter="grep $1"
$cmd | $grep_filter | grep -v "$grep_filter"
else
$cmd
fi
}
function precmd(){
PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;32m\]"
PS1="$PS1$(trunc_pwd)\[\033[00m\] "
local pcla="$(which print-color-loadavg 2>/dev/null)"
test -n "$pcla" && test -x $pcla && PS1="$PS1($(print-color-loadavg)) "
PS1="$PS1$(__git_ps1 "(%s)")\n\$ "
if [ -n "$1" ]
then
echo "$PS1"
else
export PS1
fi
}
function __git_branch_and_description(){
([[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]] || \
[[ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" == "true" ]]) && (
echo -n $(git branch 2>/dev/null | grep '*' | awk '{ print $2 }')
desc=$(cat $(git rev-parse --git-dir)/description | grep -v '^Unnamed repository')
test -n "$desc" && echo " \[\033[32m\]$desc\[\033[00m\]"
)
}
function make_pythonpath_local_only(){
export PYTHONPATH="$(
echo $PYTHONPATH | tr ":" "\n" | sort | uniq | \
grep -v -E '/(usr|var)' | grep -v '^$' | tr "\n" ":"
)"
}
function svn_list_unknown(){
svn st | awk '/^?/ { gsub(/^? */, "", $0) ; print $0 }'
}
function svn_add_unknown(){
svn add $(svn_list_unknown)
}
function unhyphenate(){
ruby -e "puts '$1'.gsub(/[\-_]/, ' ')"
}
# vim:filetype=sh