dot/bashrc: Show Git branch name in the prompt, if machinery available.
[profile] / dot / bashrc
1 ### -*-bash-*-
2 ###
3 ### Bash session things
4
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.)
7 if [ -z "$__mdw_bashrc" ]; then
8 __mdw_bashrc=done
9
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.
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
15
16 ## Completion.
17 [ -r /etc/bash_completion ] && . /etc/bash_completion
18 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
19
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.
26
27 ## Only bother if the shell is interactive.
28 if [ -t 0 ]; then
29
30 ## Fancy highlighting in some terminals.
31 marker=${STY+'\[\ek\e\\\]'}
32 case "$TERM" in
33 linux*|screen*|xterm*|vt100*|eterm*)
34 case "$(tput bold)" in
35 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
36 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
37 esac
38 gitcolour="\[$(tput setaf 6)\]"
39 rccolour="\[$(tput setaf 1)\]"
40 uncolour="\[$(tput op)\]"
41 nl="\[ \]"
42 ;;
43 *)
44 bold='' unbold='' nl='' gitcolour='' rccolour='' uncolour='';;
45 esac
46
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.
50 if (( EUID == 0 )); then
51 left=`echo « | iconv -f UTF-8 -t //translit`
52 right=`echo » | iconv -f UTF-8 -t //translit`
53 else
54 case $USER in
55 mdw|mwooding|nemo) u="" left="[" right="]" ;;
56 *) u="\\u@" left="{" right="}" ;;
57 esac
58 if [ "$__mdw_tty" = "`tty`" ]; then
59 left="<" right=">"
60 else
61 export __mdw_tty="`tty`"
62 fi
63 fi
64
65 ## If this session is insecure then highlight that.
66 if [ -z "$SSH_CLIENT" ] &&
67 [ "$__mdw_sechost" != "`hostname`" ]
68 then
69 sec_l='(' sec_r=')'
70 fi
71
72 ## Build the prompt string.
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
78 rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
79 rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
80 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$git$rc$marker$right$unbold"
81 PS2="$PS1 $bold>$unbold "
82 unset nl bold unbold left right sec_l sec_r marker
83 unset gitcolour rccolour uncolour git rc
84 fi
85
86 ###--------------------------------------------------------------------------
87 ### Other shell tweaking.
88
89 ## Random shell tweaks.
90 notify=1
91 set -b
92 shopt -u cdable_vars
93 shopt -s cdspell
94 shopt -s checkhash
95 shopt -s checkwinsize
96 shopt -s cmdhist
97 shopt -u dotglob
98 shopt -s expand_aliases
99 shopt -s extglob
100 if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
101 shopt -s gnu_errfmt
102 shopt -s histappend
103 shopt -s histreedit
104 shopt -u histverify
105 shopt -s hostcomplete
106 shopt -s huponexit
107 shopt -s interactive_comments
108 shopt -s lithist
109 shopt -u mailwarn
110 shopt -u nocaseglob
111 shopt -u nullglob
112 shopt -s promptvars
113 shopt -u shift_verbose
114 shopt -s sourcepath
115 HISTCONTROL=ignorespace:erasedups
116
117 ## Some handy aliases.
118 alias cx='chmod a+x'
119 alias which="command -v"
120 alias rc="rc -l"
121 alias ssync="rsync -e ssh"
122 rootly () {
123 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
124 $__MDW_ROOTLY "$@"
125 }
126 alias r=rootly
127 alias re="rootly $EDITOR"
128 alias pstree="pstree -hl"
129 alias cdtmp='cd ${TMPDIR-/tmp}'
130 alias pushtmp='pushd ${TMPDIR-/tmp}'
131 alias e="$EDITOR"
132 alias svn="svnwrap svn"
133 alias @="ssh"
134
135 ###--------------------------------------------------------------------------
136 ### Colour output.
137
138 ## Arrange for `ls' output to be in colour.
139 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
140 eval `dircolors -b ~/.dircolors`
141 else
142 unset LS_COLORS
143 fi
144
145 unalias ls 2>/dev/null || :; function ls () {
146 if [ -t 1 ]; then
147 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
148 else
149 command ls "$@"
150 fi
151 }
152
153 ## Arrange for `grep' output to be in colour.
154 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
155
156 greplike () {
157 declare grep=$1; shift
158 if [ -t 1 ]; then
159 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
160 else
161 command $grep "$@"
162 fi
163 }
164 alias grep="greplike grep"
165 alias egrep="greplike egrep"
166 alias fgrep="greplike fgrep"
167 alias zgrep="greplike zgrep"
168
169 ## Turn off pagers inside Emacs shell buffers.
170 case "$INSIDE_EMACS" in
171 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
172 esac
173
174 ###--------------------------------------------------------------------------
175 ### More complicated shell functions.
176
177 ## xt [@HOST] XTERM-ARGS
178 ##
179 ## Open a terminal, maybe on a remote host.
180 xt () {
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
198 ## core [y|n]
199 ##
200 ## Tweak core dumps on and off, or show the current status.
201 core () {
202 case "x$1" in
203 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
204 xoff|xn|xno) ulimit -Sc 0 ;;
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 *)
214 echo >&2 "usage: core [y|n]"
215 return 1
216 ;;
217 esac
218 }
219
220 ## world [NAME]
221 ##
222 ## Set current security world to NAME. With no NAME, print the currently
223 ## selected world.
224 world () {
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.
250 case "$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 ;;
263 esac
264
265 ###--------------------------------------------------------------------------
266 ### Path hacks.
267
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.
272 path-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
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.
292 path-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
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.
317 pathhack () {
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 --)
340 shift
341 break
342 ;;
343 [-+]*)
344 ;;
345 *)
346 break
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":*)
357 path-remove path "$dir"
358 ;;
359 +*,t,*:"$dir":*)
360 path-remove path "$dir"
361 path-add path "$dir"
362 ;;
363 +*,nil,*:"$dir":*)
364 ;;
365 +*,*)
366 path-add path "$dir"
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
379 ###--------------------------------------------------------------------------
380 ### Finishing touches.
381
382 ## For `root' use -- some simple molly-guards.
383 if (( UID == 0 )); then
384 alias rm='rm -i' cp='cp -i' mv='mv -i'
385 set -o noclobber
386 fi
387
388 ## Run any local hooks.
389 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
390
391 ## Close the `__mdw_bashrc' guard.
392 fi
393
394 ###----- That's all, folks --------------------------------------------------