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