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