From a8360d38e149069a545129cc271fcdc5072a64b9 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 27 Feb 2014 11:52:52 -0500 Subject: [PATCH] reorganize bash_aliases; use shell built-ins when possible; use cd hooks instead of PROMPT_COMMAND for actions to be performed when changing directories --- bash_aliases | 204 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 133 insertions(+), 71 deletions(-) diff --git a/bash_aliases b/bash_aliases index 7bddcfe..7bd0d2a 100755 --- a/bash_aliases +++ b/bash_aliases @@ -1,37 +1,17 @@ # vim:syntax=sh -if [[ "`whoami`" == "root" ]]; then + +########################################################################### +# PS1 +########################################################################### + +if [[ "${USER}" == "root" ]]; then PS1='\[\033[31;1m\]\u@\H\[\033[32;1m\] [\w]\[\033[35;1m\] \d \t \[\033[34;1m\](\j)\n\$ \[\033[0m\]' else PS1='\[\033[32;1m\]\u@\H\[\033[31;1m\] [\w]\[\033[35;1m\] \d \t \[\033[34;1m\](\j)\[\033[33;1m\]$(prompt_ps1_git_branch)$(prompt_ps1_svn_branch)\[\033[34;1m\]\n\$ \[\033[0m\]' fi # alternate PS1: # PS1='[\[\033[31;1m\]\u@\H\[\033[34;1m\] \w\[\033[0m\]]\$ \[\033[0m\]' -case "$TERM" in -[ax]term*|rxvt*) - PROMPT_COMMAND='echo -ne "\033]0;"$(basename "$PWD")" [${USER}@${HOSTNAME}: ${PWD}]\007"' - ;; -*) - PROMPT_COMMAND='' - ;; -esac -alias grep='grep --color=auto' -alias grepnosvn='grep --color=auto --exclude-dir=".svn"' -alias egrepnosvn='egrep --color=auto --exclude-dir=".svn"' -function gvim() -{ - arg="$1" - if [ "${arg}" = "" ]; then - command gvim - else - command gvim --remote-tab-silent "$@" - fi -} -alias cribbage='cribbage -r' -alias backgammon='backgammon -r -pb' -# put 'cattodo' in $PROMPT_COMMAND to use -alias cattodo='if [[ $CATTODO_LAST_WD != $PWD ]]; then if [[ -r .todo ]]; then echo TODO:; cat .todo; fi; CATTODO_LAST_WD=$PWD; fi' -alias cdshowgitstatus='if [[ $CDSHOWGITSTATUS_LAST_WD != $PWD ]]; then if [[ -e .git ]]; then git status; fi; CDSHOWGITSTATUS_LAST_WD=$PWD; fi' -PROMPT_COMMAND="cdshowgitstatus;$PROMPT_COMMAND" + function prompt_ps1_git_branch() { if [ -e .git ]; then @@ -50,7 +30,7 @@ function prompt_ps1_git_branch() fi fi } -#put $(prompt_ps1_git_branch) in $PS1 to use it + function prompt_ps1_svn_branch() { if [ -e .svn ]; then @@ -64,6 +44,70 @@ function prompt_ps1_svn_branch() fi fi } + +########################################################################### +# cd hooks +########################################################################### + +function cd() { command cd "$@"; cd_hook; } +function pushd() { command pushd "$@"; cd_hook; } +function popd() { command popd "$@"; cd_hook; } + +case "$TERM" in +[ax]term*|rxvt*) + function cd_hook_change_terminal_title() + { + echo -ne "\033]0;"$(basename "$PWD")" [${USER}@${HOSTNAME}: ${PWD}]\007" + } + ;; +*) + function cd_hook_change_terminal_title() + { + : + } + ;; +esac +function cd_hook_cat_todo() +{ + if [[ -r .todo ]]; then + echo TODO: + sed -re 's/^/ /' .todo + fi +} +function cd_hook_show_git_status() +{ + if [[ -e .git ]]; then + git status + fi +} +function cd_hook() +{ + if [[ "${cd_hook_last_wd}" != "${PWD}" ]]; then + cd_hook_cat_todo + cd_hook_show_git_status + cd_hook_change_terminal_title + cd_hook_last_wd="${PWD}" + fi +} + +########################################################################### +# command aliases/wrappers +########################################################################### + +alias grep='grep --color=auto' +alias grepnosvn='grep --color=auto --exclude-dir=".svn"' +alias egrepnosvn='egrep --color=auto --exclude-dir=".svn"' +function gvim() +{ + arg="$1" + if [ "${arg}" = "" ]; then + command gvim + else + command gvim --remote-tab-silent "$@" + fi +} +alias cribbage='cribbage -r' +alias backgammon='backgammon -r -pb' alias ls='ls --color=auto' alias strip-cr="sed -e 's/\x0d//'" alias rip='abcde -x -p -o mp3:"-v -b160"' @@ -90,10 +134,64 @@ function cco() fi catch_compare_output="$catch_compare_output_new" } +function git-config-joshs() +{ + git config --global user.name 'Josh Holtrop' + git config --global color.ui true + git config --global color.diff.meta yellow + git config --global core.excludesfile ${HOME}/.gitignore + git config --global core.pager 'less -FRXi' + git config --global alias.dc 'diff --cached' + # from http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs/9074343#9074343 + git config --global alias.lg 'log --graph --abbrev-commit --decorate --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) - %C(magenta)%an%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)" --all' + git config --global alias.mergef 'merge FETCH_HEAD' + git config --global alias.gdiff 'difftool -y -t gvimdiff' + git config --global alias.gdiffc 'difftool -y -t gvimdiff --cached' + git config --global alias.wdiff 'diff --word-diff=color' + git config --global alias.mktar '!function f { name="$1"; pos="$2"; if [ "$pos" == "" ]; then pos=HEAD; fi; git archive --prefix="$name"/ "$pos" | bzip2 > ../"$name".tar.bz2; }; f' + git config --global alias.mktarxz '!function f { name="$1"; pos="$2"; if [ "$pos" == "" ]; then pos=HEAD; fi; git archive --prefix="$name"/ "$pos" | xz > ../"$name".tar.xz; }; f' + git config --global push.default upstream + if [ -e /bin/cygwin1.dll ]; then + git config --global alias.bcdiff 'difftool -y -t bc3' + git config --global alias.bcdiffc 'difftool -y -t bc3 --cached' + git config --global difftool.bc3.cmd 'git_bc3diff "$LOCAL" "$REMOTE"' + git config --global alias.bcmerge 'mergetool -y -t bc3' + git config --global mergetool.bc3.cmd \ + 'git_bc3merge "$LOCAL" "$REMOTE" "$MERGED"' + git config --global mergetool.bc3.trustExitCode false + fi +} +function git-config-local-personal() +{ + local domain='gmail.com' + git config user.email 'jholtrop@'${domain} +} +alias git-find-lost-commit='git fsck --lost-found' +if [[ "$(which jsvn 2>/dev/null)" != "" ]]; then + alias svn='jsvn' +fi +alias jindent='indent -bbo -bl -blf -bli0 -bls -i4 -npcs -nut -ts8' + +########################################################################### +# environment configuration +########################################################################### + export LESS='Ri' +export EDITOR=vim + +########################################################################### +# bash configuration +########################################################################### + HISTCONTROL='ignoreboth' HISTSIZE=5000 HISTFILESIZE=${HISTSIZE} +unset PROMPT_COMMAND + +########################################################################### +# mark +########################################################################### + function mark() { local MARKS_FILE=${HOME}/.marks @@ -149,51 +247,15 @@ function mark() ;; esac } -export EDITOR=vim -function git-config-joshs() -{ - git config --global user.name 'Josh Holtrop' - git config --global color.ui true - git config --global color.diff.meta yellow - git config --global core.excludesfile ${HOME}/.gitignore - git config --global core.pager 'less -FRXi' - git config --global alias.dc 'diff --cached' - # from http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs/9074343#9074343 - git config --global alias.lg 'log --graph --abbrev-commit --decorate --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) - %C(magenta)%an%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)" --all' - git config --global alias.mergef 'merge FETCH_HEAD' - git config --global alias.gdiff 'difftool -y -t gvimdiff' - git config --global alias.gdiffc 'difftool -y -t gvimdiff --cached' - git config --global alias.wdiff 'diff --word-diff=color' - git config --global alias.mktar '!function f { name="$1"; pos="$2"; if [ "$pos" == "" ]; then pos=HEAD; fi; git archive --prefix="$name"/ "$pos" | bzip2 > ../"$name".tar.bz2; }; f' - git config --global alias.mktarxz '!function f { name="$1"; pos="$2"; if [ "$pos" == "" ]; then pos=HEAD; fi; git archive --prefix="$name"/ "$pos" | xz > ../"$name".tar.xz; }; f' - git config --global push.default upstream - if [ -e /bin/cygwin1.dll ]; then - git config --global alias.bcdiff 'difftool -y -t bc3' - git config --global alias.bcdiffc 'difftool -y -t bc3 --cached' - git config --global difftool.bc3.cmd 'git_bc3diff "$LOCAL" "$REMOTE"' - git config --global alias.bcmerge 'mergetool -y -t bc3' - git config --global mergetool.bc3.cmd \ - 'git_bc3merge "$LOCAL" "$REMOTE" "$MERGED"' - git config --global mergetool.bc3.trustExitCode false - fi -} -function git-config-local-personal() -{ - local domain='gmail.com' - git config user.email 'jholtrop@'${domain} -} -alias git-find-lost-commit='git fsck --lost-found' -if [[ "$(which jsvn 2>/dev/null)" != "" ]]; then - alias svn='jsvn' -fi -alias jindent='indent -bbo -bl -blf -bli0 -bls -i4 -npcs -nut -ts8' -# cygwin-specific aliases -if [ -e /bin/cygwin1.dll ]; then +########################################################################### +# cygwin-specific +########################################################################### +if [[ -e /bin/cygwin1.dll ]]; then alias ip="ipconfig | grep -E 'IP(v4)? Address' | sed -e 's/.*: //'" function cs { - while [ "$1" != "" ] + while [[ "$1" != "" ]] do dn=$(dirname "$1") bn=$(basename "$1") @@ -214,11 +276,11 @@ if [ -e /bin/cygwin1.dll ]; then fi # source any machine-local aliases -# this way ~/.bash_aliases can be a symlink to a version-controlled -# aliases file +# this way ~/.bash_aliases can be shared in many places if [ -f ~/.bash_aliases.local ]; then . ~/.bash_aliases.local fi + # source any alias files in ~/.bash_aliases.d, # or within a subdirectory thereof # this allows multiple alias files or repositories of alias files