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