improve grep functions
[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 can override with ssh -t which sets $SSH_TTY, which we can detect
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
200 gr() {
201 grep -i --binary-files=without-match --color=auto "$@"
202 }
203
204 gr() {
205 gr -r "$@"
206 }
207
208
209
210
211
212
213 calc() { echo "scale=3; $*" | bc -l; }
214
215 cd() {
216 if [[ $1 == .. ]]; then
217 echo 'be cool, use the alias ".." instead :)'
218 fi
219 builtin cd "$@"
220 }
221
222
223 # makes it so chown -R symlink affects the symlink and its target.
224 chown() {
225 if [[ $1 == -R ]]; then
226 shift
227 command chown -h "$@"
228 command chown "$@"
229 command chown -RH "$@"
230 else
231 command chown "$@"
232 fi
233 }
234
235
236
237 cgpl ()
238 {
239 if [[ $# == 0 ]]; then
240 cp /a/bin/data/COPYING .
241 else
242 cp /a/bin/data/COPYING "$@"
243 fi
244 }
245
246
247 dc() {
248 diff --strip-trailing-cr -w "$@" # diff content
249 }
250
251
252 distro_name() {
253 if [[ -f /etc/fedora-release ]]; then
254 echo fedora
255 else
256 grep "^ID=.*" /etc/os-release | sed 's/^ID=//'
257 fi
258 }
259
260
261 dt() {
262 date "+%A, %B %d, %r" "$@"
263 }
264
265
266 e() { echo "$@"; }
267
268
269 envload() { # load environment from a previous: export > file
270 local file=${1:-$HOME/.${USER}_env}
271 eval "$(export | sed 's/^declare -x/export -n/')"
272 while IFS= read -r line; do
273 # declare -x makes variables local to a function
274 eval ${line/#declare -x/export}
275 done < "$file"
276 }
277
278
279
280 # havn't tested these:
281 #file cut copy and paste, like the text buffers :)
282 _fbufferinit() { # internal use by
283 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
284 rm -rf "$my_f_tempdir"/*
285 }
286 fcp() { # file cp
287 _fbufferinit
288 cp "$@" "$my_f_tempdir"/
289 }
290 fct() { # file cut
291 _fbufferinit
292 mv "$@" "$my_f_tempdir"/
293 }
294 fpst() { # file paste
295 [[ $2 ]] && { echo too many arguments; return 1; }
296 target=${1:-.}
297 cp "$my_f_tempdir"/* "$target"
298 }
299
300
301 # find array. make an array of file names found by find into $x
302 # argument: find arguments
303 # return: find results in an array $x
304 fa() {
305 while read -rd ''; do
306 x+=("$REPLY");
307 done < <(find "$@" -print0);
308 }
309
310
311 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
312 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
313 local gitroot
314 gitroot || return 1 # function to set gitroot
315 builtin cd $gitroot
316 git symbolic-ref HEAD refs/heads/$1
317 rm .git/index
318 git clean -fdx
319 }
320
321 fw() {
322 firefox -P default "$@" >/dev/null 2>&1
323 }
324
325 fn() {
326 firefox -P alt "$@" >/dev/null 2>&1
327 }
328
329
330
331
332
333 # horizontal row. used to break up output
334 hr() { printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq $COLUMNS); }
335
336
337 i() {
338 git "$@"
339 }
340 # modified from ~/local/bin/git-completion.bash
341 # other completion commands are mostly taken from bash_completion package
342 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
343 || complete -o default -o nospace -F _git i
344
345
346 # insensitive find
347 ifn () {
348 find . -iname '*'"$*"'*'
349 }
350
351
352
353 l() {
354 if [[ $PWD == /[iap] ]]; then
355 command ls -A --color=auto -I lost+found "$@"
356 else
357 command ls -A --color=auto "$@"
358 fi
359 }
360
361
362 lld() { ll -d "$@"; }
363
364
365 low() { # make filenames all lowercase
366 local x y
367 for x in "$@"; do
368 y=$(tr "[A-Z]" "[a-z]" <<<"$x")
369 [[ $y != $x ]] && mv "$x" "$y"
370 done
371 }
372
373
374 lower() { # make first letter of filenames lowercase.
375 local x
376 for x in "$@"; do
377 if [[ ${x::1} == [A-Z] ]]; then
378 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
379 safe_rename "$x" "$y"
380 fi
381 done
382 }
383
384 safe_rename() {
385 if [[ $# != 2 ]]; then
386 echo safe_rename error: $# args, need 2 >2
387 return 1
388 elif [[ $1 != $2 ]]; then
389 if [[ -e $2 ]]; then
390 echo Cannot rename "$1" to "$2" as it already exists.
391 else
392 mv "$1" "$2"
393 fi
394 fi
395 }
396
397 despace() {
398 local x y
399 for x in "$@"; do
400 y="${x// /_}"
401 safe_rename "$x" "$y"
402 done
403 }
404
405 # force symbolic link creation.
406 # trash-put any existing targets,
407 # then send all arguments to ln -s
408 lnf() {
409 if [[ $# -gt 1 && -d ${!#} ]]; then
410 local oldcwd=$PWD
411 cd ${!#} # last arg
412 for x in "${@:1:$(($#-1))}"; do # all but last arg
413 # a broken symlink will fail the "exists" -e test
414 [[ -e "${x##*/}" || -L "${x##*/}" ]] && trash-put "${x##*/}"
415 done
416 cd "$oldcwd"
417 elif [[ $# -eq 2 ]]; then
418 [[ -e "$2" || -L "$2" ]] && rm "$2"
419 else
420 [[ -e "${1##*/}" || -L "${1##*/}" ]] && rm "${1##*/}"
421 fi
422 ln -s "$@"
423 }
424
425
426
427 # package manager
428 # aliases would be much more compact, but they can't be used as ssh commands
429 # also, to be used in a script, you need -i which prints annoying
430 # warnings. instead, use -l in a script to source this file
431 if type -p yum > /dev/null; then
432 p() {
433 if [[ $EUID == 0 ]]; then
434 yum "$@"
435 else
436 sudo yum "$@"
437 fi
438 }
439 pi() {
440 if [[ $EUID == 0 ]]; then
441 yum -y install "$@"
442 else
443 sudo yum -y install "$@"
444 fi
445 }
446 pf() {
447 if [[ $EUID == 0 ]]; then
448 yum search "$@"
449 else
450 sudo yum search "$@"
451 fi
452 }
453 else
454 p() {
455 if [[ $EUID == 0 ]]; then
456 aptitude "$@"
457 else
458 sudo aptitude "$@"
459 fi
460 }
461 pi() {
462 if [[ $EUID == 0 ]]; then
463 aptitude -y install "$@"
464 else
465 sudo aptitude -y install "$@"
466 fi
467 }
468 pf() {
469 if [[ $EUID == 0 ]]; then
470 aptitude search "$@"
471 else
472 sudo aptitude search "$@"
473 fi
474 }
475 fi
476
477
478
479
480
481 # fix root file ownership for FILE argument.
482 # check if parent or grandparent is not root and if the dir of FILE is also
483 # owned by that user, and change ownership to that user
484 perm_fix() {
485 local parent
486 if [[ $EUID == 0 ]]; then
487 [[ -e $1 ]] || touch $1
488 if [[ $(stat -c "%u" "$1") == 0 ]] ; then
489
490 argdir=$(dirstrip "$1")
491 if [[ $(stat -c "%u" "$argdir") != 0 ]] ; then
492 if ! chown "--reference=$argdir" "$1"; then
493 echo failed to fix bad ownership file permissons
494 return 1
495 fi
496 fi
497 fi
498 fi
499 }
500
501 pfind() { #find *$1* in $PATH
502 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
503 local pathArray
504 IFS=: pathArray=($PATH); unset IFS
505 find "${pathArray[@]}" -iname "*$1*"
506 }
507
508
509 pwd() { # do pwd + some other info.
510 echo "$(ll -d "$PWD") $USER@$HOSTNAME $(date +%r)"
511 }
512
513
514 pwgen() { # generate a random password, with digits & punctuation and without
515 arg=${1:-50}
516 head -c 200 /dev/urandom | tr -cd '[:graph:]' | head -c "$arg"
517 echo
518 head -c 200 /dev/urandom | tr -cd '[:alnum:]' | head -c "$arg"
519 echo
520 }
521
522 q() { # start / launch a program in the backround and redir output to null
523 "$@" &> /dev/null &
524 }
525
526
527
528 r() {
529 exit "$@"
530 }
531
532 # rsync, root is required to keep permissions right.
533 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
534 # --no-times --delete
535 # basically, make an exact copy, use checksums instead of file times to be more accurate
536 rl() { rsync -ahvic --delete "$@"; }
537 # don't delete files on the target end which do not exist on the original end:
538 rld() { rsync -ahvic "$@"; }
539 complete -F _rsync -o nospace rld rlt fl
540 # rl without preserving modification time. for some reason I had this as default before.
541 # perhaps that reason will come up again and I will document it.
542 rlt() { rsync -ahvic --delete --no-t "$@"; }
543
544
545
546 # use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
547 sb() {
548 local SUDOD="$PWD"
549 sudo -i bash -c "$@"
550 }
551 complete -F _root_command s sb
552
553 # use -ll, less secure but faster.
554 srm () {
555 srm -ll "$@"
556 }
557
558 # sudo redo. be aware, this command may not work right on strange distros or earlier software
559 sr() {
560 if [[ $# == 0 ]]; then
561 sudo -E bash -c -l "$(history -p '!!')"
562 else
563 echo this command redos last history item. no argument is accepted
564 fi
565 }
566
567
568
569 # log with script. timing is $1.t and script is $1.s
570 # -l to save to ~/typescripts/
571 # -t to add a timestamp to the filenames
572 slog() {
573 local logdir do_stamp arg_base
574 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
575 logdir="/a/dt/"
576 do_stamp=false
577 while getopts "lt" option
578 do
579 case $option in
580 l ) arg_base=$logdir ;;
581 t ) do_stamp=true ;;
582 esac
583 done
584 shift $(($OPTIND - 1))
585 arg_base+=$1
586 [[ -e $logdir ]] || mkdir -p $logdir
587 $do_stamp && arg_base+=$(date +%F.%T%z)
588 script -t $arg_base.s 2> $arg_base.t
589 }
590 splay() { # script replay
591 #logRoot="$HOME/typescripts/"
592 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
593 scriptreplay "$1.t" "$1.s"
594 }
595
596
597
598 # timer in minutes
599 tm() {
600 (sleep $(calc "$@ * 60") && mpv /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
601 }
602
603
604 ts() { # start editing a new file
605 [[ $# != 1 ]] && echo "I need a filename." && return 1
606 local quiet
607 if [[ $- != *i* ]]; then
608 quiet=true
609 fi
610 if [[ $1 == *.c ]]; then
611 e '#include <stdio.h>' >"$1"
612 e '#include <stdlib.h>' >>"$1"
613 e 'int main(int argc, char * argv[]) {' >>"$1"
614 e ' printf( "hello world\n");' >>"$1"
615 e ' return 0;' >>"$1"
616 e '}' >>"$1"
617 e "${1%.c}: $1" > Makefile
618 e " g++ -ggdb -std=gnu99 -o ${1%.c} $<" >> Makefile
619 e "#!/bin/bash" >run.sh
620 e "./${1%.c}" >>run.sh
621 chmod +x run.sh
622 elif [[ $1 == *.java ]]; then
623 e "public class ${1%.*} {" >"$1"
624 e ' public static void main(String[] args) {' >>"$1"
625 e ' System.out.println("Hello, world!");' >>"$1"
626 e ' }' >>"$1"
627 e '}' >>"$1"
628
629 else
630 echo "#!/bin/bash" > "$1"
631 chmod +x "$1"
632 fi
633 [[ $quiet ]] || g "$1"
634
635 }
636
637 tx() { # toggle set -x, and the prompt so it doesn't spam
638 if [[ $- == *x* ]]; then
639 set +x
640 PROMPT_COMMAND=prompt_command
641 else
642 unset PROMPT_COMMAND
643 PS1="\w \$ "
644 set -x
645 fi
646 }
647
648
649
650
651 if [[ $OS == Windows_NT ]]; then
652 # cygstart wrapper
653 cs() {
654 cygstart "$@" &
655 }
656 xp() {
657 explorer.exe .
658 }
659 # launch
660 o() {
661 local x=(*$1*)
662 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
663 cygstart *$1* &
664 }
665 else
666 o() {
667 if type gvfs-open &> /dev/null ; then
668 gvfs-open "$@"
669 else
670 xdg-open "$@"
671 fi
672 # another alternative is run-mailcap
673 }
674 fi
675
676
677 # todo, update this
678 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
679
680
681
682
683 hl() { # history limit. Write extra history to archive file.
684 local max_lines linecount tempfile
685 if ! [[ -w $HISTFILE ]] || ! [[ -w ${HISTFILE}_archive ]]; then
686 echo "error: a history file is not writable."
687 return 1
688 fi
689 history -w
690 if [[ $1 ]]; then
691 max_lines=$(($1 * 2)) # 2 lines for every history command
692 else
693 max_lines=1000000
694 fi
695 linecount=$(wc -l < $HISTFILE)
696 linecount=${linecount:-0}
697 if (($linecount > $max_lines)); then
698 prune_lines=$(($linecount - $max_lines))
699 tempfile=$(mktemp)
700 [[ $tempfile ]] || { echo mktemp failed; return 1; }
701 head -$prune_lines $HISTFILE >> ${HISTFILE}a \
702 && sed -e "1,${prune_lines}d" $HISTFILE > $tempfile \
703 && mv $tempfile $HISTFILE
704 fi
705 perm_fix $HISTFILE
706 perm_fix ${HISTFILE}_archive
707 history -c
708 history -r
709 }
710 # run hl when bash exits normally
711 trap hl EXIT
712
713
714
715 # temporary variables to test colorization
716 # some copied from gentoo /etc/bash/bashrc,
717 use_color=false
718 # dircolors --print-database uses its own built-in database
719 # instead of using /etc/DIR_COLORS. Try to use the external file
720 # first to take advantage of user additions.
721 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
722 match_lhs=""
723 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
724 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
725 [[ -z ${match_lhs} ]] \
726 && type -P dircolors >/dev/null \
727 && match_lhs=$(dircolors --print-database)
728 # test if our $TERM is in the TERM values in dircolor
729 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
730
731
732 if ${use_color} && [[ $- == *i* ]]; then
733
734 if [[ $XTERM_VERSION == Cygwin* ]]; then
735 get_term_color() {
736 for x in "$@"; do
737 case $x in
738 underl) echo -n $'\E[4m' ;;
739 bold) echo -n $'\E[1m' ;;
740 red) echo -n $'\E[31m' ;;
741 green) echo -n $'\E[32m' ;;
742 blue) echo -n $'\E[34m' ;;
743 cyan) echo -n $'\E[36m' ;;
744 yellow) echo -n $'\E[33m' ;;
745 purple) echo -n $'\E[35m' ;;
746 nocolor) echo -n $'\E(B\E[m' ;;
747 esac
748 done
749 }
750
751 else
752 get_term_color() {
753 for x in "$@"; do
754 case $x in
755 underl) echo -n $(tput smul) ;;
756 bold) echo -n $(tput bold) ;;
757 red) echo -n $(tput setaf 1) ;;
758 green) echo -n $(tput setaf 2) ;;
759 blue) echo -n $(tput setaf 4) ;;
760 cyan) echo -n $(tput setaf 6) ;;
761 yellow) echo -n $(tput setaf 3) ;;
762 purple) echo -n $(tput setaf 5) ;;
763 nocolor) echo -n $(tput sgr0) ;; # no font attributes
764 esac
765 done
766 }
767 fi
768 else
769 get_term_color() {
770 :
771 }
772 fi
773 # Try to keep environment pollution down, EPA loves us.
774 unset safe_term match_lhs use_color
775
776
777
778
779
780
781 ###############
782 # prompt ######
783 ###############
784
785
786 if [[ $- == *i* ]]; then
787 # git branch/status prompt function
788 if [[ $OS != Windows_NT ]]; then
789 GIT_PS1_SHOWDIRTYSTATE=true
790 fi
791 # arch source location
792 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
793 # fedora/debian source
794 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
795
796 # in case we didn't source git-prompt.sh
797 if ! declare -f __git_ps1 > /dev/null; then
798 __git_ps1() {
799 :
800 }
801 fi
802
803 # this needs to come before next ps1 stuff
804 if [[ $BASH_VERSION == [456789]* ]]; then
805 shopt -s autocd
806 shopt -s globstar
807 shopt -s dirspell
808 PS1='\w'
809 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
810 PROMPT_DIRTRIM=2
811 bind -m vi-command B:shell-backward-word
812 bind -m vi-command W:shell-forward-word
813 fi
814 else
815 PS1='\W'
816 fi
817
818 if [[ $SSH_CLIENT ]]; then
819 PS1="\h $PS1"
820 fi
821
822 prompt_command() {
823 local return=$? # this MUST COME FIRST
824 local psc pst
825 local ps_char ps_color
826 unset IFS
827 history -a # save history
828 history -n # read any new history
829 if [[ ! DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
830 # from the screen man page
831 if [[ $TERM == screen* ]]; then
832 local title_escape="\033]..2;"
833 else
834 local title_escape="\033]0;"
835 fi
836 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
837 fi
838
839 case $return in
840 0) ps_color="$(get_term_color blue)"
841 ps_char='\$'
842 ;;
843 1) ps_color="$(get_term_color green)"
844 ps_char=$return
845 ;;
846 *) ps_color="$(get_term_color yellow)"
847 ps_char=$return
848 ;;
849 esac
850 if [[ ! -O . ]]; then # not owner
851 if [[ -w . ]]; then # writable
852 ps_color="$(get_term_color bold red)"
853 else
854 ps_color="$(get_term_color bold green)"
855 fi
856 fi
857 PS1="${PS1/%!(*[wW]*)}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
858 }
859 PROMPT_COMMAND=prompt_command
860 fi
861
862
863 ###########################################
864 # stuff that makes sense to be at the end #
865 ###########################################
866 if [[ "$SUDOD" ]]; then
867 cd "$SUDOD"
868 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
869 cd /a
870 fi
871
872
873 # best practice
874 unset IFS
875
876
877 # if someone exported $SOE, catch errors
878 if [[ $SOE ]]; then
879 errcatch
880 fi