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