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" ] && [ -z "$SCHROOT_CHROOT_NAME" ] &&
67 [ "$__mdw_sechost" != "`hostname`" ]
72 ## If this is an schroot environment then point this out.
74 hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
76 ## Build the prompt string.
78 if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
79 if type __git_ps1 >/dev/null 2>&1; then
80 git="$unbold$gitcolour\$(rc=\$?;__git_ps1;exit \$rc)$uncolour$bold"
82 rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
83 rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
85 PS1="$nl$bold$left$sec_l$u\\h$hqual$sec_r \\w$git$rc$marker$right$unbold"
86 PS2="$PS1 $bold>$unbold "
87 unset nl bold unbold left right sec_l sec_r marker
88 unset gitcolour rccolour uncolour git rc hqual
91 ###--------------------------------------------------------------------------
92 ### Other shell tweaking.
94 ## Random shell tweaks.
100 shopt -s checkwinsize
103 shopt -s expand_aliases
105 if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi
110 shopt -s hostcomplete
112 shopt -s interactive_comments
118 shopt -u shift_verbose
120 HISTCONTROL=ignorespace:erasedups
122 ## Some handy aliases.
124 alias which="command -v"
126 alias ssync="rsync -e ssh"
128 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
132 alias re="rootly $EDITOR"
133 alias pstree="pstree -hl"
134 alias cdtmp='cd ${TMPDIR-/tmp}'
135 alias pushtmp='pushd ${TMPDIR-/tmp}'
137 alias svn="svnwrap svn"
140 ###--------------------------------------------------------------------------
143 ## Arrange for `ls' output to be in colour.
144 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
145 eval `dircolors -b ~/.dircolors`
150 unalias ls 2>/dev/null || :; function ls () {
152 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
158 ## Arrange for `grep' output to be in colour.
159 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
162 declare grep=$1; shift
164 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
169 alias grep="greplike grep"
170 alias egrep="greplike egrep"
171 alias fgrep="greplike fgrep"
172 alias zgrep="greplike zgrep"
174 ## Turn off pagers inside Emacs shell buffers.
175 case "$INSIDE_EMACS" in
176 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
179 ###--------------------------------------------------------------------------
180 ### More complicated shell functions.
182 ## xt [@HOST] XTERM-ARGS
184 ## Open a terminal, maybe on a remote host.
188 local remote=${1#@} title
190 if [ $# -gt 0 ]; then
191 title="xterm [$remote] $1"
193 title="xterm [$remote]"
195 (xterm -title "$title" -e ssh $remote "$@" &)
205 ## Tweak core dumps on and off, or show the current status.
208 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
209 xoff|xn|xno) ulimit -Sc 0 ;;
213 0) echo "Core dumps disabled" ;;
214 unlimited) echo "Core dumps enabled" ;;
215 *) echo "Core dump limit is $l blocks" ;;
219 echo >&2 "usage: core [y|n]"
227 ## Set current security world to NAME. With no NAME, print the currently
230 local nfast=${NFAST_HOME-/opt/nfast}
234 echo "${NFAST_KMDATA#$nfast/kmdata-}"
239 elif [ -d "$nfast/kmdata-$1" ]; then
240 kmdata=$nfast/kmdata-$1
242 echo >&2 "world: can't find world $1"
247 0) export NFAST_KMDATA=$kmdata ;;
254 ## Fix `man' under Slowaris.
262 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
265 command man "${man[@]}"
270 ###--------------------------------------------------------------------------
273 ## path-add [VAR] DIR
275 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
276 ## it's not there already.
278 local pathvar export dir val
280 1) pathvar=PATH dir=$1 export="export PATH";;
281 2) pathvar=$1 dir=$2 export=:;;
282 *) echo >&2 "Usage: $0 [VAR] DIR";;
284 eval "val=\$$pathvar"
289 eval "$pathvar=\$val"
293 ## path-remove [VAR] DIR
295 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
296 ## error if DIR isn't in VAR.
298 local pathvar export dir val
300 1) pathvar=PATH dir=$1 export="export PATH";;
301 2) pathvar=$1 dir=$2 export=:;;
302 *) echo >&2 "Usage: $0 [VAR] DIR";;
304 eval "val=\$$pathvar"
307 :"$dir":*) val=${val#$dir:} ;;
308 *:"$dir":) val=${val%:$dir} ;;
309 *:"$dir":*) val=${val/:$dir:/:} ;;
311 eval "$pathvar=\$val"
315 ## pathhack [-f] +HACK|-HACK...
317 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
318 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
319 ## that's already on the PATH doesn't do anything unless `-f' is set, in
320 ## which case it gets moved to the beginning. With no arguments, print the
321 ## currently installed hacks.
323 if [ $# -eq 0 ]; then
328 echo ${e#$HOME/bin/hacks/}
336 while [ $# -gt 0 ]; do
355 dir=$HOME/bin/hacks/$hack
357 echo "$0: path hack $hack not found"
360 case "$arg,$force,:$PATH:" in
362 path-remove path "$dir"
365 path-remove path "$dir"
376 if [ $# -eq 0 ]; then
384 ###--------------------------------------------------------------------------
385 ### Finishing touches.
387 ## For `root' use -- some simple molly-guards.
388 if (( UID == 0 )); then
389 alias rm='rm -i' cp='cp -i' mv='mv -i'
393 ## Run any local hooks.
394 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
396 ## Close the `__mdw_bashrc' guard.
399 ###----- That's all, folks --------------------------------------------------