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