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