3 ### Bash session things
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.)
7 if [ -z "$__mdw_bashrc" ]; then
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
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
17 [ -r /etc/bash_completion ] && . /etc/bash_completion
18 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
20 ## Set the temporary directory again. (If we've switched users, we'll want a
21 ## different temporary directory.)
22 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
24 ###--------------------------------------------------------------------------
27 ## Only bother if the shell is interactive.
30 ## Fancy highlighting in some terminals.
31 marker=${STY+'\[
\ek
\e\\\]'}
33 linux*|screen*|xterm*|vt100*|eterm*)
34 case "$(tput bold)" in
35 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
36 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
38 gitcolour="\[$(tput setaf 6)\]"
39 rccolour="\[$(tput setaf 1)\]"
40 uncolour="\[$(tput op)\]"
44 bold='' unbold='' nl='' gitcolour='' rccolour='' uncolour='';;
47 ## Choose the right delimiters. Highlight root prompts specially;
48 ## highlight when I'm running as some other user. Highlight when this
49 ## isn't the outermost shell on the terminal.
50 if (( EUID == 0 )); then
51 left=`echo « | iconv -f UTF-8 -t //translit`
52 right=`echo » | iconv -f UTF-8 -t //translit`
55 mdw|mwooding|nemo) u="" left="[" right="]" ;;
56 *) u="\\u@" left="{" right="}" ;;
58 if [ "$__mdw_tty" = "`tty`" ]; then
61 export __mdw_tty="`tty`"
65 ## If this session is insecure then highlight that.
66 if [ -z "$SSH_CLIENT" ] &&
67 [ "$__mdw_sechost" != "`hostname`" ]
72 ## Build the prompt string.
73 if type __git_ps1 >/dev/null 2>&1; then
74 git="$unbold$gitcolour\$(rc=\$?;__git_ps1;exit \$rc)$uncolour$bold"
78 rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
79 rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
80 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$git$rc$marker$right$unbold"
81 PS2="$PS1 $bold>$unbold "
82 unset nl bold unbold left right sec_l sec_r marker
83 unset gitcolour rccolour uncolour git rc
86 ###--------------------------------------------------------------------------
87 ### Other shell tweaking.
89 ## Random shell tweaks.
98 shopt -s expand_aliases
100 if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
105 shopt -s hostcomplete
107 shopt -s interactive_comments
113 shopt -u shift_verbose
115 HISTCONTROL=ignorespace:erasedups
117 ## Some handy aliases.
119 alias which="command -v"
121 alias ssync="rsync -e ssh"
123 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
127 alias re="rootly $EDITOR"
128 alias pstree="pstree -hl"
129 alias cdtmp='cd ${TMPDIR-/tmp}'
130 alias pushtmp='pushd ${TMPDIR-/tmp}'
132 alias svn="svnwrap svn"
135 ###--------------------------------------------------------------------------
138 ## Arrange for `ls' output to be in colour.
139 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
140 eval `dircolors -b ~/.dircolors`
145 unalias ls 2>/dev/null || :; function ls () {
147 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
153 ## Arrange for `grep' output to be in colour.
154 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
157 declare grep=$1; shift
159 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
164 alias grep="greplike grep"
165 alias egrep="greplike egrep"
166 alias fgrep="greplike fgrep"
167 alias zgrep="greplike zgrep"
169 ## Turn off pagers inside Emacs shell buffers.
170 case "$INSIDE_EMACS" in
171 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
174 ###--------------------------------------------------------------------------
175 ### More complicated shell functions.
177 ## xt [@HOST] XTERM-ARGS
179 ## Open a terminal, maybe on a remote host.
183 local remote=${1#@} title
185 if [ $# -gt 0 ]; then
186 title="xterm [$remote] $1"
188 title="xterm [$remote]"
190 (xterm -title "$title" -e ssh $remote "$@" &)
200 ## Tweak core dumps on and off, or show the current status.
203 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
204 xoff|xn|xno) ulimit -Sc 0 ;;
208 0) echo "Core dumps disabled" ;;
209 unlimited) echo "Core dumps enabled" ;;
210 *) echo "Core dump limit is $l blocks" ;;
214 echo >&2 "usage: core [y|n]"
222 ## Set current security world to NAME. With no NAME, print the currently
225 local nfast=${NFAST_HOME-/opt/nfast}
229 echo "${NFAST_KMDATA#$nfast/kmdata-}"
234 elif [ -d "$nfast/kmdata-$1" ]; then
235 kmdata=$nfast/kmdata-$1
237 echo >&2 "world: can't find world $1"
242 0) export NFAST_KMDATA=$kmdata ;;
249 ## Fix `man' under Slowaris.
257 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
260 command man "${man[@]}"
265 ###--------------------------------------------------------------------------
268 ## path-add [VAR] DIR
270 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
271 ## it's not there already.
273 local pathvar export dir val
275 1) pathvar=PATH dir=$1 export="export PATH";;
276 2) pathvar=$1 dir=$2 export=:;;
277 *) echo >&2 "Usage: $0 [VAR] DIR";;
279 eval "val=\$$pathvar"
284 eval "$pathvar=\$val"
288 ## path-remove [VAR] DIR
290 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
291 ## error if DIR isn't in VAR.
293 local pathvar export dir val
295 1) pathvar=PATH dir=$1 export="export PATH";;
296 2) pathvar=$1 dir=$2 export=:;;
297 *) echo >&2 "Usage: $0 [VAR] DIR";;
299 eval "val=\$$pathvar"
302 :"$dir":*) val=${val#$dir:} ;;
303 *:"$dir":) val=${val%:$dir} ;;
304 *:"$dir":*) val=${val/:$dir:/:} ;;
306 eval "$pathvar=\$val"
310 ## pathhack [-f] +HACK|-HACK...
312 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
313 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
314 ## that's already on the PATH doesn't do anything unless `-f' is set, in
315 ## which case it gets moved to the beginning. With no arguments, print the
316 ## currently installed hacks.
318 if [ $# -eq 0 ]; then
323 echo ${e#$HOME/bin/hacks/}
331 while [ $# -gt 0 ]; do
350 dir=$HOME/bin/hacks/$hack
352 echo "$0: path hack $hack not found"
355 case "$arg,$force,:$PATH:" in
357 path-remove path "$dir"
360 path-remove path "$dir"
371 if [ $# -eq 0 ]; then
379 ###--------------------------------------------------------------------------
380 ### Finishing touches.
382 ## For `root' use -- some simple molly-guards.
383 if (( UID == 0 )); then
384 alias rm='rm -i' cp='cp -i' mv='mv -i'
388 ## Run any local hooks.
389 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
391 ## Close the `__mdw_bashrc' guard.
394 ###----- That's all, folks --------------------------------------------------