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