dot/bashrc: Show Git branch name in the prompt, if machinery available.
[profile] / dot / bashrc
CommitLineData
ca7933c9 1### -*-bash-*-
59c9c0e4
MW
2###
3### Bash session things
f617db13 4
59c9c0e4
MW
5## Only do this if we haven't done it before. (Note that this guard isn't
6## exported, so subshells will need to make their own arrangements.)
f617db13 7if [ -z "$__mdw_bashrc" ]; then
f617db13
MW
8__mdw_bashrc=done
9
59c9c0e4
MW
10## If we've not run the main profile yet, we should do that first. It sets
11## up things we rely on. Also, if there's a system script, we should run
12## that too.
f617db13
MW
13[ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14[ -r /etc/bashrc ] && . /etc/bashrc
15
5e662c38
MW
16## Completion.
17[ -r /etc/bash_completion ] && . /etc/bash_completion
18[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
19
59c9c0e4
MW
20## Set the temporary directory again. (If we've switched users, we'll want a
21## different temporary directory.)
22[ "${TMPDIR+yes}" ] || eval `tmpdir -b`
23
24###--------------------------------------------------------------------------
25### Prompt hacking.
f617db13 26
59c9c0e4 27## Only bother if the shell is interactive.
f617db13
MW
28if [ -t 0 ]; then
29
59c9c0e4 30 ## Fancy highlighting in some terminals.
f0ef025e 31 marker=${STY+'\[\ek\e\\\]'}
15b95c26
MW
32 case "$TERM" in
33 linux*|screen*|xterm*|vt100*|eterm*)
59c3438f
MW
34 case "$(tput bold)" in
35 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
36 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
37 esac
1273afa0 38 gitcolour="\[$(tput setaf 6)\]"
f00ae4fa
MW
39 rccolour="\[$(tput setaf 1)\]"
40 uncolour="\[$(tput op)\]"
59c3438f
MW
41 nl="\[\r\]"
42 ;;
15b95c26 43 *)
1273afa0 44 bold='' unbold='' nl='' gitcolour='' rccolour='' uncolour='';;
15b95c26
MW
45 esac
46
59c9c0e4
MW
47 ## Choose the right delimiters. Highlight root prompts specially;
48 ## highlight when I'm running as some other user. Highlight when this
49 ## isn't the outermost shell on the terminal.
15b95c26 50 if (( EUID == 0 )); then
2b5d4580
MW
51 left=`echo « | iconv -f UTF-8 -t //translit`
52 right=`echo » | iconv -f UTF-8 -t //translit`
f617db13 53 else
15b95c26 54 case $USER in
08404785 55 mdw|mwooding|nemo) u="" left="[" right="]" ;;
59c9c0e4 56 *) u="\\u@" left="{" right="}" ;;
f617db13 57 esac
15b95c26
MW
58 if [ "$__mdw_tty" = "`tty`" ]; then
59 left="<" right=">"
f617db13 60 else
15b95c26 61 export __mdw_tty="`tty`"
f617db13 62 fi
15b95c26 63 fi
f617db13 64
59c9c0e4 65 ## If this session is insecure then highlight that.
15b95c26
MW
66 if [ -z "$SSH_CLIENT" ] &&
67 [ "$__mdw_sechost" != "`hostname`" ]
68 then
69 sec_l='(' sec_r=')'
f617db13
MW
70 fi
71
59c9c0e4 72 ## Build the prompt string.
1273afa0
MW
73 if type __git_ps1 >/dev/null 2>&1; then
74 git="$unbold$gitcolour\$(rc=\$?;__git_ps1;exit \$rc)$uncolour$bold"
75 else
76 git=""
77 fi
f00ae4fa
MW
78 rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
79 rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
1273afa0 80 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$git$rc$marker$right$unbold"
15b95c26 81 PS2="$PS1 $bold>$unbold "
bb5c6b7e 82 unset nl bold unbold left right sec_l sec_r marker
1273afa0 83 unset gitcolour rccolour uncolour git rc
59c9c0e4 84fi
15b95c26 85
59c9c0e4
MW
86###--------------------------------------------------------------------------
87### Other shell tweaking.
f617db13 88
59c9c0e4 89## Random shell tweaks.
f617db13
MW
90notify=1
91set -b
92shopt -u cdable_vars
93shopt -s cdspell
94shopt -s checkhash
95shopt -s checkwinsize
96shopt -s cmdhist
97shopt -u dotglob
98shopt -s expand_aliases
99shopt -s extglob
08404785 100if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
83a3b1eb 101shopt -s gnu_errfmt
f617db13
MW
102shopt -s histappend
103shopt -s histreedit
104shopt -u histverify
105shopt -s hostcomplete
106shopt -s huponexit
107shopt -s interactive_comments
108shopt -s lithist
109shopt -u mailwarn
110shopt -u nocaseglob
111shopt -u nullglob
112shopt -s promptvars
113shopt -u shift_verbose
114shopt -s sourcepath
83a3b1eb
MW
115HISTCONTROL=ignorespace:erasedups
116
59c9c0e4
MW
117## Some handy aliases.
118alias cx='chmod a+x'
119alias which="command -v"
120alias rc="rc -l"
121alias ssync="rsync -e ssh"
ac03a1f6
MW
122rootly () {
123 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
124 $__MDW_ROOTLY "$@"
125}
59c9c0e4
MW
126alias r=rootly
127alias re="rootly $EDITOR"
128alias pstree="pstree -hl"
129alias cdtmp='cd ${TMPDIR-/tmp}'
130alias pushtmp='pushd ${TMPDIR-/tmp}'
131alias e="$EDITOR"
132alias svn="svnwrap svn"
133alias @="ssh"
f617db13 134
59c9c0e4
MW
135###--------------------------------------------------------------------------
136### Colour output.
137
138## Arrange for `ls' output to be in colour.
15b95c26 139if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
f617db13
MW
140 eval `dircolors -b ~/.dircolors`
141else
142 unset LS_COLORS
143fi
144
08404785 145unalias ls 2>/dev/null || :; function ls () {
f617db13
MW
146 if [ -t 1 ]; then
147 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
148 else
149 command ls "$@"
150 fi
151}
152
59c9c0e4 153## Arrange for `grep' output to be in colour.
bf3f5761
MW
154export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
155
004beb57
MW
156greplike () {
157 declare grep=$1; shift
bf3f5761 158 if [ -t 1 ]; then
a085e9a8 159 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
bf3f5761 160 else
004beb57 161 command $grep "$@"
bf3f5761
MW
162 fi
163}
004beb57
MW
164alias grep="greplike grep"
165alias egrep="greplike egrep"
166alias fgrep="greplike fgrep"
4da2e817 167alias zgrep="greplike zgrep"
bf3f5761 168
59c9c0e4
MW
169## Turn off pagers inside Emacs shell buffers.
170case "$INSIDE_EMACS" in
a7676eeb 171 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
59c9c0e4 172esac
75ff64d8 173
59c9c0e4
MW
174###--------------------------------------------------------------------------
175### More complicated shell functions.
f617db13 176
59c9c0e4
MW
177## xt [@HOST] XTERM-ARGS
178##
179## Open a terminal, maybe on a remote host.
f617db13
MW
180xt () {
181 case "$1" in
182 @*)
183 local remote=${1#@} title
184 shift
185 if [ $# -gt 0 ]; then
186 title="xterm [$remote] $1"
187 else
188 title="xterm [$remote]"
189 fi
190 (xterm -title "$title" -e ssh $remote "$@" &)
191 ;;
192 *)
193 (xterm "$@" &)
194 ;;
195 esac
196}
197
59c9c0e4
MW
198## core [y|n]
199##
200## Tweak core dumps on and off, or show the current status.
f617db13
MW
201core () {
202 case "x$1" in
59c9c0e4
MW
203 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
204 xoff|xn|xno) ulimit -Sc 0 ;;
f617db13
MW
205 x)
206 local l=`ulimit -Sc`
207 case $l in
208 0) echo "Core dumps disabled" ;;
209 unlimited) echo "Core dumps enabled" ;;
210 *) echo "Core dump limit is $l blocks" ;;
211 esac
212 ;;
213 *)
59c9c0e4 214 echo >&2 "usage: core [y|n]"
f617db13
MW
215 return 1
216 ;;
217 esac
218}
219
59c9c0e4
MW
220## world [NAME]
221##
222## Set current security world to NAME. With no NAME, print the currently
223## selected world.
224world () {
225 local nfast=${NFAST_HOME-/opt/nfast}
226 local kmdata
227 case "$#" in
228 0)
229 echo "${NFAST_KMDATA#$nfast/kmdata-}"
230 ;;
231 *)
232 if [ -d "$1" ]; then
233 kmdata=$1
234 elif [ -d "$nfast/kmdata-$1" ]; then
235 kmdata=$nfast/kmdata-$1
236 else
237 echo >&2 "world: can't find world $1"
238 return 1
239 fi
240 shift
241 case "$#" in
242 0) export NFAST_KMDATA=$kmdata ;;
243 *) "$@" ;;
244 esac
245 ;;
246 esac
247}
248
249## Fix `man' under Slowaris.
250case "$MACHTYPE" in
251 *solaris*)
252 man () {
253 declare -i i=0
254 declare arg
255 declare -a man
256 for arg; do
257 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
258 man[i+=1]="$arg"
259 done
260 command man "${man[@]}"
261 }
262 ;;
263esac
264
265###--------------------------------------------------------------------------
266### Path hacks.
32f790cc 267
59c9c0e4
MW
268## path-add [VAR] DIR
269##
270## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
271## it's not there already.
32f790cc
MW
272path-add () {
273 local pathvar export dir val
274 case $# in
275 1) pathvar=PATH dir=$1 export="export PATH";;
276 2) pathvar=$1 dir=$2 export=:;;
277 *) echo >&2 "Usage: $0 [VAR] DIR";;
278 esac
279 eval "val=\$$pathvar"
280 case ":$val:" in
281 *:"$dir":*) ;;
282 *) val=$dir:$val ;;
283 esac
284 eval "$pathvar=\$val"
285 $export
286}
287
59c9c0e4
MW
288## path-remove [VAR] DIR
289##
290## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
291## error if DIR isn't in VAR.
32f790cc
MW
292path-remove () {
293 local pathvar export dir val
294 case $# in
295 1) pathvar=PATH dir=$1 export="export PATH";;
296 2) pathvar=$1 dir=$2 export=:;;
297 *) echo >&2 "Usage: $0 [VAR] DIR";;
298 esac
299 eval "val=\$$pathvar"
300 case ":$val:" in
301 :"$dir":) val= ;;
302 :"$dir":*) val=${val#$dir:} ;;
303 *:"$dir":) val=${val%:$dir} ;;
304 *:"$dir":*) val=${val/:$dir:/:} ;;
305 esac
306 eval "$pathvar=\$val"
307 $export
308}
309
59c9c0e4
MW
310## pathhack [-f] +HACK|-HACK...
311##
312## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
313## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
314## that's already on the PATH doesn't do anything unless `-f' is set, in
315## which case it gets moved to the beginning. With no arguments, print the
316## currently installed hacks.
32f790cc
MW
317pathhack () {
318 if [ $# -eq 0 ]; then
319 local IFS=:
320 for e in $PATH; do
321 case "$e" in
322 "$HOME/bin/hacks/"*)
323 echo ${e#$HOME/bin/hacks/}
324 ;;
325 esac
326 done
327 return
328 fi
329 local force=nil
330 local path=$PATH
331 while [ $# -gt 0 ]; do
332 arg=$1
333 case "$arg" in
334 -f | --force)
335 force=t
336 shift
337 continue
338 ;;
339 --)
852cd5fb 340 shift
32f790cc
MW
341 break
342 ;;
343 [-+]*)
852cd5fb 344 ;;
32f790cc 345 *)
852cd5fb 346 break
32f790cc
MW
347 ;;
348 esac
349 hack=${arg#[+-]}
350 dir=$HOME/bin/hacks/$hack
351 [ -d "$dir" ] || {
352 echo "$0: path hack $hack not found"
353 return 1
354 }
355 case "$arg,$force,:$PATH:" in
356 -*,*,*:"$dir":*)
852cd5fb 357 path-remove path "$dir"
32f790cc
MW
358 ;;
359 +*,t,*:"$dir":*)
852cd5fb 360 path-remove path "$dir"
32f790cc
MW
361 path-add path "$dir"
362 ;;
363 +*,nil,*:"$dir":*)
852cd5fb 364 ;;
32f790cc 365 +*,*)
852cd5fb 366 path-add path "$dir"
32f790cc
MW
367 ;;
368 esac
369 shift
370 done
371 if [ $# -eq 0 ]; then
372 PATH=$path
373 export PATH
374 else
375 PATH=$path "$@"
376 fi
377}
378
59c9c0e4
MW
379###--------------------------------------------------------------------------
380### Finishing touches.
f617db13 381
59c9c0e4 382## For `root' use -- some simple molly-guards.
f617db13
MW
383if (( UID == 0 )); then
384 alias rm='rm -i' cp='cp -i' mv='mv -i'
385 set -o noclobber
386fi
387
59c9c0e4 388## Run any local hooks.
68aea1d9
MW
389[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
390
59c9c0e4 391## Close the `__mdw_bashrc' guard.
f617db13 392fi
59c9c0e4
MW
393
394###----- That's all, folks --------------------------------------------------