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