chiark / gitweb /
dot/bashrc: Minor options tweaks.
[profile] / dot / bashrc
CommitLineData
bc30f5c4 1# -*- mode: sh; coding: utf-8 -*-
f617db13 2#
f617db13
MW
3# Bash session things
4#
5
6if [ -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
15if [ -t 0 ]; then
16
15b95c26
MW
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`
f617db13 27 else
15b95c26
MW
28 case $USER in
29 mdw|mwooding)
30 u="" left="[" right="]"
31 ;;
f617db13 32 *)
15b95c26
MW
33 u="\\u@" left="{" right="}"
34 ;;
f617db13 35 esac
15b95c26
MW
36 if [ "$__mdw_tty" = "`tty`" ]; then
37 left="<" right=">"
f617db13 38 else
15b95c26 39 export __mdw_tty="`tty`"
f617db13 40 fi
15b95c26 41 fi
f617db13 42
15b95c26
MW
43 if [ -z "$SSH_CLIENT" ] &&
44 [ "$__mdw_sechost" != "`hostname`" ]
45 then
46 sec_l='(' sec_r=')'
f617db13
MW
47 fi
48
15b95c26
MW
49 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
50 PS2="$PS1 $bold>$unbold "
51
f617db13
MW
52fi # is stdin a tty?
53
15b95c26
MW
54# --- Pagers are less useful within Emacs ---
55
56case "$INSIDE_EMACS" in
57 22.*,comint) export PAGER=cat ;;
58esac
59
f617db13
MW
60# --- Little preferences ---
61
62notify=1
63set -b
64shopt -u cdable_vars
65shopt -s cdspell
66shopt -s checkhash
67shopt -s checkwinsize
68shopt -s cmdhist
69shopt -u dotglob
70shopt -s expand_aliases
71shopt -s extglob
83a3b1eb
MW
72shopt -s globstar
73shopt -s gnu_errfmt
f617db13
MW
74shopt -s histappend
75shopt -s histreedit
76shopt -u histverify
77shopt -s hostcomplete
78shopt -s huponexit
79shopt -s interactive_comments
80shopt -s lithist
81shopt -u mailwarn
82shopt -u nocaseglob
83shopt -u nullglob
84shopt -s promptvars
85shopt -u shift_verbose
86shopt -s sourcepath
87
83a3b1eb
MW
88HISTCONTROL=ignorespace:erasedups
89
f617db13 90# --- Set the CDPATH ---
852cd5fb 91#
f617db13
MW
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`
15b95c26 105if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
f617db13
MW
106 eval `dircolors -b ~/.dircolors`
107else
108 unset LS_COLORS
109fi
110
111ls () {
112 if [ -t 1 ]; then
113 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
114 else
115 command ls "$@"
116 fi
117}
118
bf3f5761
MW
119# --- Some colour `grep' support ---
120
121export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
122
004beb57
MW
123greplike () {
124 declare grep=$1; shift
bf3f5761 125 if [ -t 1 ]; then
a085e9a8 126 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
bf3f5761 127 else
004beb57 128 command $grep "$@"
bf3f5761
MW
129 fi
130}
004beb57
MW
131alias grep="greplike grep"
132alias egrep="greplike egrep"
133alias fgrep="greplike fgrep"
4da2e817 134alias zgrep="greplike zgrep"
bf3f5761 135
f617db13
MW
136# --- Set up some simple aliases ---
137
138alias cx='chmod a+x'
139alias which="command -v"
140alias ssync="rsync -e ssh"
f141fe0f 141alias rootly=$__MDW_ROOTLY
f617db13 142alias r=rootly
4832db48 143alias re="rootly $EDITOR"
a1545658 144alias pstree="pstree -hl"
f617db13
MW
145alias cdtmp='cd ${TMPDIR-/tmp}'
146alias pushtmp='pushd ${TMPDIR-/tmp}'
4832db48 147alias e="$EDITOR"
ed2a32e1 148alias svn="svnwrap svn"
4543696d 149alias @="ssh"
f617db13 150
75ff64d8
MW
151[ -r /etc/bash_completion ] && . /etc/bash_completion
152[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
153
f617db13
MW
154# --- Make `xt' start an xterm, maybe logging into a remote host ---
155
156xt () {
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
176core () {
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
32f790cc
MW
199# --- Turning on and off path hacks ---
200
201path-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
217path-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
235pathhack () {
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 --)
852cd5fb 258 shift
32f790cc
MW
259 break
260 ;;
261 [-+]*)
852cd5fb 262 ;;
32f790cc 263 *)
852cd5fb 264 break
32f790cc
MW
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":*)
852cd5fb 275 path-remove path "$dir"
32f790cc
MW
276 ;;
277 +*,t,*:"$dir":*)
852cd5fb 278 path-remove path "$dir"
32f790cc
MW
279 path-add path "$dir"
280 ;;
281 +*,nil,*:"$dir":*)
852cd5fb 282 ;;
32f790cc 283 +*,*)
852cd5fb 284 path-add path "$dir"
32f790cc
MW
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
c08211aa
MW
297# --- Switching security worlds ---
298
299world () {
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
f617db13
MW
324# --- Fix `man' under Slowaris ---
325
326case "$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 ;;
340esac
341
342# --- For `root' use -- some simple molly-guards ---
343
344if (( UID == 0 )); then
345 alias rm='rm -i' cp='cp -i' mv='mv -i'
346 set -o noclobber
347fi
348
68aea1d9
MW
349[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
350
f617db13 351fi