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