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