lots of updates, and sort my bash functions
[distro-setup] / .bashrc
1 # to debug
2 #set -x
3 # redirect output to log file. this doesn't work. todo figure out why
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, but we can
10 # override that by having #!/bin/bash -l. I want something similar for ssh
11 # commands. when a local script runs an ssh command, this file should not be
12 # sourced by default, but we should be able to override that.
13 #
14 # so here we test for conditions of a script under ssh and return if so. To test
15 # for an overriding condition, we have a few options. one is to use an
16 # environment variable. env variables sent across ssh are strictly limited. ssh
17 # -t which sets $SSH_TTY, but within a script that won't work because tty
18 # allocation will fail. We could override an obscure unused LC_var, like
19 # telephone, but I don't want to run into some edge case where that messes
20 # things up. we could transfer a file which we could test for, but I can't think
21 # of a way to make that inherently limited to a single ssh command. I choose to
22 # set SendEnv and AcceptEnv ssh config vars to allow the environment variable
23 # BASH_LOGIN_SHELL to propagate across ssh.
24
25 # assume we want ssh commands to source this file if we are sourcing it,
26 # and we haven't specified otherwise already
27 [[ ! $BASH_LOGIN_SHELL ]] && export BASH_LOGIN_SHELL=true
28
29 # first conditions show that we are an ssh command without an interactive shell
30 if [[ $SSH_CONNECTION ]] \
31 && [[ $- == *c* ]] \
32 && [[ ! $SSH_TTY ]] \
33 && [[ $- != *i* ]] \
34 && [[ ! $BASH_LOGIN_SHELL == true ]]; then
35 return
36 else
37 source /etc/profile
38 fi
39
40
41
42 # note, to catch errors in functions but not outside, do:
43 # set -E -o pipefail
44 # trap return ERR
45 # trap 'trap ERR' RETURN
46
47
48
49 ############
50 # settings #
51 ############
52
53 CDPATH=.
54
55 set -o pipefail
56
57 # remove all aliases. aliases provided by the system tend to get in the way,
58 # for example, error happens if I try to define a function the same name as an alias
59 unalias -a
60
61 # remove gnome keyring warning messages
62 # there is probably a more proper way, but I didn't find any easily on google
63 # now using xfce+xmonad instead of vanilla xmonad, so disabling this
64 #unset GNOME_KEYRING_CONTROL
65
66 # use extra globing features.
67 shopt -s extglob
68 # include .files when globbing, but ignore files name . and ..
69 # setting this also sets dotglob
70 export GLOBIGNORE=*/.:*/..
71
72 # broken with bash_completion package. Saw a bug for this once. Don't anymore.
73 # still broken in wheezy
74 # still buggered in latest stable from the web, version 2.1
75 # perhaps its fixed in newer git version, which fails to make for me
76 # this note is from 6-2014.
77 # Also, enabling this before sourcing .bashrc makes PATH be empty.
78 #shopt -s nullglob
79
80 # make tab on an empty line do nothing
81 shopt -s no_empty_cmd_completion
82
83 # advanced completion
84 # http://bash-completion.alioth.debian.org/
85 # might be sourced by the system already, but I've noticed it not being sourced before
86 if ! type _init_completion &> /dev/null && [[ -r "/usr/share/bash-completion/bash_completion" ]]; then
87 . /usr/share/bash-completion/bash_completion
88 fi
89
90
91 # fix spelling errors for cd, only in interactive shell
92 shopt -s cdspell
93 # append history instead of overwritting it
94 shopt -s histappend
95 # for compatibility, per gentoo/debian bashrc
96 shopt -s checkwinsize
97 # attempt to save multiline single commands as single history entries.
98 shopt -s cmdhist
99 shopt -s globstar
100
101
102 # inside emacs fixes
103 if [[ $INSIDE_EMACS ]]; then
104 # EMACS is used by bash on startup, but we don't need it anymore.
105 # plus I hit a bug in a makefile which inherited it
106 unset EMACS
107 export INSIDE_EMACS
108 export PAGER=cat
109 export MANPAGER=cat
110 # scp completion does not work, but this doesn't fix it. todo, figure this out
111 complete -r scp &> /dev/null
112 # todo, remote file completion fails, figure out how to turn it off
113 fi
114
115
116 if [[ $- == *i* ]]; then
117 # for readline-complete.el
118 if [[ $INSIDE_EMACS ]]; then
119 # all for readline-complete.el
120 stty echo
121 bind 'set horizontal-scroll-mode on'
122 bind 'set print-completions-horizontally on'
123 bind '"\C-i": self-insert'
124 else
125 # arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
126 if [[ $TERM == "xterm" ]]; then
127 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
128 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
129 else
130 bind '"\eOc": shell-forward-word'
131 bind '"\eOd": shell-backward-word'
132 fi
133 # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
134 # gnome-terminal, overridden by bash, or disabled by the system
135 stty werase undef lnext undef stop undef start undef
136
137 fi
138
139 fi
140
141
142 # history number. History expansion is good.
143 PS4='$LINENO+ '
144 # history file size limit, set to unlimited.
145 # this needs to be different from the default because
146 # default HISTFILESIZE is 500 and could clobber our history
147 HISTFILESIZE=
148 # max commands 1 session can append/read from history
149 HISTSIZE=100000
150 # my own history size limit based on lines
151 HISTFILELINES=1000000
152 HISTFILE=$HOME/.bh
153 # the time format display when doing the history command
154 # also, setting this makes the history file record time
155 # of each command as seconds from the epoch
156 HISTTIMEFORMAT="%I:%M %p %m/%d "
157 # consecutive duplicate lines don't go in history
158 HISTCONTROL=ignoredups
159 # works in addition to HISTCONTROL to do more flexible things
160 # it could also do the same things as HISTCONTROL and thus replace it,
161 # but meh
162 HISTIGNORE='k *; *'
163
164 export BC_LINE_LENGTH=0
165
166
167 # note, if I use a machine I don't want files readable by all users, set
168 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
169
170 C_DEFAULT_DIR=/a
171
172
173 ###################
174 ## include files ###
175 ###################
176
177 for _x in /a/bin/distro-functions/src/* /a/bin/*/*-function; do
178 source "$_x"
179 done
180 unset _x
181 # so I can share my bashrc
182 for x in /a/bin/bash_unpublished/*; do source $x; done
183 source $(dirname $(readlink -f $BASH_SOURCE))/path_add-function
184 source /a/bin/log-quiet/logq-function
185 path_add /a/exe
186 path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
187 # todo, these need to be renamed to be less generic.
188 # sync overrode something else useful
189 #path_add $HOME/bin/bash-programs-by-ian/utils
190
191
192 ###############
193 ### aliases ###
194 ###############
195
196 # very few aliases, functions are always preferred.
197
198 # ancient stuff.
199 if [[ $OS == Windows_NT ]]; then
200 alias ffs='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
201 export DISPLAY=nt
202 alias j='command cygpath'
203 alias t='command cygstart'
204 alias cygstart='echo be quick, use the alias "t" instead :\)'
205 alias cygpath='echo be quick, use the alias "j" instead :\)'
206 fi
207
208
209
210 # keep this in mind? good for safety.
211 # alias cp='cp -i'
212 # alias mv='mv -i'
213
214
215 # remove any default aliases for these
216 unalias ls ll grep &>/dev/null ||:
217
218
219
220
221
222
223
224
225
226
227 #####################
228 ### functions ####
229 #####################
230
231
232 mkdir() { command mkdir -p "$@"; }
233
234
235 # fast commit all
236 ic() {
237 git commit -am "$*"
238 }
239
240
241 # file cut copy and paste, like the text buffers :)
242 # I havn't tested these.
243 _fbufferinit() { # internal use by
244 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
245 rm -rf "$my_f_tempdir"/*
246 }
247 fcp() { # file cp
248 _fbufferinit
249 cp "$@" "$my_f_tempdir"/
250 }
251 fct() { # file cut
252 _fbufferinit
253 mv "$@" "$my_f_tempdir"/
254 }
255 fpst() { # file paste
256 [[ $2 ]] && { echo too many arguments; return 1; }
257 target=${1:-.}
258 cp "$my_f_tempdir"/* "$target"
259 }
260
261 # history search
262 k() { grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 40; }
263
264 # horizontal row. used to break up output
265 hr() { printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq $COLUMNS); }
266
267
268 # insensitive find
269 ifn () {
270 find -L . -iname "*$**" 2>/dev/null
271 }
272
273 # test existence / exists
274 te() {
275 local ret=0
276 for x in "$@"; do
277 [[ -e "$x" || -L "$x" ]] || ret=1
278 done
279 return $ret
280 }
281
282 # todo, update this
283 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
284
285 # use sb instead of s is for sudo redirections, eg. sb 'echo "ok fine" > /etc/file'
286 sb() {
287 local SUDOD="$PWD"
288 sudo -i bash -c "$@"
289 }
290 complete -F _root_command s sb
291
292
293 _cdiff-prep() {
294 # join options which are continued to multiples lines onto one line
295 local first=true
296 grep -vE '^([ \t]*#|^[ \t]*$)' "$1" | while IFS= read -r line; do
297 # remove leading spaces/tabs. assumes extglob
298 if [[ $line == "[ ]*" ]]; then
299 line="${line##+( )}"
300 fi
301 if $first; then
302 pastline="$line"
303 first=false
304 elif [[ $line == *=* ]]; then
305 echo "$pastline" >> "$2"
306 pastline="$line"
307 else
308 pastline="$pastline $line"
309 fi
310 done
311 echo "$pastline" >> "$2"
312 }
313
314 _khfix_common() {
315 local h=${1##*@}
316 ssh-keygen -R $h
317 local x=$(timeout 0.1 ssh -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p");
318 ssh-keygen -R $x
319 }
320 khfix() { # known hosts fix
321 _khfix_common "$@"
322 ssh $1 :
323 }
324 khcopy() {
325 _khfix_common "$@"
326 ssh-copy-id $1
327 }
328
329 a() {
330 beet "${@}"
331 }
332
333 ack() { ack-grep "$@"; }
334
335 bashrcpush () {
336 local startdir="$PWD"
337 cd ~
338 for x in "$@"; do
339 ssh $x mkdir -p bin/distro-functions/src
340 tar cz bin/semi-private bin/distro-functions/src | ssh $x tar xz
341 done
342 cd $(mktemp -d)
343 command cp /a/c/repos/bash/!(.git) ~/.gitconfig .
344 for x in "$@"; do
345 tar cz * | ssh $x tar xz
346 done
347 cd "$startdir"
348 }
349
350 caa() { git commit --amend --no-edit -a; }
351
352 calc() { echo "scale=3; $*" | bc -l; }
353 # no having to type quotes, but also no command history:
354 clc() {
355 local x
356 read -r x
357 echo "scale=3; $x" | bc -l
358 }
359
360 cam() {
361 git commit -am "$*"
362 }
363
364 ccat () { # config cat. see a config without extra lines.
365 grep '^\s*[^[:space:]#]' "$@"
366 }
367
368 cdiff() {
369 # diff config files,
370 # setup for format of postfix, eg:
371 # option = stuff[,]
372 # [more stuff]
373 local pastline
374 local unified="$(mktemp)"
375 local f1="$(mktemp)"
376 local f2="$(mktemp)"
377 _cdiff-prep "$1" "$f1"
378 _cdiff-prep "$2" "$f2"
379 cat "$f1" "$f2" | grep -Po '^[^=]+=' | sort | uniq > "$unified"
380 while IFS= read -r line; do
381 # the default bright red / blue doesn't work in emacs shell
382 dwdiff -cblue,red -A best -d " ," <(grep "^$line" "$f1" || echo ) <(grep "^$line" "$f2" || echo ) | colordiff
383 done < "$unified"
384 }
385
386 cgpl ()
387 {
388 if [[ $# == 0 ]]; then
389 cp /a/bin/data/COPYING .
390 else
391 cp /a/bin/data/COPYING "$@"
392 fi
393 }
394
395 chown() {
396 # makes it so chown -R symlink affects the symlink and its target.
397 if [[ $1 == -R ]]; then
398 shift
399 command chown -h "$@"
400 command chown "$@"
401 command chown -RH "$@"
402 else
403 command chown "$@"
404 fi
405 }
406
407 cim() {
408 git commit -m "$*"
409 }
410
411 d() { builtin bg; }
412 complete -A stopped -P '"%' -S '"' d
413
414 dat() { # do all tee, for more complex scripts
415 tee >(ssh frodo bash -l) >(bash -l) >(ssh x2 bash -l) >(ssh tp bash -l)
416 }
417 da() { # do all
418 local host
419 "$@"
420 for host in x2 tp treetowl; do
421 ssh $host "$@"
422 done
423 }
424
425 dc() {
426 diff --strip-trailing-cr -w "$@" # diff content
427 }
428
429 debian_pick_mirror () {
430 # netselect-apt finds a fast mirror.
431 # but we need to replace the mirrors ourselves,
432 # because it doesn't do that. best it can do is
433 # output a basic sources file
434 # here we get the server it found, get the main server we use
435 # then substitute all instances of one for the other in the sources file
436 # and backup original to /etc/apt/sources.list-original.
437 # this is idempotent. the only way to identify debian sources is to
438 # note the original server, so we put it in a comment so we can
439 # identify it later.
440 local file=$(mktemp -d)/f # safe way to get file name without creating one
441 sudo netselect-apt -o "$file" || return 1
442 url=$(grep ^\\w $file | head -n1 | awk '{print $2}')
443 sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
444 sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list
445 sudo apt-get update
446 }
447
448 despace() {
449 local x y
450 for x in "$@"; do
451 y="${x// /_}"
452 safe_rename "$x" "$y"
453 done
454 }
455
456 dt() {
457 date "+%A, %B %d, %r" "$@"
458 }
459
460 dus() {
461 du -sh ${@:-*} | sort -h
462 }
463
464
465
466 e() { echo "$@"; }
467
468
469 ediff() {
470 [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
471 emacs --eval "(ediff-files \"$1\" \"$2\")"
472 }
473
474
475 envload() { # load environment from a previous: export > file
476 local file=${1:-$HOME/.${USER}_env}
477 eval "$(export | sed 's/^declare -x/export -n/')"
478 while IFS= read -r line; do
479 # declare -x makes variables local to a function
480 eval ${line/#declare -x/export}
481 done < "$file"
482 }
483
484
485 fa() {
486 # find array. make an array of file names found by find into $x
487 # argument: find arguments
488 # return: find results in an array $x
489 while read -rd ''; do
490 x+=("$REPLY");
491 done < <(find "$@" -print0);
492 }
493
494
495 ff() {
496 if type -P firefox &>/dev/null; then
497 firefox "$@"
498 else
499 iceweasel "$@"
500 fi
501 }
502
503
504
505 fn() {
506 firefox -P alt "$@" >/dev/null 2>&1
507 }
508
509
510 fsdiff () {
511 local missing=false
512 local dname="${PWD##*/}"
513 local m="/a/tmp/$dname-missing"
514 local d="/a/tmp/$dname-diff"
515 [[ -e $d ]] && rm "$d"
516 [[ -e $m ]] && rm "$m"
517 local msize=0
518 local fsfile
519 while read -r line; do
520 fsfile="$1${line#.}"
521 if [[ -e "$fsfile" ]]; then
522 md5diff "$line" "$fsfile" && tee -a "/a/tmp/$dname-diff" <<< "$fsfile $line"
523 else
524 missing=true
525 echo "$line" >> "$m"
526 msize=$((msize + 1))
527 fi
528 done < <(find -type f )
529 if $missing; then
530 echo "$m"
531 (( msize <= 100 )) && cat $m
532 fi
533 }
534 fsdiff-test() {
535 # expected output, with different tmp dirs
536 # /tmp/tmp.HDPbwMqdC9/c/d ./c/d
537 # /a/tmp/tmp.qLDkYxBYPM-missing
538 # ./b
539 cd $(mktemp -d)
540 echo ok > a
541 echo nok > b
542 mkdir c
543 echo ok > c/d
544 local x=$(mktemp -d)
545 mkdir $x/c
546 echo different > $x/c/d
547 echo ok > $x/a
548 fsdiff $x
549 }
550 rename-test() {
551 # test whether missing files were renamed, generally for use with fsdiff
552 # $1 = fsdiff output file, $2 = directory to compare to. pwd = fsdiff dir
553 # echos non-renamed files
554 local x y found
555 unset sums
556 for x in "$2"/*; do
557 { sums+=( "$(md5sum < "$x")" ) ; } 2>/dev/null
558 done
559 while read -r line; do
560 { missing_sum=$(md5sum < "$line") ; } 2>/dev/null
561 renamed=false
562 for x in "${sums[@]}"; do
563 if [[ $missing_sum == "$x" ]]; then
564 renamed=true
565 break
566 fi
567 done
568 $renamed || echo "$line"
569 done < "$1"
570 return 0
571 }
572
573
574 funce() {
575 # like -e for functions. returns on error.
576 # at the end of the function, disable with:
577 # trap ERR
578 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
579 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
580 trap ERR
581 return' ERR
582 }
583
584
585 fw() {
586 firefox -P default "$@" >/dev/null 2>&1
587 }
588
589 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
590 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
591 local gitroot
592 gitroot || return 1 # function to set gitroot
593 builtin cd "$gitroot"
594 git symbolic-ref HEAD refs/heads/$1
595 rm .git/index
596 git clean -fdx
597 }
598
599 gr() {
600 grep -iIP --color=auto "$@"
601 }
602
603
604
605
606
607 grr() {
608 if [[ ${#@} == 1 ]]; then
609 grep -riIP --color=auto "$@" .
610 else
611 grep -riIP --color=auto "$@"
612 fi
613 }
614
615
616 hl() { # history limit. Write extra history to archive file.
617 # todo: this is not working or not used currently
618 local max_lines linecount tempfile prune_lines x
619 local harchive="${HISTFILE}_archive"
620 for x in "$HISTFILE" "$harchive"; do
621 [[ -e $x ]] || { touch "$x" && echo "notice from hl(): creating $x"; }
622 if [[ ! $x || ! -e $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
623 echo "error in hl: history file \$x:$x no good"
624 return 1
625 fi
626 done
627 history -a # save history
628 max_lines=$HISTFILELINES
629 [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
630 linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
631 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
632 if (($linecount > $max_lines)); then
633 prune_lines=$(($linecount - $max_lines))
634 head -n $prune_lines "$HISTFILE" >> "$harchive" \
635 && sed -ie "1,${prune_lines}d" $HISTFILE
636 fi
637 }
638
639 i() { git "$@"; }
640 # modified from ~/local/bin/git-completion.bash
641 # other completion commands are mostly taken from bash_completion package
642 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
643 || complete -o default -o nospace -F _git i
644
645 if ! type service &>/dev/null; then
646 service() {
647 echo actually running: systemctl $2 $1
648 systemctl $2 $1
649 }
650 fi
651
652
653
654
655 if [[ $OS == Windows_NT ]]; then
656 # cygstart wrapper
657 cs() {
658 cygstart "$@" &
659 }
660 xp() {
661 explorer.exe .
662 }
663 # launch
664 o() {
665 local x=(*$1*)
666 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
667 cygstart *$1* &
668 }
669 else
670 o() {
671 if type gvfs-open &> /dev/null ; then
672 gvfs-open "$@"
673 else
674 xdg-open "$@"
675 fi
676 # another alternative is run-mailcap
677 }
678 fi
679
680
681
682 istext() {
683 grep -Il "" "$@" &>/dev/null
684 }
685
686
687 l() {
688 if [[ $PWD == /[iap] ]]; then
689 command ls -A --color=auto -I lost+found "$@"
690 else
691 command ls -A --color=auto "$@"
692 fi
693 }
694
695
696 lcn() { locate -i "*$**"; }
697
698 lld() { ll -d "$@"; }
699
700 low() { # make filenames all lowercase
701 local x y
702 for x in "$@"; do
703 y=$(tr "[A-Z]" "[a-z]" <<<"$x")
704 [[ $y != $x ]] && mv "$x" "$y"
705 done
706 }
707
708
709
710
711 lower() { # make first letter of filenames lowercase.
712 local x
713 for x in "$@"; do
714 if [[ ${x::1} == [A-Z] ]]; then
715 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
716 safe_rename "$x" "$y"
717 fi
718 done
719 }
720
721 make-targets() {
722 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
723 make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
724 }
725
726
727 md5diff() {
728 [[ $(md5sum < "$1") != $(md5sum < "$2") ]]
729 }
730
731
732
733 mkc() {
734 mkdir "$1"
735 c "$1"
736 }
737
738
739 pakaraoke() {
740 # from http://askubuntu.com/questions/456021/remove-vocals-from-mp3-and-get-only-instrumentals
741 pactl load-module module-ladspa-sink sink_name=Karaoke master=alsa_output.usb-Audioengine_Audioengine_D1-00.analog-stereo plugin=karaoke_1409 label=karaoke control=-30
742 }
743
744
745 pfind() { #find *$1* in $PATH
746 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
747 local pathArray
748 IFS=: pathArray=($PATH); unset IFS
749 find "${pathArray[@]}" -iname "*$1*"
750 }
751
752 pick-trash() {
753 # trash-restore lists everything that has been trashed at or below CWD
754 # This picks out files just in CWD, not subdirectories,
755 # which also match grep $1, usually use $1 for a time string
756 # which you get from running restore-trash once first
757 local name x ask
758 local nth=1
759 # last condition is to not ask again for ones we skipped
760 while name="$( echo | restore-trash | gr "$PWD/[^/]\+$" | gr "$1" )" \
761 && [[ $name ]] && (( $(wc -l <<<"$name") >= nth )); do
762 name="$(echo "$name" | head -n $nth | tail -n 1 )"
763 read -p "$name [Y/n] " ask
764 if [[ ! $ask || $ask == [Yy] ]]; then
765 x=$( echo "$name" | gr -o "^\s*[0-9]*" )
766 echo $x | restore-trash > /dev/null
767 elif [[ $ask == [Nn] ]]; then
768 nth=$((nth+1))
769 else
770 return
771 fi
772 done
773 }
774
775 postconfin() {
776 local MAPFILE
777 mapfile -t
778 local s
779 [[ $EUID == 0 ]] || s=s
780 $s postconf -ev "${MAPFILE[@]}"
781 }
782
783 pub() {
784 rld /a/h/_site/ li:/var/www/iankelling.org/html
785 }
786
787 pwgen() {
788 apg -s -m 10 -x 14 -t
789 }
790
791
792 q() { # start / launch a program in the backround and redir output to null
793 "$@" &> /dev/null &
794 }
795
796 r() {
797 exit "$@" 2>/dev/null
798 }
799
800 rbpipe() { rbt post -o --diff-filename=- "$@"; }
801 rbp() { rbt post -o "$@"; }
802
803 rl() {
804 # rsync, root is required to keep permissions right.
805 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
806 # --no-times --delete
807 # basically, make an exact copy, use checksums instead of file times to be more accurate
808 rsync -ahvic --delete "$@"
809 }
810 rld() {
811 # like rlu, but don't delete files on the target end which
812 # do not exist on the original end.
813 rsync -ahvic "$@"
814 }
815 complete -F _rsync -o nospace rld rl rlt
816
817 rlt() {
818 # rl without preserving modification time.
819 rsync -ahvic --delete --no-t "$@"
820 }
821
822 rlu() { # [OPTS] HOST PATH
823 # eg rlu -opts frodo testpath
824 # useful for selectively sending dirs which have been synced with unison,
825 # where the path is the same on both hosts.
826 opts=("${@:1:$#-2}") # 1 to last -2
827 path="${@:$#}" # last
828 host="${@:$#-1:1}" # last -1
829 # rync here uses checksum instead of time so we don't mess with
830 # unison relying on time as much. g is for group, same reason
831 # to keep up with unison.
832 s rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
833 }
834
835
836 rspicy() { # HOST DOMAIN
837 local port=$(ssh $1<<EOF
838 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
839 sed -rn "s/.*port='([0-9]+).*/\1/p"
840 EOF
841 )
842 if [[ $port ]]; then
843 spicy -h $1 -p $port
844 else
845 echo "error: no port found. check that the domain is running."
846 fi
847 }
848
849 s() {
850 # background
851 # alias s='SUDOD="$PWD" sudo -i '
852 # because this is an alias, and the extra space at the end, it would allow
853 # aliases to be used with it. but aliases aren't used in scripts,
854 # better to eliminate inconsistencies. Plus, you can't do s=s; $s command
855 # with an alias, which I like to do in some functions
856 # extra space at the end allows aliases to work
857 #
858 # note: gksudo is recommended for X apps because it does not set the
859 # home directory to the same, and thus apps writing to ~ fuck things up
860 # with root owned files.
861 #
862 if [[ $EUID != 0 || $1 == -* ]]; then
863 SUDOD="$PWD" sudo -i "$@"
864 else
865 "$@"
866 fi
867 }
868
869 safe_rename() {
870 if [[ $# != 2 ]]; then
871 echo safe_rename error: $# args, need 2 >2
872 return 1
873 elif [[ $1 != $2 ]]; then
874 if [[ -e $2 ]]; then
875 echo Cannot rename "$1" to "$2" as it already exists.
876 else
877 mv "$1" "$2"
878 fi
879 fi
880 }
881
882 sdf() {
883 c /sdx/test/sandbox/
884 }
885
886 ser() {
887 local s; [[ $EUID != 0 ]] && s=sudo
888 if type -p systemctl &>/dev/null; then
889 $s systemctl $1 $2
890 else
891 $s service $2 $1
892 fi
893 }
894
895 sgo() { # service go
896 service=$1
897 ser restart $service
898 if type -p systemctl &>/dev/null; then
899 ser enable $service
900 fi
901 }
902
903
904 shellck() {
905 # 2086 = unquoted $var
906 # 2046 = unquoted $(cmd)
907 # i had -x as an arg, but debian testing(stretch) doesn't support it
908 shellcheck -e 2086,2046,2068,2006,2119 "$@"
909 }
910
911
912 slog() {
913 # log with script. timing is $1.t and script is $1.s
914 # -l to save to ~/typescripts/
915 # -t to add a timestamp to the filenames
916 local logdir do_stamp arg_base
917 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
918 logdir="/a/dt/"
919 do_stamp=false
920 while getopts "lt" option
921 do
922 case $option in
923 l ) arg_base=$logdir ;;
924 t ) do_stamp=true ;;
925 esac
926 done
927 shift $(($OPTIND - 1))
928 arg_base+=$1
929 [[ -e $logdir ]] || mkdir -p $logdir
930 $do_stamp && arg_base+=$(date +%F.%T%z)
931 script -t $arg_base.s 2> $arg_base.t
932 }
933 splay() { # script replay
934 #logRoot="$HOME/typescripts/"
935 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
936 scriptreplay "$1.t" "$1.s"
937 }
938
939 sr() {
940 # sudo redo. be aware, this command may not work right on strange distros or earlier software
941 if [[ $# == 0 ]]; then
942 sudo -E bash -c -l "$(history -p '!!')"
943 else
944 echo this command redos last history item. no argument is accepted
945 fi
946 }
947
948 srm () {
949 # with -ll, less secure but faster.
950 command srm -ll "$@"
951 }
952
953 t() {
954 local x
955 local -a args
956 if type -t trash-put >/dev/null; then
957 # skip args that don't exist, or else it's an err
958 for x in "$@"; do [[ ! -e $x ]] || args+=("$x"); done
959 [[ ! ${args[@]} ]] || trash-put "${args[@]}"
960 else
961 rm -rf "$@"
962 fi
963 }
964
965
966 tclock() {
967 clear
968 date +%l:%_M
969 len=60
970 # this goes to full width
971 #len=${1:-$((COLUMNS -7))}
972 x=1
973 while true; do
974 if (( x == len )); then
975 end=true
976 d="$(date +%l:%_M) "
977 else
978 end=false
979 d=$(date +%l:%M:%_S)
980 fi
981 echo -en "\r"
982 echo -n "$d"
983 for ((i=0; i<x; i++)); do
984 if (( i % 6 )); then
985 echo -n _
986 else
987 echo -n .
988 fi
989 done
990 if $end; then
991 echo
992 x=1
993 else
994 x=$((x+1))
995 fi
996 sleep 5
997 done
998 }
999
1000
1001 tm() {
1002 # timer in minutes
1003 (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
1004 }
1005
1006 ts() { # start editing a new file
1007 [[ $# != 1 ]] && echo "I need a filename." && return 1
1008 local quiet
1009 if [[ $- != *i* ]]; then
1010 quiet=true
1011 fi
1012 if [[ $1 == *.c ]]; then
1013 e '#include <stdio.h>' >"$1"
1014 e '#include <stdlib.h>' >>"$1"
1015 e 'int main(int argc, char * argv[]) {' >>"$1"
1016 e ' printf( "hello world\n");' >>"$1"
1017 e ' return 0;' >>"$1"
1018 e '}' >>"$1"
1019 e "${1%.c}: $1" > Makefile
1020 e " g++ -ggdb -std=gnu99 -o ${1%.c} $<" >> Makefile
1021 e "#!/bin/bash" >run.sh
1022 e "./${1%.c}" >>run.sh
1023 chmod +x run.sh
1024 elif [[ $1 == *.java ]]; then
1025 e "public class ${1%.*} {" >"$1"
1026 e ' public static void main(String[] args) {' >>"$1"
1027 e ' System.out.println("Hello, world!");' >>"$1"
1028 e ' }' >>"$1"
1029 e '}' >>"$1"
1030 else
1031 echo "#!/bin/bash" > "$1"
1032 chmod +x "$1"
1033 fi
1034 [[ $quiet ]] || g "$1"
1035 }
1036
1037
1038 tu() {
1039 local s;
1040 local dir="$(dirname "$1")"
1041 if [[ -e $1 && ! -w $1 || ! -w $(dirname "$1") ]]; then
1042 s=s;
1043 fi
1044 $s teeu "$@"
1045 }
1046
1047 tx() { # toggle set -x, and the prompt so it doesn't spam
1048 if [[ $- == *x* ]]; then
1049 set +x
1050 PROMPT_COMMAND=prompt_command
1051 else
1052 unset PROMPT_COMMAND
1053 PS1="\w \$ "
1054 set -x
1055 fi
1056 }
1057
1058 virshrm() {
1059 for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
1060 }
1061
1062 vm-set-listen(){
1063 local t=$(mktemp)
1064 local vm=$1
1065 local ip=$2
1066 s virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1067 sed -r "s/listen='[^']+/listen='$ip/"> $t
1068 s virsh undefine $vm
1069 s virsh define $t
1070 }
1071
1072
1073 vmshare() {
1074 vm-set-listen $1 0.0.0.0
1075 }
1076
1077
1078 vmunshare() {
1079 vm-set-listen $1 127.0.0.1
1080 }
1081
1082 vpn() {
1083 s systemctl start openvpn@client&
1084 journalctl --unit=openvpn@client -f -n0
1085 }
1086
1087 vpnoff() {
1088 s systemctl stop openvpn@client
1089 }
1090
1091
1092 vrm() {
1093 virsh destroy $1
1094 virsh undefine $1
1095 }
1096
1097
1098
1099 vspicy() {
1100 # connect to vms made with virt-install
1101 spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
1102 sed -r "s/.*port='([0-9]+).*/\1/")
1103 }
1104
1105
1106 whatismyip() { curl ipecho.net/plain ; echo; }
1107
1108
1109 #############################
1110 ######### misc stuff ########
1111 #############################
1112
1113 if [[ $- == *i* ]]; then
1114 # commands to run when bash exits normally
1115 trap "hl; _smh" EXIT
1116 fi
1117
1118
1119 # temporary variables to test colorization
1120 # some copied from gentoo /etc/bash/bashrc,
1121 use_color=false
1122 # dircolors --print-database uses its own built-in database
1123 # instead of using /etc/DIR_COLORS. Try to use the external file
1124 # first to take advantage of user additions.
1125 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
1126 match_lhs=""
1127 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
1128 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
1129 [[ -z ${match_lhs} ]] \
1130 && type -P dircolors >/dev/null \
1131 && match_lhs=$(dircolors --print-database)
1132 # test if our $TERM is in the TERM values in dircolor
1133 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
1134
1135
1136 if ${use_color} && [[ $- == *i* ]]; then
1137
1138 if [[ $XTERM_VERSION == Cygwin* ]]; then
1139 get_term_color() {
1140 for x in "$@"; do
1141 case $x in
1142 underl) echo -n $'\E[4m' ;;
1143 bold) echo -n $'\E[1m' ;;
1144 red) echo -n $'\E[31m' ;;
1145 green) echo -n $'\E[32m' ;;
1146 blue) echo -n $'\E[34m' ;;
1147 cyan) echo -n $'\E[36m' ;;
1148 yellow) echo -n $'\E[33m' ;;
1149 purple) echo -n $'\E[35m' ;;
1150 nocolor) echo -n $'\E(B\E[m' ;;
1151 esac
1152 done
1153 }
1154
1155 else
1156 get_term_color() {
1157 for x in "$@"; do
1158 case $x in
1159 underl) echo -n $(tput smul) ;;
1160 bold) echo -n $(tput bold) ;;
1161 red) echo -n $(tput setaf 1) ;;
1162 green) echo -n $(tput setaf 2) ;;
1163 blue) echo -n $(tput setaf 4) ;;
1164 cyan) echo -n $(tput setaf 6) ;;
1165 yellow) echo -n $(tput setaf 3) ;;
1166 purple) echo -n $(tput setaf 5) ;;
1167 nocolor) echo -n $(tput sgr0) ;; # no font attributes
1168 esac
1169 done
1170 }
1171 fi
1172 else
1173 get_term_color() {
1174 :
1175 }
1176 fi
1177 # Try to keep environment pollution down, EPA loves us.
1178 unset safe_term match_lhs use_color
1179
1180
1181
1182
1183
1184
1185 ###############
1186 # prompt ######
1187 ###############
1188
1189
1190 if [[ $- == *i* ]]; then
1191 # git branch/status prompt function
1192 if [[ $OS != Windows_NT ]]; then
1193 GIT_PS1_SHOWDIRTYSTATE=true
1194 fi
1195 # arch source lopip show -fcation
1196 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
1197 # fedora/debian source
1198 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
1199
1200 # in case we didn't source git-prompt.sh
1201 if ! declare -f __git_ps1 > /dev/null; then
1202 __git_ps1() {
1203 :
1204 }
1205 fi
1206
1207 # this needs to come before next ps1 stuff
1208 # this stuff needs bash 4, feb 2009,
1209 # old enough to no longer condition on $BASH_VERSION anymore
1210 shopt -s autocd
1211 shopt -s dirspell
1212 PS1='\w'
1213 if [[ $- == *i* ]] && [[ ! $INSIDE_EMACS ]]; then
1214 PROMPT_DIRTRIM=2
1215 bind -m vi-command B:shell-backward-word
1216 bind -m vi-command W:shell-forward-word
1217 fi
1218
1219 if [[ $SSH_CLIENT ]]; then
1220 PS1="\h $PS1"
1221 fi
1222
1223 prompt_command() {
1224 local return=$? # this MUST COME FIRST
1225 local psc pst
1226 local ps_char ps_color
1227 unset IFS
1228 history -a # save history
1229 if [[ ! $DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
1230 # from the screen man page
1231 if [[ $TERM == screen* ]]; then
1232 local title_escape="\033]..2;"
1233 else
1234 local title_escape="\033]0;"
1235 fi
1236 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
1237 fi
1238
1239 case $return in
1240 0) ps_color="$(get_term_color blue)"
1241 ps_char='\$'
1242 ;;
1243 1) ps_color="$(get_term_color green)"
1244 ps_char="$return \\$"
1245 ;;
1246 *) ps_color="$(get_term_color yellow)"
1247 ps_char="$return \\$"
1248 ;;
1249 esac
1250 if [[ ! -O . ]]; then # not owner
1251 if [[ -w . ]]; then # writable
1252 ps_color="$(get_term_color bold red)"
1253 else
1254 ps_color="$(get_term_color bold green)"
1255 fi
1256 fi
1257 PS1="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
1258 # emacs completion doesn't like the git prompt atm, so disabling it.
1259 #PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
1260 }
1261 PROMPT_COMMAND=prompt_command
1262 fi
1263
1264
1265
1266
1267
1268 ###########################################
1269 # stuff that makes sense to be at the end #
1270 ###########################################
1271 if [[ "$SUDOD" ]]; then
1272 cd "$SUDOD"
1273 unset SUDOD
1274 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
1275 cd /a
1276 fi
1277
1278
1279 # best practice
1280 unset IFS
1281
1282
1283 # if someone exported $SOE, catch errors
1284 if [[ $SOE ]]; then
1285 errcatch
1286 fi
1287
1288 # I'd prefer to have system-wide, plus user ruby, due to bug in it
1289 # https://github.com/rubygems/rubygems/pull/1002
1290 # further problems: installing multi-user ruby and user ruby,
1291 # you don't get multi-user ruby when you sudo to root, unless its sudo -i.
1292 # There a third hybrid form, which passenger error suggested I use,
1293 # but it didn't actually work.
1294
1295 # in cased I never need this
1296 # rvm for non-interactive shell: modified from https://rvm.io/rvm/basics
1297 #if [[ $(type -t rvm) == file && ! $(type -t ruby) ]]; then
1298 # source $(rvm 1.9.3 do rvm env --path)
1299 #fi
1300
1301 # based on warning from rvmsudo
1302 export rvmsudo_secure_path=1
1303
1304
1305 if [[ -s "/usr/local/rvm/scripts/rvm" ]]; then
1306 source "/usr/local/rvm/scripts/rvm"
1307 elif [[ -s $HOME/.rvm/scripts/rvm ]]; then
1308 source $HOME/.rvm/scripts/rvm
1309 fi
1310
1311 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
1312 # i added an extra condition as gentoo xorg guide says depending on
1313 # $DISPLAY is fragile.
1314 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
1315 exec startx
1316 fi
1317 # ensure no bad programs appending to this file will have an affect
1318 return 0