minor improvements
[distro-setup] / .bashrc
diff --git a/.bashrc b/.bashrc
index 3dc94d42688bc01d40cbb2a3f6fb9c9de4fbc8b5..75a323231008485c9593a8a1257487f115ada86d 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -39,6 +39,7 @@
 # assume we want ssh commands to source this file if we are sourcing it,
 # and we haven't specified otherwise already
 [[ ! $BASH_LOGIN_SHELL ]] && export BASH_LOGIN_SHELL=true
+#BASH_LOGIN_SHELL=false # temporary override
 
 # first conditions show that we are an ssh command without an interactive shell
 if [[ $SSH_CONNECTION ]] \
@@ -80,7 +81,9 @@ unalias -a
 # use extra globing features.
 shopt -s extglob
 # include .files when globbing, but ignore files name . and ..
-# setting this also sets dotglob
+# setting this also sets dotglob.
+# Note, this doesn't work in bash 4.4 anymore, for paths with
+# more than 1 directory, like a/b/.foo, since * is fixed to not match /
 export GLOBIGNORE=*/.:*/..
 
 # broken with bash_completion package. Saw a bug for this once. Don't anymore.
@@ -110,15 +113,16 @@ shopt -s histappend
 shopt -s checkwinsize
 # attempt to save multiline single commands as single history entries.
 shopt -s cmdhist
+# enable **
 shopt -s globstar
 
 
 # inside emacs fixes
-if [[ $INSIDE_EMACS ]]; then
+if [[ $RLC_INSIDE_EMACS ]]; then
     # EMACS is used by bash on startup, but we don't need it anymore.
     # plus I hit a bug in a makefile which inherited it
     unset EMACS
-    export INSIDE_EMACS
+    export RLC_INSIDE_EMACS
     export PAGER=cat
     export MANPAGER=cat
     # scp completion does not work, but this doesn't fix it. todo, figure this out
@@ -151,7 +155,7 @@ fi
 
 if [[ $- == *i* ]]; then
     # for readline-complete.el
-    if [[ $INSIDE_EMACS ]]; then
+    if [[ $RLC_INSIDE_EMACS ]]; then
         # all for readline-complete.el
         stty echo
         bind 'set horizontal-scroll-mode on'
@@ -194,8 +198,8 @@ HISTTIMEFORMAT="%I:%M %p %m/%d "
 HISTCONTROL=ignoredups
 # works in addition to HISTCONTROL to do more flexible things
 # it could also do the same things as HISTCONTROL and thus replace it,
-# but meh
-HISTIGNORE='k **'
+# but meh. dunno why, but just " *" does glob expansion, so use [ ] to avoid it.
+HISTIGNORE='k *:[ ]*'
 
 export BC_LINE_LENGTH=0
 
@@ -209,16 +213,14 @@ C_DEFAULT_DIR=/a
 ###################
 ## include files ###
 ###################
