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