]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.bashrc: Fix $PAGER/$MANPAGER less options
[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
100 # history stuff
101 HISTCONTROL=ignoreboth
102 HISTFILESIZE=10000
103 HISTSIZE=10000
104
105 # ----------------------------------------------------------------------
106 # PAGER / EDITOR
107 # ----------------------------------------------------------------------
108
109 # See what we have to work with ...
110 HAVE_VIM=$(command -v vim)
111
112 # EDITOR
113 test -n "$HAVE_VIM" &&
114 EDITOR=vim ||
115 EDITOR=vi
116 export EDITOR
117
118 # PAGER
119 if test -n "$(command -v less)" ; then
120 PAGER="less -Fir"
121 MANPAGER="less -FiRs"
122 else
123 PAGER=more
124 MANPAGER="$PAGER"
125 fi
126 export PAGER MANPAGER
127
128 # Ack
129 ACK_PAGER="$PAGER"
130 ACK_PAGER_COLOR="$PAGER"
131
132 # ----------------------------------------------------------------------
133 # PROMPT
134 # ----------------------------------------------------------------------
135
136 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
137
138 if [ "$UID" = 0 ]; then
139 # root
140 PS_C1="\[\033[1;31m\]" # red
141 PS_C2="\[\033[0;37m\]" # grey
142 PS_P="#"
143 else
144 case "$UNAME" in
145 Cygwin)
146 PS_C1="\[\033[0;32m\]" # green
147 PS_C2="\[\033[0;37m\]" # grey
148 ;;
149 Darwin)
150 PS_C1="\[\033[1;97m\]" # white
151 PS_C2="\[\033[0;37m\]" # grey
152 ;;
153 SunOS)
154 PS_C1="\[\033[1;96m\]" # cyan
155 PS_C2="\[\033[0;36m\]" # cyan
156 ;;
157 *)
158 PS_C1="\[\033[1;93m\]" # yellow
159 PS_C2="\[\033[0;33m\]" # brown
160 esac
161 PS_P="\$"
162 fi
163
164 prompt_simple() {
165 unset PROMPT_COMMAND
166 PS1="[\u@\h:\w]\$ "
167 PS2="> "
168 }
169
170 prompt_compact() {
171 unset PROMPT_COMMAND
172 PS1="${PS_C1}${PS_P}\[\033[0m\] "
173 PS2="> "
174 }
175
176 prompt_color() {
177 PS1="\[\033[0;90m\][${PS_C1}\u@\h\[\033[0m\]\[\033[0;90m\]:${PS_C2}\w\[\033[0;90m\]]${PS_P}\[\033[0m\] "
178 PS2="> "
179 }
180
181 # ----------------------------------------------------------------------
182 # MACOS X / DARWIN SPECIFIC
183 # ----------------------------------------------------------------------
184
185 if [ "$UNAME" = Darwin ]; then
186 # put ports on the paths if /opt/local exists
187 test -x /opt/local -a ! -L /opt/local && {
188 PORTS=/opt/local
189
190 # setup the PATH and MANPATH
191 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
192 MANPATH="$PORTS/share/man:$MANPATH"
193
194 # nice little port alias
195 alias port="sudo nice -n +18 $PORTS/bin/port"
196 }
197 fi
198
199 # ----------------------------------------------------------------------
200 # ALIASES / FUNCTIONS
201 # ----------------------------------------------------------------------
202
203 # alias 'vi' to 'vim' if Vim is installed
204 vim="$(type -P vim)"
205 test -n "$vim" && {
206 alias vi='vim'
207 }
208 unset vim
209
210 # disk usage with human sizes and minimal depth
211 alias du1='du -h --max-depth=1'
212 alias fn='find . -name'
213 alias hi='history | tail -20'
214
215 # For Solaris, use GNU versions of grep and sed
216 if [ "$UNAME" = SunOS ]; then
217 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
218 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
219 fi
220
221 # ----------------------------------------------------------------------
222 # BASH COMPLETION
223 # ----------------------------------------------------------------------
224
225 test -z "$BASH_COMPLETION" && {
226 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
227 test -n "$PS1" && test $bmajor -gt 1 && {
228 # search for a bash_completion file to source
229 for f in /usr/local/etc/bash_completion \
230 /usr/pkg/etc/bash_completion \
231 /opt/local/etc/bash_completion \
232 /etc/bash_completion
233 do
234 test -f $f && {
235 . $f
236 break
237 }
238 done
239 }
240 unset bash bmajor bminor
241 }
242
243 # override and disable tilde expansion
244 _expand() {
245 return 0
246 }
247
248 # ----------------------------------------------------------------------
249 # LS AND DIRCOLORS
250 # ----------------------------------------------------------------------
251
252 # we always pass these to ls(1)
253 unset LS_COMMON
254 ## OS-specific options
255 #case "$UNAME" in
256 # "Linux" ) LS_COMMON="--color=auto";;
257 # "Cygwin" ) LS_COMMON="--color=auto";;
258 # "Darwin" ) LS_COMMON="--color=auto";;
259 #esac
260
261 # if the dircolors utility is available, set that up for ls
262 dircolors="$(type -P gdircolors dircolors | head -1)"
263 test -n "$dircolors" && {
264 COLORS=/etc/DIR_COLORS
265 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
266 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
267 test ! -e "$COLORS" && COLORS=
268 eval `$dircolors --sh $COLORS`
269 }
270 unset dircolors
271
272 # enable color ls output if available
273 test -n "$COLORS" &&
274 LS_COMMON="--color=auto $LS_COMMON"
275
276 # setup the main ls alias if we've established common args
277 test -n "$LS_COMMON" &&
278 alias ls="command ls $LS_COMMON"
279
280 # these use the ls aliases above
281 alias ll="ls -l"
282 alias l.="ls -d .*"
283 alias ll.="ls -ld .*"
284 alias lla="ls -la"
285
286 # setup color grep output if available
287 if [ -n "$COLORS" ]; then
288 case "$UNAME" in
289 "SunOS")
290 # For Solaris, use the GNU version of grep for color support
291 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=auto"
292 ;;
293 *)
294 alias grep="command grep --color=auto"
295 ;;
296 esac
297 fi
298
299 # --------------------------------------------------------------------
300 # MISC COMMANDS
301 # --------------------------------------------------------------------
302
303 # push SSH public key to another box
304 push_ssh_cert() {
305 local _host
306 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
307 for _host in "$@";
308 do
309 echo $_host
310 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
311 done
312 }
313
314 # --------------------------------------------------------------------
315 # PATH MANIPULATION FUNCTIONS
316 # --------------------------------------------------------------------
317
318 # Usage: pls [<var>]
319 # List path entries of PATH or environment variable <var>.
320 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
321
322 # Usage: pshift [-n <num>] [<var>]
323 # Shift <num> entries off the front of PATH or environment var <var>.
324 # with the <var> option. Useful: pshift $(pwd)
325 pshift () {
326 local n=1
327 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
328 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
329 }
330
331 # Usage: ppop [-n <num>] [<var>]
332 # Pop <num> entries off the end of PATH or environment variable <var>.
333 ppop () {
334 local n=1 i=0
335 [ "$1" = "-n" ] && { n=$2; shift 2; }
336 while [ $i -lt $n ]
337 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
338 i=$(( i + 1 ))
339 done
340 }
341
342 # Usage: prm <path> [<var>]
343 # Remove <path> from PATH or environment variable <var>.
344 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
345
346 # Usage: punshift <path> [<var>]
347 # Shift <path> onto the beginning of PATH or environment variable <var>.
348 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
349
350 # Usage: ppush <path> [<var>]
351 ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; }
352
353 # Usage: puniq [<path>]
354 # Remove duplicate entries from a PATH style value while retaining
355 # the original order. Use PATH if no <path> is given.
356 #
357 # Example:
358 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
359 # /usr/bin:/usr/local/bin
360 puniq () {
361 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
362 cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://'
363 }
364
365 # -------------------------------------------------------------------
366 # USER SHELL ENVIRONMENT
367 # -------------------------------------------------------------------
368
369 # source ~/.shenv now if it exists
370 test -r ~/.shenv &&
371 . ~/.shenv
372
373 # condense PATH entries
374 PATH=$(puniq $PATH)
375 MANPATH=$(puniq $MANPATH)
376
377 # Use the color prompt by default when interactive
378 test -n "$PS1" &&
379 prompt_color
380
381 # -------------------------------------------------------------------
382 # MOTD / FORTUNE
383 # -------------------------------------------------------------------
384
385 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
386 # Get current uname and uptime (if exists on this host)
387 # Strip any leading whitespace from uname and uptime commands
388 t_uname="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
389 t_uptime="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
390 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
391 echo " --"
392 test -n "$t_uname" && echo $t_uname
393 test -n "$t_uptime" && echo $t_uptime
394 echo " --"
395 fi
396 unset t_uname t_uptime
397 }
398
399 # vim: ts=4 sts=4 shiftwidth=4 expandtab