chiark / gitweb /
mdw-pager: New hack for pager support.
[profile] / 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 histappend
73 shopt -s histreedit
74 shopt -u histverify
75 shopt -s hostcomplete
76 shopt -s huponexit
77 shopt -s interactive_comments
78 shopt -s lithist
79 shopt -u mailwarn
80 shopt -u nocaseglob
81 shopt -u nullglob
82 shopt -s promptvars
83 shopt -u shift_verbose
84 shopt -s sourcepath
85
86 # --- Set the CDPATH ---
87 #
88 # CDPATH=~/src:/usr/src:/usr/lib:/usr/share
89 # dots=..
90 # i=6
91 # while (( i > 0 )); do
92 #   CDPATH=$CDPATH:$dots
93 #   dots=$dots/..
94 #   (( i -= 1 ))
95 # done
96 # CDPATH=$CDPATH:/
97
98 # --- Some colour `ls' support ---
99
100 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
101 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
102   eval `dircolors -b ~/.dircolors`
103 else
104   unset LS_COLORS
105 fi
106
107 ls () {
108   if [ -t 1 ]; then
109     command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
110   else
111     command ls "$@"
112   fi
113 }
114
115 # --- Some colour `grep' support ---
116
117 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
118
119 greplike () {
120   declare grep=$1; shift
121   if [ -t 1 ]; then
122     command $grep ${GREP_COLORS+--color=auto} "$@"
123   else
124     command $grep "$@"
125   fi
126 }
127 alias grep="greplike grep"
128 alias egrep="greplike egrep"
129 alias fgrep="greplike fgrep"
130
131 # --- Set up some simple aliases ---
132
133 alias cx='chmod a+x'
134 alias which="command -v"
135 alias ssync="rsync -e ssh"
136 alias rootly=$__MDW_ROOTLY
137 alias r=rootly
138 alias re="rootly $EDITOR"
139 alias pstree="pstree -hl"
140 alias cdtmp='cd ${TMPDIR-/tmp}'
141 alias pushtmp='pushd ${TMPDIR-/tmp}'
142 alias e="$EDITOR"
143 alias svn="svnwrap svn"
144 alias @="ssh"
145
146 [ -r /etc/bash_completion ] && . /etc/bash_completion
147 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
148
149 # --- Make `xt' start an xterm, maybe logging into a remote host ---
150
151 xt () {
152   case "$1" in
153     @*)
154       local remote=${1#@} title
155       shift
156       if [ $# -gt 0 ]; then
157         title="xterm [$remote] $1"
158       else
159         title="xterm [$remote]"
160       fi
161       (xterm -title "$title" -e ssh $remote "$@" &)
162       ;;
163     *)
164       (xterm "$@" &)
165       ;;
166   esac
167 }
168
169 # --- Turning on and off core dumps ---
170
171 core () {
172   case "x$1" in
173     xon|xy|xyes)
174       ulimit -Sc `ulimit -Hc`
175       ;;
176     xoff|xn|xno)
177       ulimit -Sc 0
178       ;;
179     x)
180       local l=`ulimit -Sc`
181       case $l in
182         0) echo "Core dumps disabled" ;;
183         unlimited) echo "Core dumps enabled" ;;
184         *) echo "Core dump limit is $l blocks" ;;
185       esac
186       ;;
187     *)
188       echo >&2 "usage: core [yn]"
189       return 1
190       ;;
191   esac
192 }
193
194 # --- Turning on and off path hacks ---
195
196 path-add () {
197   local pathvar export dir val
198   case $# in
199     1) pathvar=PATH dir=$1 export="export PATH";;
200     2) pathvar=$1 dir=$2 export=:;;
201     *) echo >&2 "Usage: $0 [VAR] DIR";;
202   esac
203   eval "val=\$$pathvar"
204   case ":$val:" in
205     *:"$dir":*) ;;
206     *) val=$dir:$val ;;
207   esac
208   eval "$pathvar=\$val"
209   $export
210 }
211
212 path-remove () {
213   local pathvar export dir val
214   case $# in
215     1) pathvar=PATH dir=$1 export="export PATH";;
216     2) pathvar=$1 dir=$2 export=:;;
217     *) echo >&2 "Usage: $0 [VAR] DIR";;
218   esac
219   eval "val=\$$pathvar"
220   case ":$val:" in
221     :"$dir":) val= ;;
222     :"$dir":*) val=${val#$dir:} ;;
223     *:"$dir":) val=${val%:$dir} ;;
224     *:"$dir":*) val=${val/:$dir:/:} ;;
225   esac
226   eval "$pathvar=\$val"
227   $export
228 }
229
230 pathhack () {
231   if [ $# -eq 0 ]; then
232     local IFS=:
233     for e in $PATH; do
234       case "$e" in
235         "$HOME/bin/hacks/"*)
236           echo ${e#$HOME/bin/hacks/}
237           ;;
238       esac
239     done
240     return
241   fi
242   local force=nil
243   local path=$PATH
244   while [ $# -gt 0 ]; do
245     arg=$1
246     case "$arg" in
247       -f | --force)
248         force=t
249         shift
250         continue
251         ;;
252       --)
253         shift
254         break
255         ;;
256       [-+]*)
257         ;;
258       *)
259         break
260         ;;
261     esac
262     hack=${arg#[+-]}
263     dir=$HOME/bin/hacks/$hack
264     [ -d "$dir" ] || {
265       echo "$0: path hack $hack not found"
266       return 1
267     }
268     case "$arg,$force,:$PATH:" in
269       -*,*,*:"$dir":*)
270         path-remove path "$dir"
271         ;;
272       +*,t,*:"$dir":*)
273         path-remove path "$dir"
274         path-add path "$dir"
275         ;;
276       +*,nil,*:"$dir":*)
277         ;;
278       +*,*)
279         path-add path "$dir"
280         ;;
281     esac
282     shift
283   done
284   if [ $# -eq 0 ]; then
285     PATH=$path
286     export PATH
287   else
288     PATH=$path "$@"
289   fi
290 }
291
292 # --- Switching security worlds ---
293
294 world () {
295   local nfast=${NFAST_HOME-/opt/nfast}
296   local kmdata
297   case "$#" in
298     0)
299       echo "${NFAST_KMDATA#$nfast/kmdata-}"
300       ;;
301     *)
302       if [ -d "$1" ]; then
303         kmdata=$1
304       elif [ -d "$nfast/kmdata-$1" ]; then
305         kmdata=$nfast/kmdata-$1
306       else
307         echo >&2 "world: can't find world $1"
308         return 1
309       fi
310       shift
311       case "$#" in
312         0) export NFAST_KMDATA=$kmdata ;;
313         *) "$@" ;;
314       esac
315       ;;
316   esac
317 }
318
319 # --- Fix `man' under Slowaris ---
320
321 case "$MACHTYPE" in
322   *solaris*)
323     man () {
324       declare -i i=0
325       declare arg
326       declare -a man
327
328       for arg; do
329         case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
330         man[i+=1]="$arg"
331       done
332       command man "${man[@]}"
333     }
334     ;;
335 esac
336
337 # --- For `root' use -- some simple molly-guards ---
338
339 if (( UID == 0 )); then
340   alias rm='rm -i' cp='cp -i' mv='mv -i'
341   set -o noclobber
342 fi
343
344 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
345
346 fi