.bashrc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #PROG=`basename "$0" | tr -d '\n'` # normal setting
  2. PROG="bashrc" # setting for bashrc due to errors
  3. function info() { echo ${PROG}\: info: "$@" 1>&2; }
  4. function warn() { echo ${PROG}\: warning: "$@" 1>&2; }
  5. function error() { echo ${PROG}\: error: "$@" 1>&2; }
  6. function debug() { [[ -v DEBUG ]] && echo ${PROG}\: debug: "$@" 1>&2 || true ; }
  7. function die() { echo ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
  8. export PS1="\# [\t] \u@\h \W/ $ "
  9. alias rm=' rm -i'
  10. alias ag=' alias | grep -i'
  11. alias eg=' printenv | grep -i'
  12. alias hg=' history | grep -i'
  13. alias ht=' history | tail'
  14. alias fpg=' find . -print | egrep -i'
  15. alias egi=' egrep -i'
  16. alias psg=' /bin/ps -auxww | grep'
  17. alias p8=' ping -c 3 8.8.8.8' # make sure routing works
  18. alias pp=' ping -c 3 port111.com' # make sure dns and routing work
  19. alias locate='locate -r'
  20. function dirl() {
  21. # "DIR"ectory "L"ist directory stack, one per line
  22. # Usage: dirl
  23. for d in `dirs`; do echo $d; done
  24. }
  25. function dirc() {
  26. # "DIR"ectory "C"onnect - connect to directory and list stack
  27. # Usage: dirc [DIR
  28. pushd ${1:-"$HOME"} > /dev/null
  29. dirl
  30. }
  31. function dirp () {
  32. # "DIR"ectory "P"op - pop N entries off the directory stack
  33. # Usage: dirp [N]
  34. #
  35. # OLD:
  36. # alias dirp='popd > /dev/null && dirl'
  37. for i in `seq ${1:-"1"}`; do
  38. debug "dirl: popd. i is $i"
  39. popd > /dev/null;
  40. done
  41. dirl
  42. }
  43. alias cd=pushd
  44. function gf() {
  45. # grep-find: grep for patterins in files via find
  46. #
  47. # Usage: gf patterns [files [days]]
  48. #
  49. # Examples:
  50. # gf findMeAnywhere
  51. # gf findMeInTextFiles '*.txt'
  52. # gf findMeInTextFiles .txt
  53. # gf BEGIN\|END .org 30
  54. local files=""
  55. local days="365"
  56. set -o noglob
  57. # First arg is pattern(s) for egrep
  58. if [ -z ${1+x} ]; then
  59. echo 'gf needs string(s) to search for ' 1>&2
  60. info "Usage: gf patterns [files [days]]"
  61. return 1
  62. fi
  63. # Second arg (if present) is files for find. No globbing, so "*.txt" OK
  64. if [ ! -z ${2+x} ]; then
  65. if [[ "$2" =~ ^\. ]]; then
  66. # Special case: treat ".foo" as "*.foo"
  67. # Avoids needing to quote on command line
  68. files="-name *$2"
  69. else
  70. files="-name ${2}"
  71. fi
  72. fi
  73. # $3 (if present) is find -mtime arg, default 365
  74. if [ ! -z ${3+x} ]; then
  75. days="${3}"
  76. fi
  77. # set -x
  78. find . -type f -mtime -${days} $files -exec egrep --color -H -i "${1}" \{\} \;
  79. # set +x
  80. set +o noglob
  81. }
  82. # Preserve history across sesssions
  83. #
  84. # http://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows
  85. #
  86. export HISTCONTROL=ignoredups:erasedups # no duplicate entries
  87. export HISTSIZE=100000 # big big history
  88. export HISTFILESIZE=100000 # big big history
  89. shopt -s histappend # append to history, don't overwrite it
  90. # Save and reload the history after each command finishes
  91. export PROMPT_COMMAND="history -a; history -c; history -r;"
  92. function hgt() {
  93. # hgt == "history grep (for arg) tail"
  94. #echo "Histroy Grep tail"
  95. if [ -z ${1+x} ]; then
  96. echo 'hgt needs an argument' 1>&2
  97. return 1
  98. fi
  99. history | grep -i "$1" | tail
  100. return 0
  101. }
  102. if [ -e ${HOME}/etc/hostname ]; then
  103. export HOSTNAME=`cat ${HOME}/etc/hostname`
  104. elif [ -e /etc/hostname ]; then
  105. export HOSTNAME=`cat /etc/hostname`
  106. else
  107. export HOSTNAME="unknown"
  108. fi
  109. # Set timezone if ~/bin/tz.sh exists
  110. # NEW, if neeeed?
  111. #
  112. # https://linuxize.com/post/how-to-set-or-change-timezone-in-linux/
  113. #
  114. # OLD:
  115. #
  116. # if [ -e ~/bin/tz.sh ]; then
  117. # echo Setting timezone.
  118. # source ~/bin/tz.sh # should be in ~/rc.local
  119. # fi
  120. # STILL NEEDED?
  121. #
  122. # Set local for numeric output
  123. LOCAL=`locale -a | grep -i en_us | head -1`
  124. if [[ "$LOCAL" != "" ]]; then export LC_NUMERIC="$LOCAL"; fi
  125. if [ -e ~/bin/sshagent ]; then
  126. source ~/bin/sshagent
  127. fi
  128. if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  129. alias 2clip='xclip -selection c'
  130. alias 3clip='printf %s "$(cat /dev/stdin)" | xclip -selection c' # no final \n
  131. elif [[ "$OSTYPE" == "darwin"* ]]; then
  132. alias 2clip='pbcopy'
  133. fi
  134. pathrm() {
  135. # remove an item from the path
  136. if [ -d "$1" ]; then
  137. removeThis="`echo $1 | sed -e 's#/#\\\/#'g`"
  138. newPath=`echo $PATH | awk -v RS=: -v ORS=: "/$removeThis/ {next} {print}" | sed 's/[ :]*$//g'`
  139. export PATH=$newPath
  140. fi
  141. }
  142. pathlast() {
  143. # add path to the end if not there
  144. if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  145. export PATH="${PATH:+"$PATH:"}$1"
  146. fi
  147. }
  148. pathfirst() {
  149. # add path to the front if not there
  150. if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  151. export PATH="$1:${PATH}"
  152. fi
  153. }
  154. path() {
  155. # show path
  156. echo $PATH
  157. }
  158. # show path, one entry per line
  159. alias pathcat="echo $PATH | sed 's/:/\n/g'"
  160. # Be sure we have a few specific paths if they exist
  161. pathlast $HOME/bin
  162. pathlast /usr/local/bin
  163. pathlast /opt/bin
  164. if [ -d ${HOME}/rc.local ]; then
  165. for rcfile in $(find ${HOME}/rc.local -name \*.sh); do
  166. debug running localrc ${rcfile}
  167. source ${rcfile}
  168. done
  169. fi
  170. alias emacs='setsid emacs'
  171. # from http://stuff-things.net/2014/12/16/working-with-emacsclient/
  172. if [ -z "$SSH_CONNECTION" ]; then
  173. export EMACSCLIENT=emacsclient
  174. alias ec="$EMACSCLIENT -c -n"
  175. export EDITOR="$EMACSCLIENT -c"
  176. export ALTERNATE_EDITOR=""
  177. else
  178. export EDITOR=$(type -P emacs || type -P ed)
  179. fi
  180. export VISUAL=$EDITOR
  181. # coloring for ls functions
  182. if [[ "$OSTYPE" == "linux-gnu" ]]; then
  183. color="--color";
  184. else
  185. color=""
  186. fi
  187. BIN_LS=/bin/ls
  188. alias ls=' ls '$color' -a'
  189. # Long List Reverse Tail
  190. function llrt() { ls -lrt $color ${*:-}; }
  191. # Long List Time
  192. function llt() { ls -lt $color ${*:-}; }
  193. # Long List Time, More
  194. function lltm() { ls -lt $color ${*:-} | more; }
  195. # Long List Time, Less
  196. function lltl() { ls -alt $color ${*:-} | more; }
  197. # Long List Time, Head
  198. function llth() { ls -lt $color ${*:-} | head; }
  199. # Long List Time, Tail
  200. function lltt() { ls -alt $color ${*:-} | tail; }
  201. # List Sort Size
  202. function lss() { ls -a1s $color ${*:-} | sort -n; }
  203. # List Sort Size Reverse
  204. function lssr() { ls -a1s $color ${*:-} | sort -nr; }
  205. function nf ()
  206. {
  207. # list the newest file in the current directory
  208. NF=`find ${1:-.} -maxdepth 1 -type f -print0 | xargs -0 ls -1t | head -1;`;
  209. echo ${NF:-/dev/null} | sed "s/ /\\\ /g"
  210. }
  211. # new file tail file
  212. function nftf { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail -f ; }
  213. # new file tail
  214. function nft { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail ; }
  215. # new file head
  216. function nfh { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs head ; }
  217. # new file less
  218. function nfl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs less ; }
  219. # new file cat
  220. function nfc { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs cat ; }
  221. # new file ls
  222. function nfls { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs ls -A1t ; }
  223. # new file ls -l
  224. function nflsl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs
  225. ls -Atl ; }
  226. if [[ ! -z "`which xdg-open`" ]]; then alias open='xdg-open '; fi
  227. touch $HOME/.bashrc-ran
  228. debug ".bashrc done"