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