better aptitude search
[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 # inside emacs fixes
92 if [[ $INSIDE_EMACS ]]; then
93 export INSIDE_EMACS
94 export PAGER=cat
95 export MANPAGER=cat
96 # for readline-complete.el
97 stty echo
98 fi
99
100
101 if [[ $- == *i* ]]; then
102 # for readline-complete.el
103 if [[ $INSIDE_EMACS ]]; then
104 bind 'set horizontal-scroll-mode on'
105 bind 'set print-completions-horizontally on'
106 bind '"\C-i": self-insert'
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 his='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 "$@"
580 }
581
582 # rsync, root is required to keep permissions right.
583 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
584 # --no-times --delete
585 # basically, make an exact copy, use checksums instead of file times to be more accurate
586 rl() { rsync -ahvic --delete "$@"; }
587 # don't delete files on the target end which do not exist on the original end:
588 rld() { rsync -ahvic "$@"; }
589 complete -F _rsync -o nospace rld rlt fl
590 # rl without preserving modification time. for some reason I had this as default before.
591 # perhaps that reason will come up again and I will document it.
592 rlt() { rsync -ahvic --delete --no-t "$@"; }
593
594
595
596 # use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
597 sb() {
598 local SUDOD="$PWD"
599 sudo -i bash -c "$@"
600 }
601 complete -F _root_command s sb
602
603 # use -ll, less secure but faster.
604 srm () {
605 srm -ll "$@"
606 }
607
608 # sudo redo. be aware, this command may not work right on strange distros or earlier software
609 sr() {
610 if [[ $# == 0 ]]; then
611 sudo -E bash -c -l "$(history -p '!!')"
612 else
613 echo this command redos last history item. no argument is accepted
614 fi
615 }
616
617
618
619 # log with script. timing is $1.t and script is $1.s
620 # -l to save to ~/typescripts/
621 # -t to add a timestamp to the filenames
622 slog() {
623 local logdir do_stamp arg_base
624 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
625 logdir="/a/dt/"
626 do_stamp=false
627 while getopts "lt" option
628 do
629 case $option in
630 l ) arg_base=$logdir ;;
631 t ) do_stamp=true ;;
632 esac
633 done
634 shift $(($OPTIND - 1))
635 arg_base+=$1
636 [[ -e $logdir ]] || mkdir -p $logdir
637 $do_stamp && arg_base+=$(date +%F.%T%z)
638 script -t $arg_base.s 2> $arg_base.t
639 }
640 splay() { # script replay
641 #logRoot="$HOME/typescripts/"
642 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
643 scriptreplay "$1.t" "$1.s"
644 }
645
646
647
648 # timer in minutes
649 tm() {
650 (sleep $(calc "$@ * 60") && mpv /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
651 }
652
653
654 ts() { # start editing a new file
655 [[ $# != 1 ]] && echo "I need a filename." && return 1
656 local quiet
657 if [[ $- != *i* ]]; then
658 quiet=true
659 fi
660 if [[ $1 == *.c ]]; then
661 e '#include <stdio.h>' >"$1"
662 e '#include <stdlib.h>' >>"$1"
663 e 'int main(int argc, char * argv[]) {' >>"$1"
664 e ' printf( "hello world\n");' >>"$1"
665 e ' return 0;' >>"$1"
666 e '}' >>"$1"
667 e "${1%.c}: $1" > Makefile
668 e " g++ -ggdb -std=gnu99 -o ${1%.c} $<" >> Makefile
669 e "#!/bin/bash" >run.sh
670 e "./${1%.c}" >>run.sh
671 chmod +x run.sh
672 elif [[ $1 == *.java ]]; then
673 e "public class ${1%.*} {" >"$1"
674 e ' public static void main(String[] args) {' >>"$1"
675 e ' System.out.println("Hello, world!");' >>"$1"
676 e ' }' >>"$1"
677 e '}' >>"$1"
678
679 else
680 echo "#!/bin/bash" > "$1"
681 chmod +x "$1"
682 fi
683 [[ $quiet ]] || g "$1"
684
685 }
686
687 tx() { # toggle set -x, and the prompt so it doesn't spam
688 if [[ $- == *x* ]]; then
689 set +x
690 PROMPT_COMMAND=prompt_command
691 else
692 unset PROMPT_COMMAND
693 PS1="\w \$ "
694 set -x
695 fi
696 }
697
698
699
700
701 if [[ $OS == Windows_NT ]]; then
702 # cygstart wrapper
703 cs() {
704 cygstart "$@" &
705 }
706 xp() {
707 explorer.exe .
708 }
709 # launch
710 o() {
711 local x=(*$1*)
712 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
713 cygstart *$1* &
714 }
715 else
716 o() {
717 if type gvfs-open &> /dev/null ; then
718 gvfs-open "$@"
719 else
720 xdg-open "$@"
721 fi
722 # another alternative is run-mailcap
723 }
724 fi
725
726
727 # todo, update this
728 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
729
730
731
732 hl() { # history limit. Write extra history to archive file.
733 local max_lines linecount tempfile prune_lines
734 local harchive="${HISTFILE}_archive"
735 for x in "$HISTFILE" "$harchive"; do
736 if [[ ! $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
737 echo "error in hl: history file $x no good"
738 return 1
739 fi
740 done
741 history -a # save history
742 max_lines=$HISTFILELINES
743 [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
744 linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
745 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
746 if (($linecount > $max_lines)); then
747 prune_lines=$(($linecount - $max_lines))
748 head -n $prune_lines "$HISTFILE" >> "$harchive" \
749 && sed -ie "1,${prune_lines}d" $HISTFILE
750 fi
751 }
752
753 # commands to run when bash exits normally
754 trap "hl; smh" EXIT
755
756
757 # temporary variables to test colorization
758 # some copied from gentoo /etc/bash/bashrc,
759 use_color=false
760 # dircolors --print-database uses its own built-in database
761 # instead of using /etc/DIR_COLORS. Try to use the external file
762 # first to take advantage of user additions.
763 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
764 match_lhs=""
765 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
766 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
767 [[ -z ${match_lhs} ]] \
768 && type -P dircolors >/dev/null \
769 && match_lhs=$(dircolors --print-database)
770 # test if our $TERM is in the TERM values in dircolor
771 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
772
773
774 if ${use_color} && [[ $- == *i* ]]; then
775
776 if [[ $XTERM_VERSION == Cygwin* ]]; then
777 get_term_color() {
778 for x in "$@"; do
779 case $x in
780 underl) echo -n $'\E[4m' ;;
781 bold) echo -n $'\E[1m' ;;
782 red) echo -n $'\E[31m' ;;
783 green) echo -n $'\E[32m' ;;
784 blue) echo -n $'\E[34m' ;;
785 cyan) echo -n $'\E[36m' ;;
786 yellow) echo -n $'\E[33m' ;;
787 purple) echo -n $'\E[35m' ;;
788 nocolor) echo -n $'\E(B\E[m' ;;
789 esac
790 done
791 }
792
793 else
794 get_term_color() {
795 for x in "$@"; do
796 case $x in
797 underl) echo -n $(tput smul) ;;
798 bold) echo -n $(tput bold) ;;
799 red) echo -n $(tput setaf 1) ;;
800 green) echo -n $(tput setaf 2) ;;
801 blue) echo -n $(tput setaf 4) ;;
802 cyan) echo -n $(tput setaf 6) ;;
803 yellow) echo -n $(tput setaf 3) ;;
804 purple) echo -n $(tput setaf 5) ;;
805 nocolor) echo -n $(tput sgr0) ;; # no font attributes
806 esac
807 done
808 }
809 fi
810 else
811 get_term_color() {
812 :
813 }
814 fi
815 # Try to keep environment pollution down, EPA loves us.
816 unset safe_term match_lhs use_color
817
818
819
820
821
822
823 ###############
824 # prompt ######
825 ###############
826
827
828 if [[ $- == *i* ]]; then
829 # git branch/status prompt function
830 if [[ $OS != Windows_NT ]]; then
831 GIT_PS1_SHOWDIRTYSTATE=true
832 fi
833 # arch source location
834 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
835 # fedora/debian source
836 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
837
838 # in case we didn't source git-prompt.sh
839 if ! declare -f __git_ps1 > /dev/null; then
840 __git_ps1() {
841 :
842 }
843 fi
844
845 # this needs to come before next ps1 stuff
846 if [[ $BASH_VERSION == [456789]* ]]; then
847 shopt -s autocd
848 shopt -s globstar
849 shopt -s dirspell
850 PS1='\w'
851 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
852 PROMPT_DIRTRIM=2
853 bind -m vi-command B:shell-backward-word
854 bind -m vi-command W:shell-forward-word
855 fi
856 else
857 PS1='\W'
858 fi
859
860 if [[ $SSH_CLIENT ]]; then
861 PS1="\h $PS1"
862 fi
863
864 prompt_command() {
865 local return=$? # this MUST COME FIRST
866 local psc pst
867 local ps_char ps_color
868 unset IFS
869 history -a # save history
870 history -n # read any new history
871 if [[ ! DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
872 # from the screen man page
873 if [[ $TERM == screen* ]]; then
874 local title_escape="\033]..2;"
875 else
876 local title_escape="\033]0;"
877 fi
878 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
879 fi
880
881 case $return in
882 0) ps_color="$(get_term_color blue)"
883 ps_char='\$'
884 ;;
885 1) ps_color="$(get_term_color green)"
886 ps_char=$return
887 ;;
888 *) ps_color="$(get_term_color yellow)"
889 ps_char=$return
890 ;;
891 esac
892 if [[ ! -O . ]]; then # not owner
893 if [[ -w . ]]; then # writable
894 ps_color="$(get_term_color bold red)"
895 else
896 ps_color="$(get_term_color bold green)"
897 fi
898 fi
899 PS1="${PS1/%!(*[wW]*)}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
900 }
901 PROMPT_COMMAND=prompt_command
902 fi
903
904
905 ###########################################
906 # stuff that makes sense to be at the end #
907 ###########################################
908 if [[ "$SUDOD" ]]; then
909 cd "$SUDOD"
910 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
911 cd /a
912 fi
913
914
915 # best practice
916 unset IFS
917
918
919 # if someone exported $SOE, catch errors
920 if [[ $SOE ]]; then
921 errcatch
922 fi