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