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