added my .inputrc
[distro-setup] / .bashrc
1 # to debug
2 #set -x
3 # redirect output to log file
4 #exec 1>/a/tmp/bashlog
5 #exec 2>/a/tmp/bashlog
6
7
8 # By default this file is sourced for all ssh commands. This is wonky.
9 # Normally, this file is not sourced when a script is run, and it would be much
10 # better and more consistent if that also happened when when running a script
11 # over ssh. so here we test for conditions of a script under ssh and return if
12 # so. we can override with ssh -t which sets $SSH_TTY, which we can detect
13 # But inside a script, ssh -t won't work, because we aren't using a tty at all.
14 # So we need something else. Command lines and env variables sent across ssh are strictly limited.
15 # We could override an obscure unused LC_var, like telephone, or we could transfer a file.
16 # But I choose to set SendEnv and AcceptEnv ssh vars for BASH_LOGIN_SHELL.
17 # In a private file, i have aliases for if $- == *i*, ssh -t, else ssh.
18
19 [[ $- != *i* && ! $SSH_CONNECTION ]] && export BASH_LOGIN_SHELL=true
20
21 if [[ $SSH_CONNECTION ]] \
22 && [[ $- == *c* ]] \
23 && [[ ! $SSH_TTY ]] \
24 && [[ ! $BASH_LOGIN_SHELL == true ]] \
25 && [[ $- != *i* ]]; then
26 return
27 fi
28
29
30
31
32
33
34
35 ############
36 # settings #
37 ############
38
39 CDPATH=.:/a
40
41 # remove all aliases. aliases provided by the system tend to get in the way,
42 # for example, error happens if I try to define a function the same name as an alias
43 unalias -a
44
45 # remove gnome keyring warning messages
46 # there is probably a more proper way, but I didn't find any easily on google
47 unset GNOME_KEYRING_CONTROL
48
49 #use extra globing features. See man bash, search extglob.
50 shopt -s extglob
51 #include .files when globbing.
52 shopt -s dotglob
53 #but ignore files name . and ..
54 # this also sets dotglob, but no harm in setting it explicitly
55 export GLOBIGNORE=*/.:*/..
56
57 # broken with bash_completion package. Saw a bug for this once. Don't anymore.
58 # still broken in wheezy
59 # still buggered in latest stable from the web, version 2.1
60 # perhaps its fixed in newer git version, which fails to make for me
61 # this note is from 6-2014
62 #shopt -s nullglob
63
64 # make tab on an empty line do nothing
65 shopt -s no_empty_cmd_completion
66
67 # advanced completion
68 # http://bash-completion.alioth.debian.org/
69 # might be sourced by the system already, but I've noticed it not being sourced before
70 if ! type _init_completion &> /dev/null && [[ -r "/usr/share/bash-completion/bash_completion" ]]; then
71 . /usr/share/bash-completion/bash_completion
72 fi
73
74
75 # fix spelling errors for cd, only in interactive shell
76 shopt -s cdspell
77 # append history instead of overwritting it
78 shopt -s histappend
79 # for compatibility, per gentoo/debian bashrc
80 shopt -s checkwinsize
81 # attempt to save multiline single commands as single history entries.
82 shopt -s cmdhist
83 shopt -s globstar
84
85
86 # inside emacs fixes
87 if [[ $INSIDE_EMACS ]]; then
88 export INSIDE_EMACS
89 export PAGER=cat
90 export MANPAGER=cat
91 # for readline-complete.el
92 stty echo
93 # todo, remote file completion fails, figure out how to turn it off
94 fi
95
96
97 if [[ $- == *i* ]]; then
98 # for readline-complete.el
99 if [[ $INSIDE_EMACS ]]; then
100 bind 'set horizontal-scroll-mode on'
101 bind 'set print-completions-horizontally on'
102 bind '"\C-i": self-insert'
103 else
104 # arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
105 if [[ $TERM == "xterm" ]]; then
106 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
107 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
108 else
109 bind '"\eOc": shell-forward-word'
110 bind '"\eOd": shell-backward-word'
111 fi
112 # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
113 # gnome-terminal, overridden by bash, or disabled by the system
114 stty werase undef lnext undef stop undef start undef
115
116 fi
117
118 fi
119
120
121 # history number. History expansion is good.
122 PS4='$LINENO+ '
123 # history file size limit, set to unlimited.
124 # this needs to be different from the default because
125 # default HISTFILESIZE is 500 and could clobber our history
126 HISTFILESIZE=
127 # max commands 1 session can append/read from history
128 HISTSIZE=100000
129 # my own history size limit based on lines
130 HISTFILELINES=1000000
131 HISTFILE=$HOME/.bh
132 # the time format display when doing the history command
133 # also, setting this makes the history file record time
134 # of each command as seconds from the epoch
135 HISTTIMEFORMAT="%I:%M %p %m/%d "
136 # consecutive duplicate lines don't go in history
137 HISTCONTROL=ignoredups
138 # works in addition to HISTCONTROL to do more flexible things
139 # it could also do the same things as HISTCONTROL and thus replace it,
140 # but meh
141 HISTIGNORE="k *"
142
143 export BC_LINE_LENGTH=0
144
145
146 # note, if I use a machine I don't want files readable by all users, set
147 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
148
149 C_DEFAULT_DIR=/a
150
151
152
153
154 ###################
155 ## include files ###
156 ###################
157
158 for x in $HOME/bin/bash-programs-by-ian/repos/*/*-function; do
159 source "$x"
160 done
161 source $HOME/identify-distro-functions
162 source $HOME/bin/semi-private # so I can share my bashrc
163 source $HOME/path_add-function
164 path_add --ifexists /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
165 path_add $HOME/bin/bash-programs-by-ian/utils
166
167
168 ###############
169 ### aliases ###
170 ###############
171
172 if [[ $- == *i* ]]; then
173 alias cp='cp -i'
174 alias mv='mv -i'
175 fi
176
177 # remove any default aliases for these
178 alias ls > /dev/null 2>&1 && unalias ls
179 alias ll > /dev/null 2>&1 && unalias ll
180 alias grep > /dev/null 2>&1 && unalias grep
181
182
183 mkdir() {
184 command mkdir -p "$@"
185 }
186
187
188 alias d='builtin bg'
189 complete -A stopped -P '"%' -S '"' d
190
191 alias hi='history'
192
193
194 # note: gksudo is recommended for X apps because it does not set the
195 # home directory to the same.
196
197 if [[ $- == *i* ]]; then
198 # extra space at the end allows aliases to work
199 alias s='SUDOD="$PWD" sudo -i '
200 else
201 s() {
202 if [[ $EUID != 0 || $1 == -* ]]; then
203 local SUDOD="$PWD"
204 sudo -i "$@"
205 else
206 "$@"
207 fi
208 }
209 fi
210
211
212
213 if [[ $OS == Windows_NT ]]; then
214 alias ffs='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
215 export DISPLAY=nt
216 alias j='command cygpath'
217 alias t='command cygstart'
218 alias cygstart='echo be quick, use the alias "t" instead :\)'
219 alias cygpath='echo be quick, use the alias "j" instead :\)'
220
221 fi
222
223
224
225
226
227 #####################
228 ### functions ####
229 #####################
230
231 # netselect-apt finds a fast mirror.
232 # but we need to replace the mirrors ourselves,
233 # because it doesn't do that. best it can do is
234 # output a basic sources file
235 # here we get the server it found, get the main server we use
236 # then substitute all instances of one for the other in the sources file
237 # and backup original to /etc/apt/sources.list-original.
238 # this is idempotent
239 debian_pick_mirror () {
240 local x=$(mktemp -d)/f # safe way to get file name without creating one
241 sudo netselect-apt -o "$x" || return 1
242 x=$(_debian_pick_mirror_grep stable "$x")
243 sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
244 sudo sed -i "s/$(_debian_pick_mirror_grep wheezy)/$x/" /etc/apt/sources.list
245 aptitude update
246 }
247
248 _debian_pick_mirror_grep () {
249 local x="$(grep -oP "^deb [^ ]+ $1 " ${2-/etc/apt/sources.list})"
250 x="${x#deb }"
251 x="${x% $1 }"
252 # replace / with \/ so we can use it with sed
253 echo "${x//\//\\/}"
254 }
255
256 a() {
257 beet "${@}"
258 }
259
260
261 t() {
262 trash-put "$@"
263 }
264
265
266 if type ack-grep >/dev/null 2>&1; then
267 alias ack=ack-grep
268 fi
269
270
271 gr() {
272 grep -i --binary-files=without-match --color=auto "$@"
273 }
274
275 grr() {
276 grep -ri --binary-files=without-match --color=auto "$@"
277 }
278
279 bashrcpush () {
280 local startdir="$PWD"
281 cd ~
282 for x in "$@"; do
283 ssh $x mkdir -p bin
284 tar cz bin/bash-programs-by-ian bin/semi-private | ssh $x tar xz
285 done
286 cd $(mktemp -d)
287 cp /a/c/repos/bash/!(.git) .
288 for x in "$@"; do
289 tar cz * | ssh $x tar xz
290 done
291 cd "$startdir"
292 }
293
294 # history search
295 k() { grep -P "$@" ${HISTFILE:-~/.bash_history} | tail -n 40; }
296
297 calc() { echo "scale=3; $*" | bc -l; }
298
299
300 # makes it so chown -R symlink affects the symlink and its target.
301 chown() {
302 if [[ $1 == -R ]]; then
303 shift
304 command chown -h "$@"
305 command chown "$@"
306 command chown -RH "$@"
307 else
308 command chown "$@"
309 fi
310 }
311
312
313
314 cgpl ()
315 {
316 if [[ $# == 0 ]]; then
317 cp /a/bin/data/COPYING .
318 else
319 cp /a/bin/data/COPYING "$@"
320 fi
321 }
322
323
324 dc() {
325 diff --strip-trailing-cr -w "$@" # diff content
326 }
327
328
329 dt() {
330 date "+%A, %B %d, %r" "$@"
331 }
332
333
334 e() { echo "$@"; }
335
336
337 envload() { # load environment from a previous: export > file
338 local file=${1:-$HOME/.${USER}_env}
339 eval "$(export | sed 's/^declare -x/export -n/')"
340 while IFS= read -r line; do
341 # declare -x makes variables local to a function
342 eval ${line/#declare -x/export}
343 done < "$file"
344 }
345
346
347
348 # havn't tested these:
349 #file cut copy and paste, like the text buffers :)
350 _fbufferinit() { # internal use by
351 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
352 rm -rf "$my_f_tempdir"/*
353 }
354 fcp() { # file cp
355 _fbufferinit
356 cp "$@" "$my_f_tempdir"/
357 }
358 fct() { # file cut
359 _fbufferinit
360 mv "$@" "$my_f_tempdir"/
361 }
362 fpst() { # file paste
363 [[ $2 ]] && { echo too many arguments; return 1; }
364 target=${1:-.}
365 cp "$my_f_tempdir"/* "$target"
366 }
367
368
369 # find array. make an array of file names found by find into $x
370 # argument: find arguments
371 # return: find results in an array $x
372 fa() {
373 while read -rd ''; do
374 x+=("$REPLY");
375 done < <(find "$@" -print0);
376 }
377
378
379 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
380 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
381 local gitroot
382 gitroot || return 1 # function to set gitroot
383 builtin cd "$gitroot"
384 git symbolic-ref HEAD refs/heads/$1
385 rm .git/index
386 git clean -fdx
387 }
388
389 fw() {
390 firefox -P default "$@" >/dev/null 2>&1
391 }
392
393 fn() {
394 firefox -P alt "$@" >/dev/null 2>&1
395 }
396
397
398
399
400
401 # horizontal row. used to break up output
402 hr() { printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $(seq $COLUMNS); }
403
404
405 i() {
406 git "$@"
407 }
408 # modified from ~/local/bin/git-completion.bash
409 # other completion commands are mostly taken from bash_completion package
410 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
411 || complete -o default -o nospace -F _git i
412
413 # fast commit
414 ic() {
415 git commit -am "$*"
416 }
417
418 # insensitive find
419 ifn () {
420 find . -iname '*'"$*"'*'
421 }
422
423
424
425
426 l() {
427 if [[ $PWD == /[iap] ]]; then
428 command ls -A --color=auto -I lost+found "$@"
429 else
430 command ls -A --color=auto "$@"
431 fi
432 }
433
434
435 lld() { ll -d "$@"; }
436
437
438 low() { # make filenames all lowercase
439 local x y
440 for x in "$@"; do
441 y=$(tr "[A-Z]" "[a-z]" <<<"$x")
442 [[ $y != $x ]] && mv "$x" "$y"
443 done
444 }
445
446
447 lower() { # make first letter of filenames lowercase.
448 local x
449 for x in "$@"; do
450 if [[ ${x::1} == [A-Z] ]]; then
451 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
452 safe_rename "$x" "$y"
453 fi
454 done
455 }
456
457 safe_rename() {
458 if [[ $# != 2 ]]; then
459 echo safe_rename error: $# args, need 2 >2
460 return 1
461 elif [[ $1 != $2 ]]; then
462 if [[ -e $2 ]]; then
463 echo Cannot rename "$1" to "$2" as it already exists.
464 else
465 mv "$1" "$2"
466 fi
467 fi
468 }
469
470 despace() {
471 local x y
472 for x in "$@"; do
473 y="${x// /_}"
474 safe_rename "$x" "$y"
475 done
476 }
477
478
479
480
481 # package manager
482 # aliases would be much more compact, but they can't be used as ssh commands
483 # also, to be used in a script, you need -i which prints annoying
484 # warnings. instead, use -l in a script to source this file
485 if type -p yum > /dev/null; then
486 p() {
487 if [[ $EUID == 0 ]]; then
488 yum "$@"
489 else
490 sudo yum "$@"
491 fi
492 }
493 pi() {
494 if [[ $EUID == 0 ]]; then
495 yum -y install "$@"
496 else
497 sudo yum -y install "$@"
498 fi
499 }
500 pf() {
501 if [[ $EUID == 0 ]]; then
502 yum search "$@"
503 else
504 sudo yum search "$@"
505 fi
506 }
507 else
508 p() {
509 if [[ $EUID == 0 ]]; then
510 aptitude "$@"
511 else
512 sudo aptitude "$@"
513 fi
514 }
515 pi() {
516 if [[ $EUID == 0 ]]; then
517 aptitude -y install "$@"
518 else
519 sudo aptitude -y install "$@"
520 fi
521 }
522 pf() {
523 # scratch a very annoying itch.
524 # package description width as wide as the screen, and package name field small
525 # aptitude manual can't figure out how wide emacs terminal is,
526 # of course it doesn't consult the $COLUMNS variable...
527 # and in a normal terminal, it makes the package name field ridiculously big
528 # also, remove that useless dash before the description
529 if [[ $EUID == 0 ]]; then
530 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
531 else
532 sudo aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
533 fi
534 }
535 fi
536
537
538 # test existence / exists
539 te() {
540 local ret=0
541 for x in "$@"; do
542 [[ -e "$x" || -L "$x" ]] || ret=1
543 done
544 return $ret
545 }
546
547
548 # fix root file ownership for FILE argument.
549 # check if parent or grandparent is not root and if the dir of FILE is also
550 # owned by that user, and change ownership to that user
551 perm_fix() {
552 local parent
553 if [[ $EUID == 0 ]]; then
554 te "$1" || touch "$1"
555 if [[ $(stat -c "%u" "$1") == 0 ]] ; then
556 argdir=$(getdir "$1")
557 if [[ $(stat -c "%u" "$argdir") != 0 ]] ; then
558 if ! chown "--reference=$argdir" "$1"; then
559 echo failed to fix bad ownership file permissons
560 return 1
561 fi
562 fi
563 fi
564 fi
565 }
566
567 pfind() { #find *$1* in $PATH
568 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
569 local pathArray
570 IFS=: pathArray=($PATH); unset IFS
571 find "${pathArray[@]}" -iname "*$1*"
572 }
573
574
575 pwd() { # do pwd + some other info.
576 echo "$(ll -d "$PWD") $USER@$HOSTNAME $(date +%r)"
577 }
578
579
580 pwgen() { # generate a random password, with digits & punctuation and without
581 arg=${1:-50}
582 head -c 200 /dev/urandom | tr -cd '[:graph:]' | head -c "$arg"
583 echo
584 head -c 200 /dev/urandom | tr -cd '[:alnum:]' | head -c "$arg"
585 echo
586 }
587
588 q() { # start / launch a program in the backround and redir output to null
589 "$@" &> /dev/null &
590 }
591
592
593
594 r() {
595 exit "$@" 2>/dev/null
596 }
597
598 # trash-restore lists everything that has been trashed at or below CWD
599 # This picks out files just in CWD, not subdirectories,
600 # which also match grep $1, usually use $1 for a time string
601 # which you get from running restore-trash once first
602 pick-trash() {
603 local name x ask
604 local nth=1
605 # last condition is to not ask again for ones we skipped
606 while name="$( echo | restore-trash | gr "$PWD/[^/]\+$" | gr "$1" )" \
607 && [[ $name ]] && (( $(wc -l <<<"$name") >= nth )); do
608 name="$(echo "$name" | head -n $nth | tail -n 1 )"
609 read -p "$name [Y/n] " ask
610 if [[ ! $ask || $ask == [Yy] ]]; then
611 x=$( echo "$name" | gr -o "^\s*[0-9]*" )
612 echo $x | restore-trash > /dev/null
613 elif [[ $ask == [Nn] ]]; then
614 nth=$((nth+1))
615 else
616 return
617 fi
618 done
619 }
620
621 # rsync, root is required to keep permissions right.
622 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
623 # --no-times --delete
624 # basically, make an exact copy, use checksums instead of file times to be more accurate
625 rl() { rsync -ahvic --delete "$@"; }
626 # don't delete files on the target end which do not exist on the original end:
627 rld() { rsync -ahvic "$@"; }
628 complete -F _rsync -o nospace rld rlt fl
629 # rl without preserving modification time. for some reason I had this as default before.
630 # perhaps that reason will come up again and I will document it.
631 rlt() { rsync -ahvic --delete --no-t "$@"; }
632
633
634
635 # use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
636 sb() {
637 local SUDOD="$PWD"
638 sudo -i bash -c "$@"
639 }
640 complete -F _root_command s sb
641
642 # use -ll, less secure but faster.
643 srm () {
644 command srm -ll "$@"
645 }
646
647 # sudo redo. be aware, this command may not work right on strange distros or earlier software
648 sr() {
649 if [[ $# == 0 ]]; then
650 sudo -E bash -c -l "$(history -p '!!')"
651 else
652 echo this command redos last history item. no argument is accepted
653 fi
654 }
655
656
657
658 # log with script. timing is $1.t and script is $1.s
659 # -l to save to ~/typescripts/
660 # -t to add a timestamp to the filenames
661 slog() {
662 local logdir do_stamp arg_base
663 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
664 logdir="/a/dt/"
665 do_stamp=false
666 while getopts "lt" option
667 do
668 case $option in
669 l ) arg_base=$logdir ;;
670 t ) do_stamp=true ;;
671 esac
672 done
673 shift $(($OPTIND - 1))
674 arg_base+=$1
675 [[ -e $logdir ]] || mkdir -p $logdir
676 $do_stamp && arg_base+=$(date +%F.%T%z)
677 script -t $arg_base.s 2> $arg_base.t
678 }
679 splay() { # script replay
680 #logRoot="$HOME/typescripts/"
681 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
682 scriptreplay "$1.t" "$1.s"
683 }
684
685
686
687 # timer in minutes
688 tm() {
689 (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
690 }
691
692
693 ts() { # start editing a new file
694 [[ $# != 1 ]] && echo "I need a filename." && return 1
695 local quiet
696 if [[ $- != *i* ]]; then
697 quiet=true
698 fi
699 if [[ $1 == *.c ]]; then
700 e '#include <stdio.h>' >"$1"
701 e '#include <stdlib.h>' >>"$1"
702 e 'int main(int argc, char * argv[]) {' >>"$1"
703 e ' printf( "hello world\n");' >>"$1"
704 e ' return 0;' >>"$1"
705 e '}' >>"$1"
706 e "${1%.c}: $1" > Makefile
707 e " g++ -ggdb -std=gnu99 -o ${1%.c} $<" >> Makefile
708 e "#!/bin/bash" >run.sh
709 e "./${1%.c}" >>run.sh
710 chmod +x run.sh
711 elif [[ $1 == *.java ]]; then
712 e "public class ${1%.*} {" >"$1"
713 e ' public static void main(String[] args) {' >>"$1"
714 e ' System.out.println("Hello, world!");' >>"$1"
715 e ' }' >>"$1"
716 e '}' >>"$1"
717
718 else
719 echo "#!/bin/bash" > "$1"
720 chmod +x "$1"
721 fi
722 [[ $quiet ]] || g "$1"
723
724 }
725
726 tx() { # toggle set -x, and the prompt so it doesn't spam
727 if [[ $- == *x* ]]; then
728 set +x
729 PROMPT_COMMAND=prompt_command
730 else
731 unset PROMPT_COMMAND
732 PS1="\w \$ "
733 set -x
734 fi
735 }
736
737
738
739
740 if [[ $OS == Windows_NT ]]; then
741 # cygstart wrapper
742 cs() {
743 cygstart "$@" &
744 }
745 xp() {
746 explorer.exe .
747 }
748 # launch
749 o() {
750 local x=(*$1*)
751 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
752 cygstart *$1* &
753 }
754 else
755 o() {
756 if type gvfs-open &> /dev/null ; then
757 gvfs-open "$@"
758 else
759 xdg-open "$@"
760 fi
761 # another alternative is run-mailcap
762 }
763 fi
764
765
766
767
768 # todo, update this
769 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
770
771
772
773 hl() { # history limit. Write extra history to archive file.
774 local max_lines linecount tempfile prune_lines
775 local harchive="${HISTFILE}_archive"
776 for x in "$HISTFILE" "$harchive"; do
777 [[ -e $x ]] || { touch "$x" && echo "notice from hl(): creating $x"; }
778 if [[ ! $x || ! -e $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
779 echo "error in hl: history file \$x:$x no good"
780 return 1
781 fi
782 done
783 history -a # save history
784 max_lines=$HISTFILELINES
785 [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
786 linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
787 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
788 if (($linecount > $max_lines)); then
789 prune_lines=$(($linecount - $max_lines))
790 head -n $prune_lines "$HISTFILE" >> "$harchive" \
791 && sed -ie "1,${prune_lines}d" $HISTFILE
792 fi
793 }
794
795 if [[ $- == *i* ]]; then
796 # commands to run when bash exits normally
797 trap "hl; smh" EXIT
798 fi
799
800
801 # temporary variables to test colorization
802 # some copied from gentoo /etc/bash/bashrc,
803 use_color=false
804 # dircolors --print-database uses its own built-in database
805 # instead of using /etc/DIR_COLORS. Try to use the external file
806 # first to take advantage of user additions.
807 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
808 match_lhs=""
809 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
810 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
811 [[ -z ${match_lhs} ]] \
812 && type -P dircolors >/dev/null \
813 && match_lhs=$(dircolors --print-database)
814 # test if our $TERM is in the TERM values in dircolor
815 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
816
817
818 if ${use_color} && [[ $- == *i* ]]; then
819
820 if [[ $XTERM_VERSION == Cygwin* ]]; then
821 get_term_color() {
822 for x in "$@"; do
823 case $x in
824 underl) echo -n $'\E[4m' ;;
825 bold) echo -n $'\E[1m' ;;
826 red) echo -n $'\E[31m' ;;
827 green) echo -n $'\E[32m' ;;
828 blue) echo -n $'\E[34m' ;;
829 cyan) echo -n $'\E[36m' ;;
830 yellow) echo -n $'\E[33m' ;;
831 purple) echo -n $'\E[35m' ;;
832 nocolor) echo -n $'\E(B\E[m' ;;
833 esac
834 done
835 }
836
837 else
838 get_term_color() {
839 for x in "$@"; do
840 case $x in
841 underl) echo -n $(tput smul) ;;
842 bold) echo -n $(tput bold) ;;
843 red) echo -n $(tput setaf 1) ;;
844 green) echo -n $(tput setaf 2) ;;
845 blue) echo -n $(tput setaf 4) ;;
846 cyan) echo -n $(tput setaf 6) ;;
847 yellow) echo -n $(tput setaf 3) ;;
848 purple) echo -n $(tput setaf 5) ;;
849 nocolor) echo -n $(tput sgr0) ;; # no font attributes
850 esac
851 done
852 }
853 fi
854 else
855 get_term_color() {
856 :
857 }
858 fi
859 # Try to keep environment pollution down, EPA loves us.
860 unset safe_term match_lhs use_color
861
862
863
864
865
866
867 ###############
868 # prompt ######
869 ###############
870
871
872 if [[ $- == *i* ]]; then
873 # git branch/status prompt function
874 if [[ $OS != Windows_NT ]]; then
875 GIT_PS1_SHOWDIRTYSTATE=true
876 fi
877 # arch source location
878 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
879 # fedora/debian source
880 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
881
882 # in case we didn't source git-prompt.sh
883 if ! declare -f __git_ps1 > /dev/null; then
884 __git_ps1() {
885 :
886 }
887 fi
888
889 # this needs to come before next ps1 stuff
890 # this stuff needs bash 4, feb 2009,
891 # old enough to no longer condition on $BASH_VERSION anymore
892 shopt -s autocd
893 shopt -s dirspell
894 PS1='\w'
895 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
896 PROMPT_DIRTRIM=2
897 bind -m vi-command B:shell-backward-word
898 bind -m vi-command W:shell-forward-word
899 fi
900
901 if [[ $SSH_CLIENT ]]; then
902 PS1="\h $PS1"
903 fi
904
905 prompt_command() {
906 local return=$? # this MUST COME FIRST
907 local psc pst
908 local ps_char ps_color
909 unset IFS
910 history -a # save history
911 history -n # read any new history
912 if [[ ! $DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
913 # from the screen man page
914 if [[ $TERM == screen* ]]; then
915 local title_escape="\033]..2;"
916 else
917 local title_escape="\033]0;"
918 fi
919 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
920 fi
921
922 case $return in
923 0) ps_color="$(get_term_color blue)"
924 ps_char='\$'
925 ;;
926 1) ps_color="$(get_term_color green)"
927 ps_char="$return \\$"
928 ;;
929 *) ps_color="$(get_term_color yellow)"
930 ps_char="$return \\$"
931 ;;
932 esac
933 if [[ ! -O . ]]; then # not owner
934 if [[ -w . ]]; then # writable
935 ps_color="$(get_term_color bold red)"
936 else
937 ps_color="$(get_term_color bold green)"
938 fi
939 fi
940 PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
941 }
942 PROMPT_COMMAND=prompt_command
943 fi
944
945
946
947
948
949 ###########################################
950 # stuff that makes sense to be at the end #
951 ###########################################
952 if [[ "$SUDOD" ]]; then
953 cd "$SUDOD"
954 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
955 cd /a
956 fi
957
958
959 # best practice
960 unset IFS
961
962
963 # if someone exported $SOE, catch errors
964 if [[ $SOE ]]; then
965 errcatch
966 fi