history more reliable. change debconf solution
[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 if [[ $SSH_CONNECTION ]] \
14 && [[ $- == *c* ]] \
15 && [[ ! $SSH_TTY ]] \
16 && [[ $- != *i* ]]; then
17 return
18 fi
19
20 # Side note on ssh. Command lines and env variables sent across ssh are strictly limited.
21 # If we did want to easily pass info, we could override an obscure unused LC_var
22 # Or we could set SendEnv and AcceptEnv ssh vars, or we could transfer a file.
23
24
25
26 ###################
27 ## include files ###
28 ###################
29
30 for x in $HOME/bin/bash-programs-by-ian/repos/*/*-function; do
31 source "$x"
32 done
33
34 source $HOME/bin/semi-private # so I can share my bashrc
35 source $HOME/path_add-function
36
37
38
39
40 ############
41 # settings #
42 ############
43
44 CDPATH=.:/a
45
46 # remove gnome keyring warning messages
47 # there is probably a more proper way, but I didn't find any easily on google
48 unset GNOME_KEYRING_CONTROL
49
50 path_add /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
51
52 #use extra globing features. See man bash, search extglob.
53 shopt -s extglob
54 #include .files when globbing.
55 shopt -s dotglob
56
57 # disabled because it is broken with bash_completion package. It is a known bug they hope to fix.
58 # When a glob expands to nothing, make it an empty string instead of the literal characters.
59 # shopt -s nullglob
60
61 # make tab on an empty line do nothing
62 shopt -s no_empty_cmd_completion
63
64
65 # advanced completion
66 # http://bash-completion.alioth.debian.org/
67 # i was using the git version for a while for a bug fix.
68 # it's made it into distros now
69 # usually this is sourced by the system already,
70 # but I check just incase
71 if ! type _init_completion &> /dev/null && [[ -r "/usr/share/bash-completion/bash_completion" ]]; then
72 . /usr/share/bash-completion/bash_completion
73 fi
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
84 # inside emacs fixes
85 if [[ $INSIDE_EMACS ]]; then
86 export INSIDE_EMACS
87 export PAGER=cat
88 export MANPAGER=cat
89 # for readline-complete.el
90 stty echo
91 else
92 # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
93 # gnome-terminal, overridden by bash, or disabled by the system
94 stty werase undef lnext undef stop undef start undef
95 fi
96
97
98 if [[ $- == *i* ]]; then
99 # for readline-complete.el
100 if [[ $INSIDE_EMACS ]]; then
101 bind 'set horizontal-scroll-mode on'
102 bind 'set print-completions-horizontally on'
103 bind '"\C-i": self-insert'
104 else
105 # arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
106 if [[ $TERM == "xterm" ]]; then
107 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
108 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
109 else
110 bind '"\eOc": shell-forward-word'
111 bind '"\eOd": shell-backward-word'
112 fi
113 fi
114
115 fi
116
117
118 # history number. History expansion is good.
119 PS4='$LINENO+ '
120 # history file size limit, set to unlimited.
121 # this needs to be different from the default because
122 # default HISTFILESIZE is 500 and could clobber our history
123 HISTFILESIZE=
124 # max commands 1 session can append/read from history
125 HISTSIZE=100000
126 # my own history size limit based on lines
127 HISTFILELINES=1000000
128 HISTFILE=$HOME/.bh
129 # the time format display when doing the history command
130 # also, setting this makes the history file record time
131 # of each command as seconds from the epoch
132 HISTTIMEFORMAT="%I:%M %p %m/%d "
133 # consecutive duplicate lines don't go in history
134 HISTCONTROL=ignoredups
135 # just defensively unsetting this
136 # this var can replace HISTCONTROL to do more flexible things like
137 # not saving single char history items or specific names
138 unset HISTIGNORE
139
140 export BC_LINE_LENGTH=0
141
142 path_add /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
143 path_add $HOME/bin/bash-programs-by-ian/utils
144 # note, if I use a machine I don't want files readable by all users, set
145 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
146
147 C_DEFAULT_DIR=/a
148
149
150
151
152 ###############
153 ### aliases ###
154 ###############
155
156 if [[ $- == *i* ]]; then
157 alias cp='cp -i'
158 alias mv='mv -i'
159 fi
160
161 # remove any default aliases for these
162 alias ls > /dev/null 2>&1 && unalias ls
163 alias ll > /dev/null 2>&1 && unalias ll
164 alias grep > /dev/null 2>&1 && unalias grep
165
166
167 mkdir() {
168 command mkdir -p "$@"
169 }
170
171
172 alias d='builtin bg'
173 complete -A stopped -P '"%' -S '"' d
174
175 alias his='history'
176
177
178 # note: gksudo is recommended for X apps because it does not set the
179 # home directory to the same.
180
181 if [[ $- == *i* ]]; then
182 # extra space at the end allows aliases to work
183 alias s='SUDOD="$PWD" sudo -i '
184 else
185 s() {
186 if [[ $EUID != 0 || $1 == -* ]]; then
187 local SUDOD="$PWD"
188 sudo -i "$@"
189 else
190 "$@"
191 fi
192 }
193 fi
194
195
196
197 if [[ $OS == Windows_NT ]]; then
198 alias ffs='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
199 export DISPLAY=nt
200 alias j='command cygpath'
201 alias t='command cygstart'
202 alias cygstart='echo be quick, use the alias "t" instead :\)'
203 alias cygpath='echo be quick, use the alias "j" instead :\)'
204
205 fi
206
207
208 #####################
209 ### functions ####
210 #####################
211
212
213
214 a() {
215 beet "${@}"
216 }
217
218
219 t() {
220 trash-put "$@"
221 }
222
223
224 if type ack-grep >/dev/null 2>&1; then
225 alias ack=ack-grep
226 fi
227
228
229 gr() {
230 grep -i --binary-files=without-match --color=auto "$@"
231 }
232
233 grr() {
234 grep -ri --binary-files=without-match --color=auto "$@"
235 }
236
237
238
239
240 calc() { echo "scale=3; $*" | bc -l; }
241
242
243 # makes it so chown -R symlink affects the symlink and its target.
244 chown() {
245 if [[ $1 == -R ]]; then
246 shift
247 command chown -h "$@"
248 command chown "$@"
249 command chown -RH "$@"
250 else
251 command chown "$@"
252 fi
253 }
254
255
256
257 cgpl ()
258 {
259 if [[ $# == 0 ]]; then
260 cp /a/bin/data/COPYING .
261 else
262 cp /a/bin/data/COPYING "$@"
263 fi
264 }
265
266
267 dc() {
268 diff --strip-trailing-cr -w "$@" # diff content
269 }
270
271
272 distro_name() {
273 if [[ -f /etc/fedora-release ]]; then
274 echo fedora
275 else
276 grep "^ID=.*" /etc/os-release | sed 's/^ID=//'
277 fi
278 }
279
280
281 dt() {
282 date "+%A, %B %d, %r" "$@"
283 }
284
285
286 e() { echo "$@"; }
287
288
289 envload() { # load environment from a previous: export > file
290 local file=${1:-$HOME/.${USER}_env}
291 eval "$(export | sed 's/^declare -x/export -n/')"
292 while IFS= read -r line; do
293 # declare -x makes variables local to a function
294 eval ${line/#declare -x/export}
295 done < "$file"
296 }
297
298
299
300 # havn't tested these:
301 #file cut copy and paste, like the text buffers :)
302 _fbufferinit() { # internal use by
303 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
304 rm -rf "$my_f_tempdir"/*
305 }
306 fcp() { # file cp
307 _fbufferinit
308 cp "$@" "$my_f_tempdir"/
309 }
310 fct() { # file cut
311 _fbufferinit
312 mv "$@" "$my_f_tempdir"/
313 }
314 fpst() { # file paste
315 [[ $2 ]] && { echo too many arguments; return 1; }
316 target=${1:-.}
317 cp "$my_f_tempdir"/* "$target"
318 }
319
320
321 # find array. make an array of file names found by find into $x
322 # argument: find arguments
323 # return: find results in an array $x
324 fa() {
325 while read -rd ''; do
326 x+=("$REPLY");
327 done < <(find "$@" -print0);
328 }
329
330
331 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
332 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
333 local gitroot
334 gitroot || return 1 # function to set gitroot
335 builtin cd $gitroot
336 git symbolic-ref HEAD refs/heads/$1
337 rm .git/index
338 git clean -fdx
339 }
340
341 fw() {
342 firefox -P default "$@" >/dev/null 2>&1
343 }
344
345 fn() {
346 firefox -P alt "$@" >/dev/null 2>&1
347 }
348
349
350
351
352
353 # horizontal row. used to break up output
354 hr() { printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $(seq $COLUMNS); }
355
356
357 i() {
358 git "$@"
359 }
360 # modified from ~/local/bin/git-completion.bash
361 # other completion commands are mostly taken from bash_completion package
362 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
363 || complete -o default -o nospace -F _git i
364
365
366 # insensitive find
367 ifn () {
368 find . -iname '*'"$*"'*'
369 }
370
371
372
373 l() {
374 if [[ $PWD == /[iap] ]]; then
375 command ls -A --color=auto -I lost+found "$@"
376 else
377 command ls -A --color=auto "$@"
378 fi
379 }
380
381
382 lld() { ll -d "$@"; }
383
384
385 low() { # make filenames all lowercase
386 local x y
387 for x in "$@"; do
388 y=$(tr "[A-Z]" "[a-z]" <<<"$x")
389 [[ $y != $x ]] && mv "$x" "$y"
390 done
391 }
392
393
394 lower() { # make first letter of filenames lowercase.
395 local x
396 for x in "$@"; do
397 if [[ ${x::1} == [A-Z] ]]; then
398 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
399 safe_rename "$x" "$y"
400 fi
401 done
402 }
403
404 safe_rename() {
405 if [[ $# != 2 ]]; then
406 echo safe_rename error: $# args, need 2 >2
407 return 1
408 elif [[ $1 != $2 ]]; then
409 if [[ -e $2 ]]; then
410 echo Cannot rename "$1" to "$2" as it already exists.
411 else
412 mv "$1" "$2"
413 fi
414 fi
415 }
416
417 despace() {
418 local x y
419 for x in "$@"; do
420 y="${x// /_}"
421 safe_rename "$x" "$y"
422 done
423 }
424
425 # force symbolic link creation.
426 # trash-put any existing files where links would be created.
427 # mkdir -p the directory containing the link(s) if needed.
428 # then do ln -s -- "$@"
429 lnf() {
430 if [[ $# -gt 2 && ! -d ${!#} ]]; then
431 mkdir -p "${!#}"
432 fi
433 if [[ $# -gt 1 && -d ${!#} ]]; then
434 local oldcwd=$PWD
435 cd ${!#} # last arg
436 for x in "${@:1:$(($#-1))}"; do # all but last arg
437 # remove any trailing slashes
438 x="${x%%+(/)}"
439 # remove any leading directory components
440 x="${x##*/}"
441 te "$x" && trash-put "$x"
442 done
443 cd "$oldcwd"
444 elif [[ $# -eq 2 ]]; then
445 if te "$2"; then
446 trash-put "$2"
447 elif [[ ! -d $(getdir "$2") ]]; then
448 mkdir -p $(getdir "$2")
449 fi
450 else
451 te "${1##*/}" && rm "${1##*/}"
452 fi
453 ln -s -- "$@"
454 }
455
456
457
458 # package manager
459 # aliases would be much more compact, but they can't be used as ssh commands
460 # also, to be used in a script, you need -i which prints annoying
461 # warnings. instead, use -l in a script to source this file
462 if type -p yum > /dev/null; then
463 p() {
464 if [[ $EUID == 0 ]]; then
465 yum "$@"
466 else
467 sudo yum "$@"
468 fi
469 }
470 pi() {
471 if [[ $EUID == 0 ]]; then
472 yum -y install "$@"
473 else
474 sudo yum -y install "$@"
475 fi
476 }
477 pf() {
478 if [[ $EUID == 0 ]]; then
479 yum search "$@"
480 else
481 sudo yum search "$@"
482 fi
483 }
484 else
485 p() {
486 if [[ $EUID == 0 ]]; then
487 aptitude "$@"
488 else
489 sudo aptitude "$@"
490 fi
491 }
492 pi() {
493 if [[ $EUID == 0 ]]; then
494 aptitude -y install "$@"
495 else
496 sudo aptitude -y install "$@"
497 fi
498 }
499 pf() {
500 if [[ $EUID == 0 ]]; then
501 aptitude search "$@"
502 else
503 sudo aptitude search "$@"
504 fi
505 }
506 fi
507
508
509 # test existence / exists
510 te() {
511 local ret=0
512 for x in "$@"; do
513 [[ -e "$x" || -L "$x" ]] || ret=1
514 done
515 return $ret
516 }
517
518
519 # fix root file ownership for FILE argument.
520 # check if parent or grandparent is not root and if the dir of FILE is also
521 # owned by that user, and change ownership to that user
522 perm_fix() {
523 local parent
524 if [[ $EUID == 0 ]]; then
525 te "$1" || touch $1
526 if [[ $(stat -c "%u" "$1") == 0 ]] ; then
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 local harchive="${HISTFILE}_archive"
722 for x in "$HISTFILE" "$harchive"; do
723 if [[ ! $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
724 echo "error in hl: history file $x no good."
725 return 1
726 fi
727 done
728 history -w
729 max_lines=$HISTFILELINES
730 [[ $max_lines ]] || { echo "error in hl: failed to get max line count"; return 1; }
731 linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
732 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
733 if (($linecount > $max_lines)); then
734 prune_lines=$(($linecount - $max_lines))
735 head -n $prune_lines "$HISTFILE" >> "$harchive" \
736 && sed -ie "1,${prune_lines}d" $HISTFILE
737 fi
738 }
739
740 # commands to run when bash exits normally
741 trap "hl; smh" EXIT
742
743
744 # temporary variables to test colorization
745 # some copied from gentoo /etc/bash/bashrc,
746 use_color=false
747 # dircolors --print-database uses its own built-in database
748 # instead of using /etc/DIR_COLORS. Try to use the external file
749 # first to take advantage of user additions.
750 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
751 match_lhs=""
752 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
753 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
754 [[ -z ${match_lhs} ]] \
755 && type -P dircolors >/dev/null \
756 && match_lhs=$(dircolors --print-database)
757 # test if our $TERM is in the TERM values in dircolor
758 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
759
760
761 if ${use_color} && [[ $- == *i* ]]; then
762
763 if [[ $XTERM_VERSION == Cygwin* ]]; then
764 get_term_color() {
765 for x in "$@"; do
766 case $x in
767 underl) echo -n $'\E[4m' ;;
768 bold) echo -n $'\E[1m' ;;
769 red) echo -n $'\E[31m' ;;
770 green) echo -n $'\E[32m' ;;
771 blue) echo -n $'\E[34m' ;;
772 cyan) echo -n $'\E[36m' ;;
773 yellow) echo -n $'\E[33m' ;;
774 purple) echo -n $'\E[35m' ;;
775 nocolor) echo -n $'\E(B\E[m' ;;
776 esac
777 done
778 }
779
780 else
781 get_term_color() {
782 for x in "$@"; do
783 case $x in
784 underl) echo -n $(tput smul) ;;
785 bold) echo -n $(tput bold) ;;
786 red) echo -n $(tput setaf 1) ;;
787 green) echo -n $(tput setaf 2) ;;
788 blue) echo -n $(tput setaf 4) ;;
789 cyan) echo -n $(tput setaf 6) ;;
790 yellow) echo -n $(tput setaf 3) ;;
791 purple) echo -n $(tput setaf 5) ;;
792 nocolor) echo -n $(tput sgr0) ;; # no font attributes
793 esac
794 done
795 }
796 fi
797 else
798 get_term_color() {
799 :
800 }
801 fi
802 # Try to keep environment pollution down, EPA loves us.
803 unset safe_term match_lhs use_color
804
805
806
807
808
809
810 ###############
811 # prompt ######
812 ###############
813
814
815 if [[ $- == *i* ]]; then
816 # git branch/status prompt function
817 if [[ $OS != Windows_NT ]]; then
818 GIT_PS1_SHOWDIRTYSTATE=true
819 fi
820 # arch source location
821 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
822 # fedora/debian source
823 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
824
825 # in case we didn't source git-prompt.sh
826 if ! declare -f __git_ps1 > /dev/null; then
827 __git_ps1() {
828 :
829 }
830 fi
831
832 # this needs to come before next ps1 stuff
833 if [[ $BASH_VERSION == [456789]* ]]; then
834 shopt -s autocd
835 shopt -s globstar
836 shopt -s dirspell
837 PS1='\w'
838 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
839 PROMPT_DIRTRIM=2
840 bind -m vi-command B:shell-backward-word
841 bind -m vi-command W:shell-forward-word
842 fi
843 else
844 PS1='\W'
845 fi
846
847 if [[ $SSH_CLIENT ]]; then
848 PS1="\h $PS1"
849 fi
850
851 prompt_command() {
852 local return=$? # this MUST COME FIRST
853 local psc pst
854 local ps_char ps_color
855 unset IFS
856 history -a # save history
857 history -n # read any new history
858 if [[ ! DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
859 # from the screen man page
860 if [[ $TERM == screen* ]]; then
861 local title_escape="\033]..2;"
862 else
863 local title_escape="\033]0;"
864 fi
865 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
866 fi
867
868 case $return in
869 0) ps_color="$(get_term_color blue)"
870 ps_char='\$'
871 ;;
872 1) ps_color="$(get_term_color green)"
873 ps_char=$return
874 ;;
875 *) ps_color="$(get_term_color yellow)"
876 ps_char=$return
877 ;;
878 esac
879 if [[ ! -O . ]]; then # not owner
880 if [[ -w . ]]; then # writable
881 ps_color="$(get_term_color bold red)"
882 else
883 ps_color="$(get_term_color bold green)"
884 fi
885 fi
886 PS1="${PS1/%!(*[wW]*)}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
887 }
888 PROMPT_COMMAND=prompt_command
889 fi
890
891
892 ###########################################
893 # stuff that makes sense to be at the end #
894 ###########################################
895 if [[ "$SUDOD" ]]; then
896 cd "$SUDOD"
897 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
898 cd /a
899 fi
900
901
902 # best practice
903 unset IFS
904
905
906 # if someone exported $SOE, catch errors
907 if [[ $SOE ]]; then
908 errcatch
909 fi