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