2 # this gets sourced. shebang is just for file mode detection
4 # note, to catch errors in functions but not outside, do:
7 # trap 'trap ERR' RETURN
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
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
28 # use extra globing features.
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
=*/.
:*/..
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.
44 # make tab on an empty line do nothing
45 shopt -s no_empty_cmd_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
55 # fix spelling errors for cd, only in interactive shell
57 # append history instead of overwritting it
59 # for compatibility, per gentoo/debian bashrc
61 # attempt to save multiline single commands as single history entries.
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
72 export RLC_INSIDE_EMACS
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
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('');
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
98 export NODE_NO_READLINE
=1
103 if [[ $
- == *i
* ]]; then
104 # for readline-complete.el
105 if [[ $RLC_INSIDE_EMACS ]]; then
106 # all for readline-complete.el
108 bind 'set horizontal-scroll-mode on'
109 bind 'set print-completions-horizontally on'
110 bind '"\C-i": self-insert'
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
117 bind '"\eOc": shell-forward-word'
118 bind '"\eOd": shell-backward-word'
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
129 # history number. History expansion is good.
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
135 # max commands 1 session can append/read from history
137 # my own history size limit based on lines
138 HISTFILELINES
=1000000
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 *:[ ]*'
151 export BC_LINE_LENGTH
=0
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
163 for _x
in /a
/bin
/distro-functions
/src
/* /a
/bin
/!(githtml
)/*-function?
(s
); do
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
172 path_add
--ifexists --end /a
/opt
/adt-bundle
*/tools
/a
/opt
/adt-bundle
*/platform-tools
174 # based on readme.debian. dunno if this will break on other distros.
175 _x
=/usr
/share
/wcd
/wcd-include.sh
176 if [[ -e $_x ]]; then source $_x; fi
183 # very few aliases, functions are always preferred.
186 if [[ $OS == Windows_NT
]]; then
187 alias ffs
='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
189 alias j
='command cygpath'
190 alias t
='command cygstart'
191 alias cygstart
='echo be quick, use the alias "t" instead :\)'
192 alias cygpath
='echo be quick, use the alias "j" instead :\)'
197 # keep this in mind? good for safety.
202 # remove any default aliases for these
203 unalias ls ll
grep &>/dev
/null ||
:
213 #####################
215 #####################
220 ....
() { c ..
/..
/..
; }
221 .....
() { c ..
/..
/..
/..
; }
222 ......
() { c ..
/..
/..
/..
/..
; }
225 # file cut copy and paste, like the text buffers :)
226 # I havn't tested these.
227 _fbufferinit
() { # internal use by
228 ! [[ $my_f_tempdir ]] && my_f_tempdir
=$
(mktemp
-d)
229 rm -rf "$my_f_tempdir"/*
233 cp "$@" "$my_f_tempdir"/
237 mv "$@" "$my_f_tempdir"/
239 fpst
() { # file paste
240 [[ $2 ]] && { echo too many arguments
; return 1; }
242 cp "$my_f_tempdir"/* "$target"
247 complete
-F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
251 # join options which are continued to multiples lines onto one line
253 grep -vE '^([ \t]*#|^[ \t]*$)' "$1" |
while IFS
= read -r line
; do
254 # remove leading spaces/tabs. assumes extglob
255 if [[ $line == "[ ]*" ]]; then
261 elif [[ $line == *=* ]]; then
262 echo "$pastline" >> "$2"
265 pastline
="$pastline $line"
268 echo "$pastline" >> "$2"
274 ssh-keygen
-R $h -f $
(readlink
-f ~
/.ssh
/known_hosts
)
275 x
=$
(timeout
1 ssh -oBatchMode=yes -v $1 |
& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p")
277 echo "khfix: ssh failed"
280 echo "khfix: removing key for $x"
281 ssh-keygen
-R $x -f $
(readlink
-f ~
/.ssh
/known_hosts
)
283 khfix
() { # known hosts fix
284 _khfix_common
"$@" ||
return 1
296 ack
() { ack-grep
"$@"; }
299 # googling android emulator libGL error: failed to load driver: r600
300 # lead to http://stackoverflow.com/a/36625175/14456
301 export ANDROID_EMULATOR_USE_SYSTEM_LIBS
=1
302 /a
/opt
/android-studio
/bin
/studio.sh
"$@" &r
;
311 # use -p from interactive shell
315 bfg
() { java
-jar /a
/opt
/bfg-1.12
.14.jar
"$@"; }
318 local f
=/etc
/bitcoin
/bitcoin.conf
319 # importprivkey will timeout if using the default of 15 mins.
320 # upped it to 1 hour.
321 bitcoin-cli
-rpcclienttimeout=60000 -$
(s
grep rpcuser
= $f) -$
(s
grep rpcpassword
= $f) "$@"
326 price
="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
327 printf "$%s\n" "$price"
329 printf "$%.2f\n" "$(echo "scale
=4; $price * $1"| bc -l)"
334 price
="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
335 printf "$%s\n" "$price"
337 # 100 mil satoshi / btc. 8 digits after the 1.
338 printf "%.8f btc\n" "$(echo "scale
=10; $1 / $price "| bc -l)"
343 price
="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
344 price
=$
(echo "scale=10; $price * 0.00000001"|
bc -l)
345 printf "$%f\n" "$price"
347 printf "$%.2f\n" "$(echo "scale
=10; $price * $1"| bc -l)"
353 if [[ $RLC_INSIDE_EMACS ]]; then
354 c
() { wcd
-c -z 50 -o "$@"; }
356 # lets see what the fancy terminal does from time to time
357 c
() { wcd
-c -z 50 "$@"; }
360 caa
() { git commit
--amend --no-edit -a; }
363 find -L $1 -type f
-not \
( -name .svn
-prune -o -name .git
-prune \
364 -o -name .hg
-prune -o -name .editor-backups
-prune \
365 -o -name .undo-tree-history
-prune \
) \
366 -exec bash
-lc 'hr; echo "$1"; hr; cat "$1"' _
{} \
; 2>/dev
/null
370 calc
() { echo "scale=3; $*" |
bc -l; }
371 # no having to type quotes, but also no command history:
375 echo "scale=3; $x" |
bc -l
382 ccat
() { # config cat. see a config without extra lines.
383 grep '^\s*[^[:space:]#]' "$@"
388 # setup for format of postfix, eg:
392 local unified
="$(mktemp)"
395 _cdiff-prep
"$1" "$f1"
396 _cdiff-prep
"$2" "$f2"
397 cat "$f1" "$f2" |
grep -Po '^[^=]+=' |
sort |
uniq > "$unified"
398 while IFS
= read -r line
; do
399 # the default bright red / blue doesn't work in emacs shell
400 dwdiff
-cblue,red
-A best
-d " ," <(grep "^$line" "$f1" ||
echo ) <(grep "^$line" "$f2" ||
echo ) | colordiff
407 cp /a
/bin
/data
/COPYING
"$@"
409 cp /a
/bin
/data
/COPYING .
415 cp /a
/bin
/data
/LICENSE
"$@"
417 cp /a
/bin
/data
/LICENSE .
421 # makes it so chown -R symlink affects the symlink and its target.
422 if [[ $1 == -R ]]; then
424 command chown
-h "$@"
425 command chown
-R "$@"
436 # choose recent directory. cl = cd list
441 CHROMIUM_FLAGS
='--enable-remote-extensions' chromium
&r
445 complete
-A stopped
-P '"%' -S '"' d
447 dat
() { # do all tee, for more complex scripts
448 tee >(ssh frodo bash
-l) >(bash
-l) >(ssh x2 bash
-l) >(ssh tp bash
-l)
453 for host in x2 tp treetowl
; do
459 diff --strip-trailing-cr -w "$@" # diff content
462 debian_pick_mirror
() {
463 # netselect-apt finds a fast mirror.
464 # but we need to replace the mirrors ourselves,
465 # because it doesn't do that. best it can do is
466 # output a basic sources file
467 # here we get the server it found, get the main server we use
468 # then substitute all instances of one for the other in the sources file
469 # and backup original to /etc/apt/sources.list-original.
470 # this is idempotent. the only way to identify debian sources is to
471 # note the original server, so we put it in a comment so we can
473 local file=$
(mktemp
-d)/f
# safe way to get file name without creating one
474 sudo netselect-apt
-o "$file" ||
return 1
475 url
=$
(grep ^
\\w
$file |
head -n1 |
awk '{print $2}')
476 sudo
cp -f /etc
/apt
/sources.list
/etc
/apt
/sources.list-original
477 sudo
sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc
/apt
/sources.list
485 safe_rename
"$x" "$y"
490 date "+%A, %B %d, %r" "$@"
493 dus
() { # du, sorted, default arg of
494 du
-sh ${@:-*} |
sort -h
503 [[ ${#@} == 2 ]] ||
{ echo "error: ediff requires 2 arguments"; return 1; }
504 emacs
--eval "(ediff-files \"$1\" \"$2\")"
508 envload
() { # load environment from a previous: export > file
509 local file=${1:-$HOME/.${USER}_env}
510 eval "$(export | sed 's/^declare -x/export -n/')"
511 while IFS
= read -r line
; do
512 # declare -x makes variables local to a function
513 eval ${line/#declare -x/export}
523 # find array. make an array of file names found by find into $x
524 # argument: find arguments
525 # return: find results in an array $x
526 while read -rd ''; do
528 done < <(find "$@" -print0);
531 faf
() { # find all files
532 find -L $1 -type f
-not \
( -name .svn
-prune -o -name .git
-prune \
533 -o -name .hg
-prune -o -name .editor-backups
-prune \
534 -o -name .undo-tree-history
-prune \
) 2>/dev
/null
537 fastboot
() { /a
/opt
/androidsdk
/platform-tools
/fastboot
"$@"; }
540 # List of apps to install/update
541 # Create from existing manually installed apps by doing
542 # fdroidcl search -i, then manually removing
543 # automatically installed/preinstalled apps
546 com.alaskalinuxuser.justnotes
547 com.artifex.mupdfdemo
550 com.jmstudios.redmoon
552 com.nutomic.syncthingandroid
554 com.zoffcc.applications.zanavi
555 cz.martykan.forecastie
557 de.marmaro.krt.ffupdater
558 im.vector.alpha
# riot
560 me.ccrama.redditslide
561 me.tripsit.tripmobile
564 net.sourceforge.opencamera
565 org.dmfs.tasks
# caldav tasks thing
568 org.kde.kdeconnect_tp
569 org.quantumbadger.redreader
570 org.smssecure.smssecure
571 org.fedorahosted.freeotp
573 # https://forum.xda-developers.com/android/software-hacking/wip-selinux-capable-superuser-t3216394
578 local -A installed updated
581 for p
in $
(fdroidcl search
-i|
grep -o "^\S\+"); do
584 for p
in $
(fdroidcl search
-u|
grep -o "^\S\+"); do
587 for p
in ${fdroid_pkgs[@]}; do
588 ${installed[$p]:-false} || fdroidcl
install $p
590 for p
in ${!installed[@]}; do
591 ${updated[$p]:-true} || fdroidcl upgrade
$p
595 firefox-default-profile
() {
596 key
=Default value
=1 section
=$1
597 file=/p
/c
/subdir_files
/.mozilla
/firefox
/profiles.ini
598 sed -ri "/^ *$key/d" "$file"
599 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key=$value" "$file"
601 fdhome
() { #firefox default home profile
602 firefox-default-profile Profile0
606 firefox-default-profile Profile4
610 if type -P firefox
&>/dev
/null
; then
620 firefox
-P alt
"$@" >/dev
/null
2>&1
626 local dname
="${PWD##*/}"
627 local m
="/a/tmp/$dname-missing"
628 local d
="/a/tmp/$dname-diff"
629 [[ -e $d ]] && rm "$d"
630 [[ -e $m ]] && rm "$m"
633 while read -r line
; do
635 if [[ -e "$fsfile" ]]; then
636 md5diff
"$line" "$fsfile" && tee -a "/a/tmp/$dname-diff" <<< "$fsfile $line"
642 done < <(find -type f
)
645 (( msize
<= 100 )) && cat $m
649 # expected output, with different tmp dirs
650 # /tmp/tmp.HDPbwMqdC9/c/d ./c/d
651 # /a/tmp/tmp.qLDkYxBYPM-missing
660 echo different
> $x/c
/d
665 # test whether missing files were renamed, generally for use with fsdiff
666 # $1 = fsdiff output file, $2 = directory to compare to. pwd = fsdiff dir
667 # echos non-renamed files
671 { sums
+=( "$(md5sum < "$x")" ) ; } 2>/dev
/null
673 while read -r line
; do
674 { missing_sum
=$
(md5sum < "$line") ; } 2>/dev
/null
676 for x
in "${sums[@]}"; do
677 if [[ $missing_sum == "$x" ]]; then
682 $renamed ||
echo "$line"
688 # F = fullscren, z = random, Z = auto zoom
689 command feh
-FzZ "$@"
693 # like -e for functions. returns on error.
694 # at the end of the function, disable with:
696 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
697 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
704 firefox
-P default
"$@" >/dev
/null
2>&1
708 local help="Usage: getdir [--help] PATH
709 Output the directory of PATH, or just PATH if it is a directory."
710 if [[ $1 == --help ]]; then
714 if [[ $# -ne 1 ]]; then
715 echo "getdir error: expected 1 argument, got $#"
721 local dir
="$(dirname "$1")"
722 if [[ -d $dir ]]; then
725 echo "getdir error: directory does not exist"
731 git_empty_branch
() { # start an empty git branch. carefull, it deletes untracked files.
732 [[ $# == 1 ]] ||
{ echo 'need a branch name!'; return 1;}
734 gitroot ||
return 1 # function to set gitroot
735 builtin cd "$gitroot"
736 git symbolic-ref HEAD refs
/heads
/$1
742 local help="Usage: gitroot [--help]
743 Print the full path to the root of the current git repo
745 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
746 and works in older versions of git which did not have that."
747 if [[ $1 == --help ]]; then
751 local p
=$
(git rev-parse
--git-dir) ||
{ echo "error: not in a git repo" ; return 1; }
752 [[ $p != /* ]] && p
=$PWD
757 # quit will prompt if the program crashes.
758 gdb
-ex=r
-ex=quit
--args emacs
"$@"; r
;
762 # kill the emacs daemon
767 git send-email
--notes '--envelope-sender=<ian@iankelling.org>' \
768 --suppress-cc=self
"$@"
772 grep -iIP --color=auto
"$@"
776 if [[ ${#@} == 1 ]]; then
777 grep -riIP --color=auto
"$@" .
779 grep -riIP --color=auto
"$@"
784 # do git status on published repos
786 for x
in !(forks
) forks
/* ian-specific
/*; do
787 cd `readlink -f $x`/..
795 hl
() { # history limit. Write extra history to archive file.
796 # todo: this is not working or not used currently
797 local max_lines linecount tempfile prune_lines x
798 local harchive
="${HISTFILE}_archive"
799 for x
in "$HISTFILE" "$harchive"; do
800 [[ -e $x ]] ||
{ touch "$x" && echo "notice from hl(): creating $x"; }
801 if [[ ! $x ||
! -e $x ||
! -w $x || $
(stat
-c "%u" "$x") != $EUID ]]; then
802 echo "error in hl: history file \$x:$x no good"
806 history -a # save history
807 max_lines
=$HISTFILELINES
808 [[ $max_lines =~ ^
[0-9]+$
]] ||
{ echo "error in hl: failed to get max line count"; return 1; }
809 linecount
=$
(wc -l < $HISTFILE) # pipe so it doesn't output a filename
810 [[ $linecount =~ ^
[0-9]+$
]] ||
{ echo "error in hl: wc failed"; return 1; }
811 if (($linecount > $max_lines)); then
812 prune_lines
=$
(($linecount - $max_lines))
813 head -n $prune_lines "$HISTFILE" >> "$harchive" \
814 && sed --follow-symlinks -ie "1,${prune_lines}d" $HISTFILE
818 hr
() { # horizontal row. used to break up output
819 printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $
(seq ${COLUMNS:-60})
823 hrcat
() { local f
; for f
; do [[ -f $f ]] ||
continue; hr
; echo "$f"; cat "$f"; done }
827 # modified from ~/local/bin/git-completion.bash
828 # other completion commands are mostly taken from bash_completion package
829 complete
-o bashdefault
-o default
-o nospace
-F _git i
2>/dev
/null \
830 || complete
-o default
-o nospace
-F _git i
832 if ! type service
&>/dev
/null
; then
834 echo actually running
: systemctl
$2 $1
845 /a
/opt
/idea-IC-163.7743
.44/bin
/idea.sh
"$@" &r
850 find -L .
-not \
( -name .svn
-prune -o -name .git
-prune \
851 -o -name .hg
-prune -o -name .editor-backups
-prune \
852 -o -name .undo-tree-history
-prune \
) -iname "*$**" 2>/dev
/null
856 if [[ $OS == Windows_NT
]]; then
867 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
872 if type gvfs-open
&> /dev
/null
; then
877 # another alternative is run-mailcap
882 e
"lspci -nnk|gr -iA2 net"
883 lspci
-nnk|gr
-iA2 net
885 e
"s lshw -C network"
892 grep -Il "" "$@" &>/dev
/null
896 journalctl
-n 10000 -f "$@" |
grep -Evi "^(\S+\s+){4}(sudo|sshd|cron)"
901 if [[ $PWD == /[iap
] ]]; then
902 command ls -A --color=auto
-I lost
+found
"$@"
904 command ls -A --color=auto
"$@"
909 lcn
() { locate -i "*$**"; }
911 lld
() { ll
-d "$@"; }
913 low
() { # make filenames lowercase, remove bad chars
916 new
="${f,,}" # downcase
917 new
="${new//[^[:alnum:]._-]/_}" # sub bad chars
918 new
="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
919 new
="${new%"${new##*[[:alnum:]]}"}"
920 # remove bad underscores, like __ and _._
921 new
=$
(echo $new |
sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
922 safe_rename
"$f" "$new" ||
return 1
927 lower
() { # make first letter of filenames lowercase.
930 if [[ ${x::1} == [A-Z
] ]]; then
931 y
=$
(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
932 safe_rename
"$x" "$y" ||
return 1
938 k
() { # history search
939 grep -P --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
tail -n 80;
942 ks
() { # history search
943 grep -P --binary-files=text
"$@" ${HISTFILE:-~/.bash_history} |
uniq;
948 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
949 make -qp |
awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
957 mv -T $src $dst ||
{ set +x
; return 1; }
966 if [[ -L $dst ]]; then rm $dst; fi
977 mkt
() { # mkdir and touch file
979 mkdir
-p "$(dirname "$path")"
983 mkdir
() { command mkdir
-p "$@"; }
986 echo "oathtool --totp -b"
987 oathtool
--totp -b "$@"
992 export PYTHONPATH
=/a
/opt
/Pithosfly
997 # from http://askubuntu.com/questions/456021/remove-vocals-from-mp3-and-get-only-instrumentals
998 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
1002 pfind
() { #find *$1* in $PATH
1003 [[ $# != 1 ]] && { echo requires
1 argument
; return 1; }
1005 IFS
=: pathArray
=($PATH); unset IFS
1006 find "${pathArray[@]}" -iname "*$1*"
1011 pid
=($
(pgrep
-f "$*"))
1017 0) echo "no pid found" ;;
1025 # trash-restore lists everything that has been trashed at or below CWD
1026 # This picks out files just in CWD, not subdirectories,
1027 # which also match grep $1, usually use $1 for a time string
1028 # which you get from running restore-trash once first
1031 # last condition is to not ask again for ones we skipped
1032 while name
="$( echo | restore-trash | gr "$PWD/[^
/]\
+$
" | gr "$1" )" \
1033 && [[ $name ]] && (( $
(wc -l <<<"$name") >= nth
)); do
1034 name
="$(echo "$name" | head -n $nth | tail -n 1 )"
1035 read -p "$name [Y/n] " ask
1036 if [[ ! $ask ||
$ask == [Yy
] ]]; then
1037 x
=$
( echo "$name" | gr
-o "^\s*[0-9]*" )
1038 echo $x | restore-trash
> /dev
/null
1039 elif [[ $ask == [Nn
] ]]; then
1047 ping8
() { ping 8.8.8.8; }
1050 rld
/a
/h
/_site
/ li
:/var
/www
/iankelling.org
/html
1053 pubip
() { curl
-4s https
://icanhazip.com
; }
1054 whatismyip
() { pubip
; }
1057 # fixes the menu bar in xmonad. this won\'t be needed when xmonad
1058 # packages catches up on some changes in future (this is written in
1061 # geekosaur: so you'll want to upgrade to xmonad 0.13 or else use a
1062 # locally modified XMonad.Hooks.ManageDocks that doesn't set the
1063 # work area; turns out it's impossible to set correctly if you are
1064 # not a fully EWMH compliant desktop environment
1066 # geekosaur: chrome shows one failure mode, qt/kde another, other
1067 # gtk apps a third, ... I came up with a setting that works for me
1068 # locally but apparently doesn't work for others, so we joined the
1069 # other tiling window managers in giving up on setting it at all
1071 xprop
-root -remove _NET_WORKAREA
1079 # -t = print pronunciation
1084 # -M CLN = use Caps, Lowercase, Numbers
1086 # -a 1 = use random instead of pronounceable algorithm
1087 apg
-m 50 -x 70 -n 1 -a 1 -M CLN
1091 q
() { # start / launch a program in the backround and redir output to null
1096 exit "$@" 2>/dev
/null
1099 rbpipe
() { rbt post
-o --diff-filename=- "$@"; }
1100 rbp
() { rbt post
-o "$@"; }
1103 # rsync, root is required to keep permissions right.
1104 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
1105 # --no-times --delete
1106 # basically, make an exact copy, use checksums instead of file times to be more accurate
1107 rsync
-ahvic --delete "$@"
1110 # like rlu, but don't delete files on the target end which
1111 # do not exist on the original end.
1114 complete
-F _rsync
-o nospace rld rl rlt
1117 # rl without preserving modification time.
1118 rsync
-ahvic --delete --no-t "$@"
1121 rlu
() { # [OPTS] HOST PATH
1122 # eg. rlu -opts frodo /testpath
1123 # relative paths will expanded with readlink -f.
1124 # useful for selectively sending dirs which have been synced with unison,
1125 # where the path is the same on both hosts.
1126 opts
=("${@:1:$#-2}") # 1 to last -2
1127 path
="${@:$#}" # last
1128 host="${@:$#-1:1}" # last -1
1129 if [[ $path == .
* ]]; then
1130 path
=$
(readlink
-f $path)
1132 # rync here uses checksum instead of time so we don't mess with
1133 # unison relying on time as much. g is for group, same reason
1134 # to keep up with unison.
1135 s rsync
-rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
1138 r2eadd
() { # usage: name url
1139 # initial setup of rss2email:
1140 # r2e new r2e@iankelling.org
1141 # that initializes files, and sets default email.
1142 # symlink to the config doesn't work, so I copied it to /p/c
1143 # and then use cli option to specify explicit path.
1144 # Only option changed from default config is to set
1147 # or else for a few feeds, the from address is set by the feed, and
1148 # if I fail delivery, then I send a bounce message to that from
1149 # address, which makes me be a spammer.
1151 r2e add
$1 "$2" $1@r2e.iankelling.org
1152 # get up to date and don't send old entries now:
1153 r2e run
--no-send $1
1155 r2e
() { command r2e
-d /p
/c
/rss2email.json
-c /p
/c
/rss2email.cfg
"$@"; }
1157 rspicy
() { # usage: HOST DOMAIN
1158 # connect to spice vm remote host. use vspicy for local host
1159 local port
=$
(ssh $1<<EOF
1160 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
1161 sed -rn "s/.*port='([0-9]+).*/\1/p"
1164 if [[ $port ]]; then
1165 spicy
-h $1 -p $port
1167 echo "error: no port found. check that the domain is running."
1172 ssh fencepost
head -n 300 /gd
/gnuorg
/EventAndTravelInfo
/rms-current-trips.txt
1177 # I use a function because otherwise we can't use in a script,
1178 # can't assign to variable.
1180 # note: gksudo is recommended for X apps because it does not set the
1181 # home directory to the same, and thus apps writing to ~ fuck things up
1182 # with root owned files.
1184 if [[ $EUID != 0 ||
$1 == -* ]]; then
1185 SUDOD
="$PWD" sudo
-i "$@"
1191 safe_rename
() { # warn and don't rename if file exists.
1192 # mv -n exists, but it's silent
1193 if [[ $# != 2 ]]; then
1194 echo safe_rename error
: $# args
, need
2 >2
1197 if [[ $1 != $2 ]]; then # yes, we want to silently ignore this
1198 if [[ -e $2 ||
-L $2 ]]; then
1199 echo "Cannot rename $1 to $2 as it already exists."
1207 sb
() { # sudo bash -c
1208 # use sb instead of s is for sudo redirections,
1209 # eg. sb 'echo "ok fine" > /etc/file'
1211 sudo
-i bash
-c "$@"
1213 complete
-F _root_command s sb
1216 # s gem install scss-lint
1217 pushd /a
/opt
/thoughtbot-guides
1220 scss-lint
-c /a
/opt
/thoughtbot-guides
/style
/sass
/.scss-lint.yml
"$@"
1224 local s
; [[ $EUID != 0 ]] && s
=sudo
1225 if type -p systemctl
&>/dev
/null
; then
1232 sgo
() { # service go
1234 ser restart
$service
1235 if type -p systemctl
&>/dev
/null
; then
1242 # 2086 = unquoted $var
1243 # 2046 = unquoted $(cmd)
1244 # i had -x as an arg, but debian testing(stretch) doesn't support it
1245 shellcheck
-e 2086,2046,2068,2006,2119 "$@"
1251 out
=${2:-${1%.*}.sh}
1253 script -t -c "mpv --no-config --no-resume-playback --no-terminal --no-audio-display '$1'" $tmp/typescript
2>$tmp/timing
1254 # todo, the current sleep seems pretty good, but it
1255 # would be nice to have an empirical measurement, or
1256 # some better wait to sync up.
1258 # note: --loop-file=no prevents it from hanging if you have that
1259 # set to inf the mpv config.
1260 # --loop=no prevents it from exit code 3 due to stdin if you
1261 # had it set to inf in mpv config.
1263 # args go to mpv, for example --volume=80, 50%
1264 cat >$out <<EOFOUTER
1266 trap "trap - TERM && kill 0" INT TERM ERR; set -e
1267 ( sleep .2; scriptreplay <( cat <<'EOF'
1271 $(cat $tmp/typescript)
1274 base64 -d - <<'EOF'| mpv --loop=no --loop-file=no --no-terminal --no-audio-display "\$@" -
1284 # log with script. timing is $1.t and script is $1.s
1285 # -l to save to ~/typescripts/
1286 # -t to add a timestamp to the filenames
1287 local logdir do_stamp arg_base
1288 (( $# >= 1 )) ||
{ echo "arguments wrong"; return 1; }
1291 while getopts "lt" option
1294 l
) arg_base
=$logdir ;;
1295 t
) do_stamp
=true
;;
1298 shift $
(($OPTIND - 1))
1300 [[ -e $logdir ]] || mkdir
-p $logdir
1301 $do_stamp && arg_base
+=$
(date +%F.
%T
%z
)
1302 script -t $arg_base.s
2> $arg_base.t
1304 splay
() { # script replay
1305 #logRoot="$HOME/typescripts/"
1306 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1307 scriptreplay
"$1.t" "$1.s"
1315 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1316 if [[ $# == 0 ]]; then
1317 sudo
-E bash
-c -l "$(history -p '!!')"
1319 echo this
command redos last
history item. no argument is accepted
1324 # with -ll, less secure but faster.
1325 command srm
-ll "$@"
1330 ssh $1 /tmp
/${2##*/} "${@:2}"
1344 if type -t trash-put
>/dev
/null
; then
1345 # skip args that don't exist, or else trash-put will have an error
1347 if [[ -e $x ||
-L $x ]]; then
1351 [[ ! ${args[@]} ]] || trash-put
"${args[@]}"
1362 # this goes to full width
1363 #len=${1:-$((COLUMNS -7))}
1366 if (( x
== len
)); then
1368 d
="$(date +%l:%_M) "
1371 d
=$
(date +%l
:%M
:%_S
)
1375 for ((i
=0; i
<x
; i
++)); do
1376 if (( i
% 6 )); then
1394 # test existence / exists
1397 [[ -e "$x" ||
-L "$x" ]] || ret
=1
1403 declare -gi _seq
; _seq
+=1
1404 echo "test body" | m
mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
1405 # for testing to send from an external address, you can do for example
1406 # -aFrom:ian@iank.bid web-6fnbs@mail-tester.com
1407 # note in exim, you can retry a deferred message
1409 # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
1412 # to test sieve, use below command. make modifications, then copy to live file, use -eW to actually modify mailbox
1413 # cp /p/c/subdir_files/sieve/personal{test,}.sieve; testsievelist -eW INBOX
1415 # Another option is to use sieve-test SCRIPT MAIL_FILE. note,
1416 # sieve-test doesn't know about envelopes, I'm not sure if sieve-filter does.
1418 # sieve with output filter. arg is mailbox, like INBOX.
1419 # This depends on dovecot conf, notably mail_location in /etc/dovecot/conf.d/10-mail.conf
1422 sieve-filter ~
/sieve
/maintest.sieve
"$@" >/tmp
/testsieve.log
2> >(tail) && sed -rn '/^Performed actions:/{n;n;p}' /tmp
/testsieve.log |
sort -u
1428 sieve-filter ~
/sieve
/main.sieve
"$@"
1432 # testmail above calls sendmail, which is a link to exim/postfix.
1433 # it's docs don't say a way of adding an argument
1434 # to sendmail to turn on debug output. We could make a wrapper, but
1435 # that is a pain. Exim debug args are documented here:
1436 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
1438 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
1439 # note, for exim daemon, you can turn on debug options by
1440 # adding -d, etc to COMMONOPTIONS in
1441 # /etc/default/exim4
1442 # for testing external mail, you need the to address as final cmdline arg
1443 exim
-d+tls
-t <<'EOF'
1444 From: root@frodo.lan
1445 To: ian@mail.iankelling.org
1446 Subject: Testing Exim
1448 This is a test message.
1455 (sleep $
(calc
"$@ * 60") && mpv
--no-config --volume 50 /a
/bin
/data
/alarm.mp3
) > /dev
/null
2>&1 &
1461 local dir
="$(dirname "$1")"
1462 if [[ -e $1 && ! -w $1 ||
! -w $
(dirname "$1") ]]; then
1468 tx
() { # toggle set -x, and the prompt so it doesn't spam
1469 if [[ $
- == *x
* ]]; then
1471 PROMPT_COMMAND
=prompt_command
1473 unset PROMPT_COMMAND
1480 # show all processes in the network namespace $1.
1481 # blank entries appear to be subprocesses/threads
1485 s
find -L /proc
/[1-9]*/task
/*/ns
/net
-samefile /run
/netns
/$netns | cut
-d/ -f5 | \
1487 x
=$
(ps
-w --no-headers -p $l);
1488 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1492 m
() { printf "%s\n" "$*"; "$@"; }
1496 m s nsenter
-t $
(pgrep
-f "/usr/sbin/openvpn --suppress-timestamps --nobind --config /etc/openvpn/client/client.conf") -n -m "$@"
1499 vpncmd gksudo
-u ian
"firefox -no-remote -P firefox-main-profile" &r
1506 trg
() { transmission-remote-gtk
&r
; }
1509 # local pid=$(cat /var/lib/transmission-daemon/transmission-daemon.pid)
1510 # if [[ $pid && -e /proc/$pid ]]; then
1511 # echo "noop. already running."
1515 # local NAME=transmission-daemon
1516 # local DAEMON=/usr/bin/$NAME
1517 # local duser=debian-transmission
1519 # [ -e /etc/default/$NAME ] && . /etc/default/$NAME
1520 # s ip netns exec vpn sudo -u $duser ionice -c 3 nice -n 19 $DAEMON $OPTIONS
1524 for x
in "$@"; do virsh destroy
"$x"; virsh undefine
"$x"; done
1531 s virsh dumpxml
$vm |
sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1532 sed -r "s/listen='[^']+/listen='$ip/"> $t
1533 s virsh undefine
$vm
1539 vm-set-listen
$1 0.0.0.0
1544 vm-set-listen
$1 127.0.0.1
1549 [[ $1 ]] ||
{ echo need arg
; return 1; }
1550 journalctl
--unit=openvpn-client@
$1 -f -n0 &
1551 s systemctl start openvpn-client@
$1
1555 [[ $1 ]] ||
{ echo need arg
; return 1; }
1556 s systemctl stop openvpn-client@
$1
1567 vspicy
() { # usage: VIRSH_DOMAIN
1568 # connect to vms made with virt-install
1569 spicy
-p $
(sudo virsh dumpxml
"$1"|
grep "<graphics.*type='spice'"|\
1570 sed -r "s/.*port='([0-9]+).*/\1/")
1574 wtr
() { curl wttr.in
/boston
; }
1577 # this succeeds even if gnome-screensaver isn't running.
1578 gnome-screensaver-command
--exit &>/dev
/null
1579 if ! pidof xscreensaver
; then
1581 # 1 was not long enough
1584 xscreensaver-command
-lock
1587 #############################
1588 ######### misc stuff ########
1589 #############################
1591 # from curl cheat.sh/:bash_completion
1592 _cheatsh_complete_curl
()
1595 _get_comp_words_by_ref
-n : cur
1598 #cur="${COMP_WORDS[COMP_CWORD]}"
1599 prev
="${COMP_WORDS[COMP_CWORD-1]}"
1600 opts
="$(curl -s cheat.sh/:list | sed s@^@cheat.sh/@)"
1602 if [[ ${cur} == cheat.sh
/* ]] ; then
1603 COMPREPLY
=( $
(compgen
-W "${opts}" -- ${cur}) )
1604 __ltrim_colon_completions
"$cur"
1608 complete
-F _cheatsh_complete_curl curl
1611 if [[ $
- == *i
* ]]; then
1612 # commands to run when bash exits normally
1617 # temporary variables to test colorization
1618 # some copied from gentoo /etc/bash/bashrc,
1620 # dircolors --print-database uses its own built-in database
1621 # instead of using /etc/DIR_COLORS. Try to use the external file
1622 # first to take advantage of user additions.
1623 safe_term
=${TERM//[^[:alnum:]]/?} # sanitize TERM
1625 [[ -f ~
/.dir_colors
]] && match_lhs
="${match_lhs}$(<~/.dir_colors)"
1626 [[ -f /etc
/DIR_COLORS
]] && match_lhs
="${match_lhs}$(</etc/DIR_COLORS)"
1627 [[ -z ${match_lhs} ]] \
1628 && type -P dircolors
>/dev
/null \
1629 && match_lhs
=$
(dircolors
--print-database)
1630 # test if our $TERM is in the TERM values in dircolor
1631 [[ $
'\n'${match_lhs} == *$
'\n'"TERM "${safe_term}* ]] && use_color
=true
1634 if ${use_color} && [[ $
- == *i
* ]]; then
1636 if [[ $XTERM_VERSION == Cygwin
* ]]; then
1640 underl
) echo -n $
'\E[4m' ;;
1641 bold
) echo -n $
'\E[1m' ;;
1642 red
) echo -n $
'\E[31m' ;;
1643 green
) echo -n $
'\E[32m' ;;
1644 blue
) echo -n $
'\E[34m' ;;
1645 cyan
) echo -n $
'\E[36m' ;;
1646 yellow
) echo -n $
'\E[33m' ;;
1647 purple
) echo -n $
'\E[35m' ;;
1648 nocolor
) echo -n $
'\E(B\E[m' ;;
1657 underl
) echo -n $
(tput smul
) ;;
1658 bold
) echo -n $
(tput bold
) ;;
1659 red
) echo -n $
(tput setaf
1) ;;
1660 green
) echo -n $
(tput setaf
2) ;;
1661 blue
) echo -n $
(tput setaf
4) ;;
1662 cyan
) echo -n $
(tput setaf
6) ;;
1663 yellow
) echo -n $
(tput setaf
3) ;;
1664 purple
) echo -n $
(tput setaf
5) ;;
1665 nocolor
) echo -n $
(tput sgr0
) ;; # no font attributes
1675 # Try to keep environment pollution down, EPA loves us.
1676 unset safe_term match_lhs use_color
1688 if [[ $
- == *i
* ]]; then
1689 # git branch/status prompt function
1690 if [[ $OS != Windows_NT
]]; then
1691 GIT_PS1_SHOWDIRTYSTATE
=true
1693 # arch source lopip show -fcation
1694 [[ -r /usr
/share
/git
/git-prompt.sh
]] && source /usr
/share
/git
/git-prompt.sh
1695 # fedora/debian source
1696 [[ -r /usr
/share
/git-core
/contrib
/completion
/git-prompt.sh
]] && source /usr
/share
/git-core
/contrib
/completion
/git-prompt.sh
1698 # in case we didn't source git-prompt.sh
1699 if ! declare -f __git_ps1
> /dev
/null
; then
1705 # this needs to come before next ps1 stuff
1706 # this stuff needs bash 4, feb 2009,
1707 # old enough to no longer condition on $BASH_VERSION anymore
1711 if [[ $
- == *i
* ]] && [[ ! $RLC_INSIDE_EMACS ]]; then
1713 bind -m vi-command B
:shell-backward-word
1714 bind -m vi-command W
:shell-forward-word
1717 if [[ $SSH_CLIENT ]]; then
1722 local return=$?
# this MUST COME FIRST
1723 local psc pst ps_char ps_color stale_subvol
1725 history -a # save history
1728 if [[ ! $DESKTOP_SESSION == xmonad
&& $TERM == *(screen
*|xterm
*|rxvt
*) ]]; then
1729 # from the screen man page
1730 if [[ $TERM == screen
* ]]; then
1731 local title_escape
="\033]..2;"
1733 local title_escape
="\033]0;"
1735 echo -ne "$title_escape${PWD/#$HOME/~} $USER@$HOSTNAME\007"
1740 0) ps_color
="$(get_term_color blue)"
1743 1) ps_color
="$(get_term_color green)"
1744 ps_char
="$return \\$"
1746 *) ps_color
="$(get_term_color yellow)"
1747 ps_char
="$return \\$"
1750 if [[ ! -O .
]]; then # not owner
1751 if [[ -w .
]]; then # writable
1752 ps_color
="$(get_term_color bold red)"
1754 ps_color
="$(get_term_color bold green)"
1757 # I would set nullglob, but bash has had bugs where that
1758 # doesn't work if not in top level.
1759 if [[ -e /nocow
/btrfs-stale
]] && ((`ls -AUq /nocow/btrfs-stale|wc -l`)); then
1760 ps_char
="! $ps_char"
1762 PS1
="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
1763 # emacs completion doesn't like the git prompt atm, so disabling it.
1764 #PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
1766 PROMPT_COMMAND
=prompt_command
1771 ###########################################
1772 # stuff that makes sense to be at the end #
1773 ###########################################
1774 if [[ "$SUDOD" ]]; then
1777 elif [[ -d /a
]] && [[ $PWD == $HOME ]] && [[ $
- == *i
* ]]; then
1786 # if someone exported $SOE, catch errors
1791 # I'd prefer to have system-wide, plus user ruby, due to bug in it
1792 # https://github.com/rubygems/rubygems/pull/1002
1793 # further problems: installing multi-user ruby and user ruby,
1794 # you don't get multi-user ruby when you sudo to root, unless its sudo -i.
1795 # There a third hybrid form, which passenger error suggested I use,
1796 # but it didn't actually work.
1798 # in cased I never need this
1799 # rvm for non-interactive shell: modified from https://rvm.io/rvm/basics
1800 #if [[ $(type -t rvm) == file && ! $(type -t ruby) ]]; then
1801 # source $(rvm 1.9.3 do rvm env --path)
1804 # based on warning from rvmsudo
1805 export rvmsudo_secure_path
=1
1807 if [[ -s "/usr/local/rvm/scripts/rvm" ]]; then
1808 source "/usr/local/rvm/scripts/rvm"
1809 elif [[ -s $HOME/.rvm
/scripts
/rvm
]]; then
1810 source $HOME/.rvm
/scripts
/rvm
1813 export GOPATH
=$HOME/go
1814 path_add
$GOPATH/bin
1817 path_add
--end ~
/.npm-global
1820 # didn't get drush working, if I did, this seems like the
1821 # only good thing to include for it.
1822 # Include Drush completion.
1823 # if [ -f "/home/ian/.drush/drush.complete.sh" ] ; then
1824 # source /home/ian/.drush/drush.complete.sh
1828 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
1829 # i added an extra condition as gentoo xorg guide says depending on
1830 # $DISPLAY is fragile.
1831 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell
&& isarch
; then
1836 # ensure no bad programs appending to this file will have an affect