hacks/ssh: Trivial formatting change.
[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 37 if (( EUID == 0 )); then
2b5d4580
MW
38 left=`echo « | iconv -f UTF-8 -t //translit`
39 right=`echo » | iconv -f UTF-8 -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"
ac03a1f6
MW
101rootly () {
102 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
103 $__MDW_ROOTLY "$@"
104}
59c9c0e4
MW
105alias r=rootly
106alias re="rootly $EDITOR"
107alias pstree="pstree -hl"
108alias cdtmp='cd ${TMPDIR-/tmp}'
109alias pushtmp='pushd ${TMPDIR-/tmp}'
110alias e="$EDITOR"
111alias svn="svnwrap svn"
112alias @="ssh"
f617db13 113
59c9c0e4
MW
114## Completion.
115[ -r /etc/bash_completion ] && . /etc/bash_completion
116[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
f617db13 117
59c9c0e4
MW
118###--------------------------------------------------------------------------
119### Colour output.
120
121## Arrange for `ls' output to be in colour.
15b95c26 122if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
f617db13
MW
123 eval `dircolors -b ~/.dircolors`
124else
125 unset LS_COLORS
126fi
127
128ls () {
129 if [ -t 1 ]; then
130 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
131 else
132 command ls "$@"
133 fi
134}
135
59c9c0e4 136## Arrange for `grep' output to be in colour.
bf3f5761
MW
137export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
138
004beb57
MW
139greplike () {
140 declare grep=$1; shift
bf3f5761 141 if [ -t 1 ]; then
a085e9a8 142 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
bf3f5761 143 else
004beb57 144 command $grep "$@"
bf3f5761
MW
145 fi
146}
004beb57
MW
147alias grep="greplike grep"
148alias egrep="greplike egrep"
149alias fgrep="greplike fgrep"
4da2e817 150alias zgrep="greplike zgrep"
bf3f5761 151
59c9c0e4
MW
152## Turn off pagers inside Emacs shell buffers.
153case "$INSIDE_EMACS" in
154 22.*,comint) export PAGER=cat ;;
155esac
75ff64d8 156
59c9c0e4
MW
157###--------------------------------------------------------------------------
158### More complicated shell functions.
f617db13 159
59c9c0e4
MW
160## xt [@HOST] XTERM-ARGS
161##
162## Open a terminal, maybe on a remote host.
f617db13
MW
163xt () {
164 case "$1" in
165 @*)
166 local remote=${1#@} title
167 shift
168 if [ $# -gt 0 ]; then
169 title="xterm [$remote] $1"
170 else
171 title="xterm [$remote]"
172 fi
173 (xterm -title "$title" -e ssh $remote "$@" &)
174 ;;
175 *)
176 (xterm "$@" &)
177 ;;
178 esac
179}
180
59c9c0e4
MW
181## core [y|n]
182##
183## Tweak core dumps on and off, or show the current status.
f617db13
MW
184core () {
185 case "x$1" in
59c9c0e4
MW
186 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
187 xoff|xn|xno) ulimit -Sc 0 ;;
f617db13
MW
188 x)
189 local l=`ulimit -Sc`
190 case $l in
191 0) echo "Core dumps disabled" ;;
192 unlimited) echo "Core dumps enabled" ;;
193 *) echo "Core dump limit is $l blocks" ;;
194 esac
195 ;;
196 *)
59c9c0e4 197 echo >&2 "usage: core [y|n]"
f617db13
MW
198 return 1
199 ;;
200 esac
201}
202
59c9c0e4
MW
203## world [NAME]
204##
205## Set current security world to NAME. With no NAME, print the currently
206## selected world.
207world () {
208 local nfast=${NFAST_HOME-/opt/nfast}
209 local kmdata
210 case "$#" in
211 0)
212 echo "${NFAST_KMDATA#$nfast/kmdata-}"
213 ;;
214 *)
215 if [ -d "$1" ]; then
216 kmdata=$1
217 elif [ -d "$nfast/kmdata-$1" ]; then
218 kmdata=$nfast/kmdata-$1
219 else
220 echo >&2 "world: can't find world $1"
221 return 1
222 fi
223 shift
224 case "$#" in
225 0) export NFAST_KMDATA=$kmdata ;;
226 *) "$@" ;;
227 esac
228 ;;
229 esac
230}
231
232## Fix `man' under Slowaris.
233case "$MACHTYPE" in
234 *solaris*)
235 man () {
236 declare -i i=0
237 declare arg
238 declare -a man
239 for arg; do
240 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
241 man[i+=1]="$arg"
242 done
243 command man "${man[@]}"
244 }
245 ;;
246esac
247
248###--------------------------------------------------------------------------
249### Path hacks.
32f790cc 250
59c9c0e4
MW
251## path-add [VAR] DIR
252##
253## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
254## it's not there already.
32f790cc
MW
255path-add () {
256 local pathvar export dir val
257 case $# in
258 1) pathvar=PATH dir=$1 export="export PATH";;
259 2) pathvar=$1 dir=$2 export=:;;
260 *) echo >&2 "Usage: $0 [VAR] DIR";;
261 esac
262 eval "val=\$$pathvar"
263 case ":$val:" in
264 *:"$dir":*) ;;
265 *) val=$dir:$val ;;
266 esac
267 eval "$pathvar=\$val"
268 $export
269}
270
59c9c0e4
MW
271## path-remove [VAR] DIR
272##
273## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
274## error if DIR isn't in VAR.
32f790cc
MW
275path-remove () {
276 local pathvar export dir val
277 case $# in
278 1) pathvar=PATH dir=$1 export="export PATH";;
279 2) pathvar=$1 dir=$2 export=:;;
280 *) echo >&2 "Usage: $0 [VAR] DIR";;
281 esac
282 eval "val=\$$pathvar"
283 case ":$val:" in
284 :"$dir":) val= ;;
285 :"$dir":*) val=${val#$dir:} ;;
286 *:"$dir":) val=${val%:$dir} ;;
287 *:"$dir":*) val=${val/:$dir:/:} ;;
288 esac
289 eval "$pathvar=\$val"
290 $export
291}
292
59c9c0e4
MW
293## pathhack [-f] +HACK|-HACK...
294##
295## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
296## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
297## that's already on the PATH doesn't do anything unless `-f' is set, in
298## which case it gets moved to the beginning. With no arguments, print the
299## currently installed hacks.
32f790cc
MW
300pathhack () {
301 if [ $# -eq 0 ]; then
302 local IFS=:
303 for e in $PATH; do
304 case "$e" in
305 "$HOME/bin/hacks/"*)
306 echo ${e#$HOME/bin/hacks/}
307 ;;
308 esac
309 done
310 return
311 fi
312 local force=nil
313 local path=$PATH
314 while [ $# -gt 0 ]; do
315 arg=$1
316 case "$arg" in
317 -f | --force)
318 force=t
319 shift
320 continue
321 ;;
322 --)
852cd5fb 323 shift
32f790cc
MW
324 break
325 ;;
326 [-+]*)
852cd5fb 327 ;;
32f790cc 328 *)
852cd5fb 329 break
32f790cc
MW
330 ;;
331 esac
332 hack=${arg#[+-]}
333 dir=$HOME/bin/hacks/$hack
334 [ -d "$dir" ] || {
335 echo "$0: path hack $hack not found"
336 return 1
337 }
338 case "$arg,$force,:$PATH:" in
339 -*,*,*:"$dir":*)
852cd5fb 340 path-remove path "$dir"
32f790cc
MW
341 ;;
342 +*,t,*:"$dir":*)
852cd5fb 343 path-remove path "$dir"
32f790cc
MW
344 path-add path "$dir"
345 ;;
346 +*,nil,*:"$dir":*)
852cd5fb 347 ;;
32f790cc 348 +*,*)
852cd5fb 349 path-add path "$dir"
32f790cc
MW
350 ;;
351 esac
352 shift
353 done
354 if [ $# -eq 0 ]; then
355 PATH=$path
356 export PATH
357 else
358 PATH=$path "$@"
359 fi
360}
361
59c9c0e4
MW
362###--------------------------------------------------------------------------
363### Finishing touches.
f617db13 364
59c9c0e4 365## For `root' use -- some simple molly-guards.
f617db13
MW
366if (( UID == 0 )); then
367 alias rm='rm -i' cp='cp -i' mv='mv -i'
368 set -o noclobber
369fi
370
59c9c0e4 371## Run any local hooks.
68aea1d9
MW
372[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
373
59c9c0e4 374## Close the `__mdw_bashrc' guard.
f617db13 375fi
59c9c0e4
MW
376
377###----- That's all, folks --------------------------------------------------