fix bug which loses bash history
[distro-setup] / .bashrc
diff --git a/.bashrc b/.bashrc
index 99c311cbda9c45c78e2aba30735711ecc9005894..067782edfcf92bdada68956f5ba39960b099cdee 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -29,8 +29,7 @@ for x in $HOME/bin/bash-programs-by-ian/repos/*/*-function; do
     source "$x"
 done
 
-# so I can share my bashrc
-source $HOME/bin/bash_private
+source $HOME/bin/bash_private # so I can share my bashrc
 source $HOME/path_add-function
 
 
@@ -42,6 +41,10 @@ source $HOME/path_add-function
 
 CDPATH=.:/a
 
+# remove gnome keyring warning messages
+# there is probably a more proper way, but I didn't find any easily on google
+unset GNOME_KEYRING_CONTROL
+
 path_add /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
 
 #use extra globing features. See man bash, search extglob.
@@ -80,6 +83,8 @@ if [[ $- == *i* ]]; then
     if [[ $INSIDE_EMACS ]]; then
         bind 'set horizontal-scroll-mode on'
         bind 'set print-completions-horizontally on'
+        export PAGER=cat
+        export MANPAGER=cat
         stty echo
     else
         stty werase undef lnext undef stop undef start undef
@@ -105,12 +110,14 @@ PS4='$LINENO+ '
 HISTFILESIZE=
 # max commands 1 session can append to history
 HISTSIZE=100000
+# my history size limit based on lines
+HISTFILELINES=1000000
 # this needs to be different from the derault because
 # default HISTFILESIZE is 500 and could clobber our history
 HISTFILE=$HOME/.bh
 HISTTIMEFORMAT="%I:%M %p %m/%d "
-# duplicate, single letter, and space prepended commands do not go in history
-HISTIGNORE="&:?: *"
+# consecutive duplicate lines don't go in history
+HISTIGNORE="&"
 
 export BC_LINE_LENGTH=0
 
@@ -119,7 +126,7 @@ path_add $HOME/bin/bash-programs-by-ian/utils
 # note, if I use a machine I don't want files readable by all users, set
 # umask 077  # If fewer than 4 digits are entered, leading zeros are assumed
 
-
+C_DEFAULT_DIR=/a
 
 
 
@@ -196,15 +203,18 @@ t() {
 }
 
 
-grp() {
-    command grep --binary-files=without-match --color=auto "$@"
-}
+if type ack-grep >/dev/null 2>&1; then
+    alias ack=ack-grep
+fi
+
 
 gr() {
-    grep -i -r "$@"
+    grep -i --binary-files=without-match --color=auto "$@"
 }
 
-
+grr() {
+    grep -ri --binary-files=without-match --color=auto "$@"
+}
 
 
 
@@ -404,23 +414,34 @@ despace() {
 }
 
 # force symbolic link creation.
-# trash-put any existing targets,
-# then send all arguments to ln -s
+# trash-put any existing files where links would be created.
+# mkdir -p the directory containing the link(s) if needed.
+# then do ln -s -- "$@"
 lnf() {
+    if [[ $# -gt 2 && ! -d ${!#} ]]; then
+        mkdir -p "${!#}"
+    fi
     if [[ $# -gt 1 && -d ${!#} ]]; then
         local oldcwd=$PWD
         cd ${!#} # last arg
         for x in "${@:1:$(($#-1))}"; do # all but last arg
-            # a broken symlink will fail the "exists" -e test
-            [[ -e "${x##*/}" || -L "${x##*/}" ]] && trash-put "${x##*/}"
+            # remove any trailing slashes
+            x="${x%%+(/)}"
+            # remove any leading directory components
+            x="${x##*/}"
+            te "$x" && trash-put "$x"
         done
         cd "$oldcwd"
     elif [[ $# -eq 2 ]]; then
-        [[ -e "$2" || -L "$2" ]] && rm "$2"
+        if te "$2"; then
+            trash-put "$2"
+        elif [[ ! -d $(getdir "$2") ]]; then
+            mkdir -p $(getdir "$2")
+        fi
     else
-        [[ -e "${1##*/}" || -L "${1##*/}" ]] && rm "${1##*/}"
+        te "${1##*/}" && rm "${1##*/}"
     fi
-    ln -s "$@"
+    ln -s -- "$@"
 }
 
 
@@ -476,7 +497,14 @@ else
 fi
 
 
-
+# test existence
+te() {
+    local ret=0
+    for x in "$@"; do
+        [[ -e "$x" || -L "$x" ]] || ret=1
+    done
+    return $ret
+}
 
 
 # fix root file ownership for FILE argument.
@@ -488,7 +516,7 @@ perm_fix() {
        [[ -e $1 ]] || touch $1
        if [[ $(stat -c "%u" "$1") == 0 ]] ; then
 
-           argdir=$(dirstrip "$1")
+           argdir=$(getdir "$1")
            if [[ $(stat -c "%u" "$argdir") != 0 ]] ; then
                if ! chown "--reference=$argdir" "$1"; then
                    echo failed to fix bad ownership file permissons
@@ -506,11 +534,6 @@ pfind() { #find *$1* in $PATH
     find "${pathArray[@]}" -iname "*$1*"
 }
 
-pstree() {
-    ps -ejH "$@"
-}
-
-
 
 pwd() { # do pwd + some other info.
     echo "$(ll -d "$PWD")              $USER@$HOSTNAME         $(date +%r)"
@@ -685,18 +708,19 @@ complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
 
 
 
-
 hl() { # history limit. Write extra history to archive file.
     local max_lines linecount tempfile
-    if ! [[ -w $HISTFILE ]] || ! [[ -w ${HISTFILE}_archive ]]; then
-       echo "error: a history file is not writable."
-       return 1
-    fi
+    for x in $HISTFILE ${HISTFILE}_archive; do
+        if [[ -e $x && ! -w $x ]]; then
+            echo "error: history file $x is not writable."
+           return 1
+        fi
+    done
     history -w
     if [[ $1 ]]; then
        max_lines=$(($1 * 2)) # 2 lines for every history command
     else
-       max_lines=1000000
+       max_lines=$HISTFILELINES
     fi
     linecount=$(wc -l < $HISTFILE)
     linecount=${linecount:-0}
@@ -705,18 +729,14 @@ hl() { # history limit. Write extra history to archive file.
        tempfile=$(mktemp)
        [[ $tempfile ]] || { echo mktemp failed; return 1; }
        head -$prune_lines $HISTFILE >> ${HISTFILE}a \
-           && sed -e "1,${prune_lines}d"  $HISTFILE > $tempfile \
-           && mv $tempfile $HISTFILE
+           && sed -ie "1,${prune_lines}d"  $HISTFILE
     fi
     perm_fix $HISTFILE
     perm_fix ${HISTFILE}_archive
-    history -c
-    history -r
-    history
 }
-# run hl when bash exits normally
-trap hl EXIT
 
+# commands to run when bash exits normally
+trap "hl; smh" EXIT
 
 
 # temporary variables to test colorization