chiark / gitweb /
dot/bashrc: Minor options tweaks.
[profile] / dot / bashrc
1 # -*- mode: sh; coding: utf-8 -*-
2 #
3 # Bash session things
4 #
5
6 if [ -z "$__mdw_bashrc" ]; then
7
8 __mdw_bashrc=done
9
10 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
11 [ -r /etc/bashrc ] && . /etc/bashrc
12
13 # --- First of all, set up the prompt string ---
14
15 if [ -t 0 ]; then
16
17   case "$TERM" in
18     linux*|screen*|xterm*|vt100*|eterm*)
19       bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" nl="\[\r\]" ;;
20     *)
21       bold='' unbold='' nl='' ;;
22   esac
23
24   if (( EUID == 0 )); then
25     left=`echo « | iconv -f utf8 -t //translit`
26     right=`echo » | iconv -f utf8 -t //translit`
27   else
28     case $USER in
29       mdw|mwooding)
30         u="" left="[" right="]"
31         ;;
32       *)
33         u="\\u@" left="{" right="}"
34         ;;
35     esac
36     if [ "$__mdw_tty" = "`tty`" ]; then
37       left="<" right=">"
38     else
39       export __mdw_tty="`tty`"
40     fi
41   fi
42
43   if [ -z "$SSH_CLIENT" ] &&
44     [ "$__mdw_sechost" != "`hostname`" ]
45     then
46     sec_l='(' sec_r=')'
47   fi
48
49   PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
50   PS2="$PS1 $bold>$unbold "
51
52 fi # is stdin a tty?
53
54 # --- Pagers are less useful within Emacs ---
55
56 case "$INSIDE_EMACS" in
57   22.*,comint) export PAGER=cat ;;
58 esac
59
60 # --- Little preferences ---
61
62 notify=1
63 set -b
64 shopt -u cdable_vars
65 shopt -s cdspell
66 shopt -s checkhash
67 shopt -s checkwinsize
68 shopt -s cmdhist
69 shopt -u dotglob
70 shopt -s expand_aliases
71 shopt -s extglob
72 shopt -s globstar
73 shopt -s gnu_errfmt
74 shopt -s histappend
75 shopt -s histreedit
76 shopt -u histverify
77 shopt -s hostcomplete
78 shopt -s huponexit
79 shopt -s interactive_comments
80 shopt -s lithist
81 shopt -u mailwarn
82 shopt -u nocaseglob
83 shopt -u nullglob
84 shopt -s promptvars
85 shopt -u shift_verbose
86 shopt -s sourcepath
87
88 HISTCONTROL=ignorespace:erasedups
89
90 # --- Set the CDPATH ---
91 #
92 # CDPATH=~/src:/usr/src:/usr/lib:/usr/share
93 # dots=..
94 # i=6
95 # while (( i > 0 )); do
96 #   CDPATH=$CDPATH:$dots
97 #   dots=$dots/..
98 #   (( i -= 1 ))
99 # done
100 # CDPATH=$CDPATH:/
101
102 # --- Some colour `ls' support ---
103
104 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
105 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
106   eval `dircolors -b ~/.dircolors`
107 else
108   unset LS_COLORS
109 fi
110
111 ls () {
112   if [ -t 1 ]; then
113     command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
114   else
115     command ls "$@"
116   fi
117 }
118
119 # --- Some colour `grep' support ---
120
121 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
122
123 greplike () {
124   declare grep=$1; shift
125   if [ -t 1 ]; then
126     command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
127   else
128     command $grep "$@"
129   fi
130 }
131 alias grep="greplike grep"
132 alias egrep="greplike egrep"
133 alias fgrep="greplike fgrep"
134 alias zgrep="greplike zgrep"
135
136 # --- Set up some simple aliases ---
137
138 alias cx='chmod a+x'
139 alias which="command -v"
140 alias ssync="rsync -e ssh"
141 alias rootly=$__MDW_ROOTLY
142 alias r=rootly
143 alias re="rootly $EDITOR"
144 alias pstree="pstree -hl"
145 alias cdtmp='cd ${TMPDIR-/tmp}'
146 alias pushtmp='pushd ${TMPDIR-/tmp}'
147 alias e="$EDITOR"
148 alias svn="svnwrap svn"
149 alias @="ssh"
150
151 [ -r /etc/bash_completion ] && . /etc/bash_completion
152 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
153
154 # --- Make `xt' start an xterm, maybe logging into a remote host ---
155
156 xt () {
157   case "$1" in
158     @*)
159       local remote=${1#@} title
160       shift
161       if [ $# -gt 0 ]; then
162         title="xterm [$remote] $1"
163       else
164         title="xterm [$remote]"
165       fi
166       (xterm -title "$title" -e ssh $remote "$@" &)
167       ;;
168     *)
169       (xterm "$@" &)
170       ;;
171   esac
172 }
173
174 # --- Turning on and off core dumps ---
175
176 core () {
177   case "x$1" in
178     xon|xy|xyes)
179       ulimit -Sc `ulimit -Hc`
180       ;;
181     xoff|xn|xno)
182       ulimit -Sc 0
183       ;;
184     x)
185       local l=`ulimit -Sc`
186       case $l in
187         0) echo "Core dumps disabled" ;;
188         unlimited) echo "Core dumps enabled" ;;
189         *) echo "Core dump limit is $l blocks" ;;
190       esac
191       ;;
192     *)
193       echo >&2 "usage: core [yn]"
194       return 1
195       ;;
196   esac
197 }
198
199 # --- Turning on and off path hacks ---
200
201 path-add () {
202   local pathvar export dir val
203   case $# in
204     1) pathvar=PATH dir=$1 export="export PATH";;
205     2) pathvar=$1 dir=$2 export=:;;
206     *) echo >&2 "Usage: $0 [VAR] DIR";;
207   esac
208   eval "val=\$$pathvar"
209   case ":$val:" in
210     *:"$dir":*) ;;
211     *) val=$dir:$val ;;
212   esac
213   eval "$pathvar=\$val"
214   $export
215 }
216
217 path-remove () {
218   local pathvar export dir val
219   case $# in
220     1) pathvar=PATH dir=$1 export="export PATH";;
221     2) pathvar=$1 dir=$2 export=:;;
222     *) echo >&2 "Usage: $0 [VAR] DIR";;
223   esac
224   eval "val=\$$pathvar"
225   case ":$val:" in
226     :"$dir":) val= ;;
227     :"$dir":*) val=${val#$dir:} ;;
228     *:"$dir":) val=${val%:$dir} ;;
229     *:"$dir":*) val=${val/:$dir:/:} ;;
230   esac
231   eval "$pathvar=\$val"
232   $export
233 }
234
235 pathhack () {
236   if [ $# -eq 0 ]; then
237     local IFS=:
238     for e in $PATH; do
239       case "$e" in
240         "$HOME/bin/hacks/"*)
241           echo ${e#$HOME/bin/hacks/}
242           ;;
243       esac
244     done
245     return
246   fi
247   local force=nil
248   local path=$PATH
249   while [ $# -gt 0 ]; do
250     arg=$1
251     case "$arg" in
252       -f | --force)
253         force=t
254         shift
255         continue
256         ;;
257       --)
258         shift
259         break
260         ;;
261       [-+]*)
262         ;;
263       *)
264         break
265         ;;
266     esac
267     hack=${arg#[+-]}
268     dir=$HOME/bin/hacks/$hack
269     [ -d "$dir" ] || {
270       echo "$0: path hack $hack not found"
271       return 1
272     }
273     case "$arg,$force,:$PATH:" in
274       -*,*,*:"$dir":*)
275         path-remove path "$dir"
276         ;;
277       +*,t,*:"$dir":*)
278         path-remove path "$dir"
279         path-add path "$dir"
280         ;;
281       +*,nil,*:"$dir":*)
282         ;;
283       +*,*)
284         path-add path "$dir"
285         ;;
286     esac
287     shift
288   done
289   if [ $# -eq 0 ]; then
290     PATH=$path
291     export PATH
292   else
293     PATH=$path "$@"
294   fi
295 }
296
297 # --- Switching security worlds ---
298
299 world () {
300   local nfast=${NFAST_HOME-/opt/nfast}
301   local kmdata
302   case "$#" in
303     0)
304       echo "${NFAST_KMDATA#$nfast/kmdata-}"
305       ;;
306     *)
307       if [ -d "$1" ]; then
308         kmdata=$1
309       elif [ -d "$nfast/kmdata-$1" ]; then
310         kmdata=$nfast/kmdata-$1
311       else
312         echo >&2 "world: can't find world $1"
313         return 1
314       fi
315       shift
316       case "$#" in
317         0) export NFAST_KMDATA=$kmdata ;;
318         *) "$@" ;;
319       esac
320       ;;
321   esac
322 }
323
324 # --- Fix `man' under Slowaris ---
325
326 case "$MACHTYPE" in
327   *solaris*)
328     man () {
329       declare -i i=0
330       declare arg
331       declare -a man
332
333       for arg; do
334         case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
335         man[i+=1]="$arg"
336       done
337       command man "${man[@]}"
338     }
339     ;;
340 esac
341
342 # --- For `root' use -- some simple molly-guards ---
343
344 if (( UID == 0 )); then
345   alias rm='rm -i' cp='cp -i' mv='mv -i'
346   set -o noclobber
347 fi
348
349 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
350
351 fi