-
-for _x in /a/bin/distro-functions/src/* /a/bin/*/*-function?(s); do
+for _x in /a/bin/distro-functions/src/* /a/bin/!(githtml)/*-function?(s); do
     source "$_x"
 done
 unset _x
 # so I can share my bashrc
-for x in /a/bin/bash_unpublished/*; do source $x; done
+for x in /a/bin/bash_unpublished/source-!(.#*); do source $x; done
 source $(dirname $(readlink -f $BASH_SOURCE))/path_add-function
 source /a/bin/log-quiet/logq-function
-source /a/bin/log-quiet/log-once-function
 path_add /a/exe
 path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
 # todo, these need to be renamed to be less generic.
@@ -226,6 +228,7 @@ path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-t
 #path_add $HOME/bin/bash-programs-by-ian/utils
 
 
+
 ###############
 ### aliases ###
 ###############
@@ -260,7 +263,6 @@ unalias ls ll grep &>/dev/null ||:
 
 
 
-
 #####################
 ###  functions   ####
 #####################
@@ -335,6 +337,13 @@ a() {
 
 ack() { ack-grep "$@"; }
 
+astudio() {
+    # googling android emulator libGL error: failed to load driver: r600
+    # lead to http://stackoverflow.com/a/36625175/14456
+    export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
+    /a/opt/android-studio/bin/studio.sh "$@" &r;
+}
+
 bashrcpush () {
     local startdir="$PWD"
     cd ~
@@ -343,7 +352,7 @@ bashrcpush () {
         tar cz bin/semi-private bin/distro-functions/src | ssh $x tar xz
     done
     cd $(mktemp -d)
-    command cp /a/c/repos/bash/!(.git) ~/.gitconfig .
+    command cp /a/c/repos/bash/!(.git|..|.) ~/.gitconfig .
     for x in "$@"; do
         tar cz * | ssh $x tar xz
     done
@@ -500,6 +509,11 @@ fa() {
     done < <(find "$@" -print0);
 }
 
+faf() { # find all files
+    find $@ -type f
+}
+
+fastboot() { /home/ian/Android/Sdk/platform-tools/fastboot "$@"; }
 
 ff() {
     if type -P firefox &>/dev/null; then
@@ -579,6 +593,9 @@ rename-test() {
     return 0
 }
 
+feh() {
+    command feh -FzZ "$@"
+}
 
 funce() {
     # like -e for functions. returns on error.
@@ -629,13 +646,32 @@ git_empty_branch() { # start an empty git branch. carefull, it deletes untracked
     git clean -fdx
 }
 
-gr() {
-    grep -iIP --color=auto "$@"
-}
+gitroot() {
+    local help="Usage: gitroot [--help]
+Print the full path to the root of the current git repo
 
+Handles being within a .git directory, unlike git rev-parse --show-toplevel,
+and works in older versions of git which did not have that."
+    if [[ $1 == --help ]]; then
+        echo "$help"
+        return
+    fi
+    local p=$(git rev-parse --git-dir) || { echo "error: not in a git repo" ; return 1; }
+    [[ $p != /* ]] && p=$PWD
+    echo "${p%%/.git}"
+}
 
+# quit will prompt if the program crashes.
+gmacs() { gdb -ex=r -ex=quit --args emacs "$@"; r; }
 
+gse() {
+    git send-email --notes '--envelope-sender=<ian@iankelling.org>' \
+        --suppress-cc=self "$@"
+}
 
+gr() {
+    grep -iIP --color=auto "$@"
+}
 
 grr() {
     if [[ ${#@} == 1 ]]; then
@@ -645,6 +681,18 @@ grr() {
     fi
 }
 
+hstatus() {
+    # do git status on published repos
+    cd /a/bin/githtml
+    for x in !(forks) forks/* ian-specific/*; do
+        cd `readlink -f $x`/..
+        hr
+        echo $x
+        i status
+        cd /a/bin/githtml
+    done
+}
+
 hl() { # history limit. Write extra history to archive file.
     # todo: this is not working or not used currently
     local max_lines linecount tempfile prune_lines x
@@ -673,6 +721,8 @@ hr() { # horizontal row. used to break up output
     echo
 }
 
+hrcat() { local f; for f; do [[ -f $f ]] || continue; hr; echo "$f"; cat "$f"; done }
+
 
 i() { git "$@"; }
 # modified from ~/local/bin/git-completion.bash
@@ -693,9 +743,10 @@ ic() {
 }
 
 
-ifn () {
+ifn() {
     # insensitive find
-    find -L . -iname "*$**" 2>/dev/null
+    find -L . -not \( -name .svn -prune -o -name .git -prune \
+         -o -name .hg -prune \) -iname "*$**" 2>/dev/null
 }
 
 
@@ -730,6 +781,10 @@ istext() {
     grep -Il "" "$@" &>/dev/null
 }
 
+jtail() {
+    journalctl -f "$@" | grep -Evi "^(\S+\s+){4}(sudo|ovpn|sshd|cron)"
+}
+
 
 l() {
     if [[ $PWD == /[iap] ]]; then
@@ -842,7 +897,7 @@ whatismyip() { pubip; }
 
 
 pwgen() {
-    apg -s -m 10 -x 14 -t
+    apg -m 12 -x 16 -t
 }
 
 
@@ -877,7 +932,7 @@ rlt() {
 }
 
 rlu() { # [OPTS] HOST PATH
-    # eg rlu -opts frodo testpath
+    # eg rlu -opts frodo /testpath
     # useful for selectively sending dirs which have been synced with unison,
     # where the path is the same on both hosts.
     opts=("${@:1:$#-2}") #  1 to last -2
@@ -891,7 +946,8 @@ rlu() { # [OPTS] HOST PATH
 }
 
 
-rspicy() { # HOST DOMAIN
+rspicy() { # usage: HOST DOMAIN
+    # connect to spice vm remote host. use vspicy for local host
     local port=$(ssh $1<<EOF
 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
        sed -rn "s/.*port='([0-9]+).*/\1/p"
@@ -942,6 +998,13 @@ sb() { # sudo bash -c
 }
 complete -F _root_command s sb
 
