]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.screenrc: TERM=xterm-256color
[dotfiles.git] / .bashrc
1 #!/bin/bash
2 # ~/.bashrc: executed by bash(1) for non-login shells.
3 # A basically sane bash environment.
4 # Tony Duckles <http://nynim.org/about/> (based on http://github.com/rtomayko/dotfiles)
5
6 # the basics
7 : ${HOME=~}
8 : ${LOGNAME=$(id -un)}
9 : ${UNAME=$(uname)}
10 # strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
11 UNAME=${UNAME/CYGWIN_*/Cygwin}
12
13 # complete hostnames from this file
14 HOSTFILE=~/.ssh/known_hosts
15
16 # readline config
17 INPUTRC=~/.inputrc
18
19 # if current $TERM isn't valid, fall-back to TERM=xterm-color
20 case $(tput colors 2>&1) in
21 tput* )
22 export TERM=xterm-color
23 ;;
24 esac
25
26 if [ "$UNAME" = "Darwin" ]; then
27 # Fink init, for OSX
28 test -r /sw/bin/init.sh &&
29 source /sw/bin/init.sh
30 fi
31
32 # ----------------------------------------------------------------------
33 # SHELL OPTIONS
34 # ----------------------------------------------------------------------
35
36 # bring in system bashrc
37 test -r /etc/bashrc &&
38 . /etc/bashrc
39
40 # notify of bg job completion immediately
41 set -o notify
42
43 # shell opts. see bash(1) for details
44 shopt -s cdspell >/dev/null 2>&1
45 shopt -s extglob >/dev/null 2>&1
46 shopt -s histappend >/dev/null 2>&1
47 shopt -s hostcomplete >/dev/null 2>&1
48 shopt -s interactive_comments >/dev/null 2>&1
49 shopt -u mailwarn >/dev/null 2>&1
50 shopt -s no_empty_cmd_completion >/dev/null 2>&1
51
52 # don't check for new mail
53 unset MAILCHECK
54
55 # disable core dumps
56 ulimit -S -c 0
57
58 # default umask
59 umask 0022
60
61 # ----------------------------------------------------------------------
62 # PATH
63 # ----------------------------------------------------------------------
64
65 # we want the various sbins on the path along with /usr/local/bin
66 PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
67 PATH="/usr/local/bin:$PATH"
68
69 # put ~/bin on PATH if you have it
70 test -d "$HOME/bin" &&
71 PATH="$HOME/bin:$PATH"
72
73 # ----------------------------------------------------------------------
74 # ENVIRONMENT CONFIGURATION
75 # ----------------------------------------------------------------------
76
77 # detect interactive shell
78 case "$-" in
79 *i*) INTERACTIVE=yes ;;
80 *) unset INTERACTIVE ;;
81 esac
82
83 # detect login shell
84 case "$0" in
85 -*) LOGIN=yes ;;
86 *) unset LOGIN ;;
87 esac
88
89 # enable en_US locale w/ utf-8 encodings if not already
90 # configured
91 : ${LANG:="en_US.UTF-8"}
92 : ${LANGUAGE:="en"}
93 : ${LC_CTYPE:="en_US.UTF-8"}
94 : ${LC_ALL:="en_US.UTF-8"}
95 export LANG LANGUAGE LC_CTYPE LC_ALL
96
97 # ignore backups, CVS directories, python bytecode, vim swap files
98 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
99 HISTCONTROL=ignoreboth
100
101 # ----------------------------------------------------------------------
102 # PAGER / EDITOR
103 # ----------------------------------------------------------------------
104
105 # See what we have to work with ...
106 HAVE_VIM=$(command -v vim)
107
108 # EDITOR
109 test -n "$HAVE_VIM" &&
110 EDITOR=vim ||
111 EDITOR=vi
112 export EDITOR
113
114 # PAGER
115 if test -n "$(command -v less)" ; then
116 PAGER="less -FirSwX"
117 MANPAGER="less -FiRswX"
118 else
119 PAGER=more
120 MANPAGER="$PAGER"
121 fi
122 export PAGER MANPAGER
123
124 # ----------------------------------------------------------------------
125 # PROMPT
126 # ----------------------------------------------------------------------
127
128 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
129
130 if [ "$UID" = 0 ]; then
131 # root
132 PS_C1="\[\033[1;31m\]" # red
133 PS_C2="\[\033[0;37m\]" # grey
134 PS_P="#"
135 else
136 case "$UNAME" in
137 Cygwin)
138 PS_C1="\[\033[0;32m\]" # green
139 PS_C2="\[\033[0;37m\]" # grey
140 ;;
141 Darwin)
142 PS_C1="\[\033[1;97m\]" # white
143 PS_C2="\[\033[0;37m\]" # grey
144 ;;
145 SunOS)
146 PS_C1="\[\033[1;96m\]" # cyan
147 PS_C2="\[\033[0;36m\]" # cyan
148 ;;
149 *)
150 PS_C1="\[\033[1;93m\]" # yellow
151 PS_C2="\[\033[0;33m\]" # brown
152 esac
153 PS_P="\$"
154 fi
155
156 prompt_simple() {
157 unset PROMPT_COMMAND
158 PS1="[\u@\h:\w]\$ "
159 PS2="> "
160 }
161
162 prompt_compact() {
163 unset PROMPT_COMMAND
164 PS1="${PS_C1}${PS_P}\[\033[0m\] "
165 PS2="> "
166 }
167
168 prompt_color() {
169 PS1="\[\033[0;90m\][${PS_C1}\u@\h\[\033[0m\]\[\033[0;90m\]:${PS_C2}\w\[\033[0;90m\]]${PS_P}\[\033[0m\] "
170 PS2="> "
171 }
172
173 # ----------------------------------------------------------------------
174 # MACOS X / DARWIN SPECIFIC
175 # ----------------------------------------------------------------------
176
177 if [ "$UNAME" = Darwin ]; then
178 # put ports on the paths if /opt/local exists
179 test -x /opt/local -a ! -L /opt/local && {
180 PORTS=/opt/local
181
182 # setup the PATH and MANPATH
183 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
184 MANPATH="$PORTS/share/man:$MANPATH"
185
186 # nice little port alias
187 alias port="sudo nice -n +18 $PORTS/bin/port"
188 }
189 fi
190
191 # ----------------------------------------------------------------------
192 # ALIASES / FUNCTIONS
193 # ----------------------------------------------------------------------
194
195 # alias 'vi' to 'vim' if Vim is installed
196 vim="$(type -P vim)"
197 test -n "$vim" && {
198 alias vi='vim'
199 }
200 unset vim
201
202 # disk usage with human sizes and minimal depth
203 alias du1='du -h --max-depth=1'
204 alias fn='find . -name'
205 alias hi='history | tail -20'
206
207 # For Solaris, use GNU versions of grep and sed
208 if [ "$UNAME" = SunOS ]; then
209 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
210 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
211 fi
212
213 # ----------------------------------------------------------------------
214 # BASH COMPLETION
215 # ----------------------------------------------------------------------
216
217 test -z "$BASH_COMPLETION" && {
218 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
219 test -n "$PS1" && test $bmajor -gt 1 && {
220 # search for a bash_completion file to source
221 for f in /usr/local/etc/bash_completion \
222 /usr/pkg/etc/bash_completion \
223 /opt/local/etc/bash_completion \
224 /etc/bash_completion
225 do
226 test -f $f && {
227 . $f
228 break
229 }
230 done
231 }
232 unset bash bmajor bminor
233 }
234
235 # override and disable tilde expansion
236 _expand()
237 {
238 return 0
239 }
240
241 # ----------------------------------------------------------------------
242 # LS AND DIRCOLORS
243 # ----------------------------------------------------------------------
244
245 # we always pass these to ls(1)
246 unset LS_COMMON
247 ## OS-specific options
248 #case "$UNAME" in
249 # "Linux" ) LS_COMMON="--color=auto";;
250 # "Cygwin" ) LS_COMMON="--color=auto";;
251 # "Darwin" ) LS_COMMON="--color=auto";;
252 #esac
253
254 # if the dircolors utility is available, set that up for ls
255 dircolors="$(type -P gdircolors dircolors | head -1)"
256 test -n "$dircolors" && {
257 COLORS=/etc/DIR_COLORS
258 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
259 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
260 test ! -e "$COLORS" && COLORS=
261 eval `$dircolors --sh $COLORS`
262 }
263 unset dircolors
264
265 # enable color ls output if available
266 test -n "$COLORS" &&
267 LS_COMMON="--color=auto $LS_COMMON"
268
269 # setup the main ls alias if we've established common args
270 test -n "$LS_COMMON" &&
271 alias ls="command ls $LS_COMMON"
272
273 # these use the ls aliases above
274 alias ll="ls -l"
275 alias l.="ls -d .*"
276 alias ll.="ls -ld .*"
277 alias lla="ls -la"
278
279 # setup color grep output if available
280 if [ -n "$COLORS" ]; then
281 case "$UNAME" in
282 "SunOS")
283 # For Solaris, use the GNU version of grep for color support
284 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=auto"
285 ;;
286 *)
287 alias grep="command grep --color=auto"
288 ;;
289 esac
290 fi
291
292 # --------------------------------------------------------------------
293 # MISC COMMANDS
294 # --------------------------------------------------------------------
295
296 # push SSH public key to another box
297 push_ssh_cert() {
298 local _host
299 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
300 for _host in "$@";
301 do
302 echo $_host
303 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
304 done
305 }
306
307 # --------------------------------------------------------------------
308 # PATH MANIPULATION FUNCTIONS
309 # --------------------------------------------------------------------
310
311 # Usage: pls [<var>]
312 # List path entries of PATH or environment variable <var>.
313 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
314
315 # Usage: pshift [-n <num>] [<var>]
316 # Shift <num> entries off the front of PATH or environment var <var>.
317 # with the <var> option. Useful: pshift $(pwd)
318 pshift () {
319 local n=1
320 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
321 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
322 }
323
324 # Usage: ppop [-n <num>] [<var>]
325 # Pop <num> entries off the end of PATH or environment variable <var>.
326 ppop () {
327 local n=1 i=0
328 [ "$1" = "-n" ] && { n=$2; shift 2; }
329 while [ $i -lt $n ]
330 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
331 i=$(( i + 1 ))
332 done
333 }
334
335 # Usage: prm <path> [<var>]
336 # Remove <path> from PATH or environment variable <var>.
337 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
338
339 # Usage: punshift <path> [<var>]
340 # Shift <path> onto the beginning of PATH or environment variable <var>.
341 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
342
343 # Usage: puniq [<path>]
344 # Remove duplicate entries from a PATH style value while retaining
345 # the original order. Use PATH if no <path> is given.
346 #
347 # Example:
348 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
349 # /usr/bin:/usr/local/bin
350 puniq () {
351 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
352 cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://'
353 }
354
355 # -------------------------------------------------------------------
356 # USER SHELL ENVIRONMENT
357 # -------------------------------------------------------------------
358
359 # source ~/.shenv now if it exists
360 test -r ~/.shenv &&
361 . ~/.shenv
362
363 # condense PATH entries
364 PATH=$(puniq $PATH)
365 MANPATH=$(puniq $MANPATH)
366
367 # Use the color prompt by default when interactive
368 test -n "$PS1" &&
369 prompt_color
370
371 # vim: ts=4 sts=4 shiftwidth=4 expandtab