+scssl() {
+    # s gem install scss-lint
+    pushd /a/opt/thoughtbot-guides
+    git pull --stat
+    popd
+    scss-lint -c /a/opt/thoughtbot-guides/style/sass/.scss-lint.yml "$@"
+}
 
 ser() {
     local s; [[ $EUID != 0 ]] && s=sudo
@@ -1019,8 +1082,12 @@ t() {
     local x
     local -a args
     if type -t trash-put >/dev/null; then
-        # skip args that don't exist, or else it's an err
-        for x in "$@"; do [[ ! -e $x ]] || args+=("$x"); done
+        # skip args that don't exist, or else trash-put will have an error
+        for x in "$@"; do
+            if [[ -e $x || -L $x ]]; then
+                args+=("$x")
+            fi
+        done
         [[ ! ${args[@]} ]] || trash-put "${args[@]}"
     else
         rm -rf "$@"
@@ -1074,7 +1141,7 @@ te() {
 
 tm() {
     # timer in minutes
-    (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
+    (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3 --loop=no) > /dev/null 2>&1 &
 }
 
 ts() { # start editing a new file
@@ -1135,26 +1202,36 @@ vc() {
     # until I get it all wired up with systemd.
     newns vpn start
     pid=$(< /run/openvpn/client.pid)
-    if [[ ! $pid ]]; then
-        s ip netns exec vpn /usr/sbin/openvpn --daemon ovpn --config /etc/openvpn/client.conf --cd /etc/openvpn --writepid /run/openvpn/client.pid
-    elif [[ ! -e /proc/$pid ]]; then
-        echo "$0: ERROR: pidfile pid $pid is not a process!!!"
-        return 1
+    vpn_on=false
+    if [[ $pid ]]; then
+        if [[ -e /proc/$pid ]]; then
+            vpn_on=true
+        else
+            vpn_on=false
+            s rm -f /run/openvpn/client.pid
+        fi
     fi
+    $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
     gksudo -- ip netns exec vpn gksudo -u ${SUDO_USER:-$USER} "$@"
 }
 
 transmission() {
     vc transmission-gtk&
     i=0
-    while ((i < 10)); do
+    while true; do
+        if ((i > 10)); then
+            echo "$0: error: vpn tun0 didn't show up"
+            return 1
+        fi
         tun_ip=$(s ip netns exec vpn ip a show dev tun0 | sed -rn 's/^ *inet (10\.8\.\S+).*/\1/p')
         [[ ! $tun_ip ]] || break
         sleep 1
+        i=$((i + 1))
     done
     echo "$0: tun_ip=$tun_ip"
     [[ $tun_ip ]] || { e "$0: error: no tun0 addr found"; return 1; }
     ssh dopub bash <<EOF
+set -e
 rule="-A PREROUTING -i eth0 -p tcp -m tcp --dport 63324 -j DNAT --to-destination $tun_ip:63324"
 found=false
 while read -r line; do
@@ -1210,7 +1287,7 @@ vrm() {
 
 
 
-vspicy() {
+vspicy() { # usage: VIRSH_DOMAIN
     # connect to vms made with virt-install
     spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
                    sed -r "s/.*port='([0-9]+).*/\1/")
@@ -1324,7 +1401,7 @@ if [[ $- == *i* ]]; then
     shopt -s autocd
     shopt -s dirspell
     PS1='\w'
-    if [[ $- == *i* ]]  && [[ ! $INSIDE_EMACS ]]; then
+    if [[ $- == *i* ]]  && [[ ! $RLC_INSIDE_EMACS ]]; then
         PROMPT_DIRTRIM=2
        bind -m vi-command B:shell-backward-word
        bind -m vi-command W:shell-forward-word
@@ -1336,10 +1413,11 @@ if [[ $- == *i* ]]; then
 
     prompt_command() {
         local return=$? # this MUST COME FIRST
-        local psc pst
-        local ps_char ps_color
+        local psc pst ps_char ps_color stale_subvol
         unset IFS
         history -a # save history
+
+        # for titlebar
         if [[ ! $DESKTOP_SESSION == xmonad && $TERM == *(screen*|xterm*|rxvt*) ]]; then
             # from the screen man page
             if [[ $TERM == screen* ]]; then
@@ -1350,6 +1428,7 @@ if [[ $- == *i* ]]; then
            echo -ne "$title_escape${PWD/#$HOME/~}  $USER@$HOSTNAME\007"
        fi
 
+
         case $return in
            0) ps_color="$(get_term_color blue)"
                ps_char='\$'
@@ -1368,6 +1447,11 @@ if [[ $- == *i* ]]; then
                ps_color="$(get_term_color bold green)"
             fi
         fi
+        # I would set nullglob, but bash has had bugs where that
+        # doesn't work if not in top level.
+        if ((`ls -AUq /nocow/btrfs-stale|wc -l`)); then
+            ps_char="! $ps_char"
+        fi
         PS1="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
         # emacs completion doesn't like the git prompt atm, so disabling it.
         #PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
@@ -1377,8 +1461,6 @@ fi
 
 
 
-
-
 ###########################################
 # stuff that makes sense to be at the end #
 ###########################################
@@ -1428,11 +1510,22 @@ fi
 
 path_add --end ~/.npm-global
 
+
+# didn't get drush working, if I did, this seems like the
+# only good thing to include for it.
+# Include Drush completion.
+# if [ -f "/home/ian/.drush/drush.complete.sh" ] ; then
+#     source /home/ian/.drush/drush.complete.sh
+# fi
+
+
 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
 # i added an extra condition as gentoo xorg guide says depending on
 # $DISPLAY is fragile.
 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
     exec startx
 fi
+
+
 # ensure no bad programs appending to this file will have an affect
 return 0