.bashrc.pre-org 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. # #+title: .bashrc
  2. # #+date: <2020-11-29 10:51:24 Sunday>
  3. # #+author: George M Jones
  4. # #+email: gmj@pobox.com
  5. # #+options: ':nil *:t -:t ::t <:t H:3 \n:nil ^:nil arch:headline
  6. # #+options: author:t broken-links:nil c:nil creator:nil
  7. # #+options: d:(not "LOGBOOK") date:t e:t email:nil f:t inline:t num:2
  8. # #+options: p:nil pri:nil prop:nil stat:t tags:t tasks:t tex:t
  9. # #+options: timestamp:t title:t toc:t todo:t |:t
  10. # #+language: en
  11. # #+select_tags: export
  12. # #+exclude_tags: noexport
  13. # #+creator: Emacs 28.0.50 (Org mode 9.4)
  14. #
  15. #
  16. # * About
  17. # ** Intro
  18. # This is George Jones' .bashrc file as an literate programming file in
  19. # emacs org mode using babel blocks.
  20. #
  21. # ** To generate the actual .bashrc
  22. # This .bashrc.org file must be process to generate the actual .bashrc
  23. #
  24. # It can be processed interactively to generate .bashrc via
  25. # org-babel-tangle-file or from the command line as
  26. #
  27. # #+begin_example
  28. # emacs --batch --eval "(require 'org)" --eval '(org-babel-tangle-file ".bashrc.org")'
  29. # #+end_example
  30. #
  31. # Permanent changes must be made to the .org version, as the actual
  32. # .bashrc will be overwritten when the .org version is "compiled"
  33. #
  34. # ** Debugging
  35. #
  36. # In most bash files I do
  37. #
  38. # #+begin_example
  39. # set -e
  40. # set -u
  41. # #+end_example
  42. #
  43. # but there are problems setting it in .bashrc. An error then causes
  44. # you to exit the shell entirely (not what you want), and there are
  45. # several constructs that cause warnings due to undefined variables
  46. # (these can/probably should be fixd)
  47. #
  48. # Set
  49. #
  50. # #+begin_example
  51. # export DEBUG=1
  52. # #+end_example
  53. #
  54. # to enable debugging output from the debug helper function.
  55. #
  56. # * .bashrc
  57. # ** Helper functions
  58. # I define a few syslog-ish helper functions to print warnings,
  59. # errors, etc.
  60. #
  61. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  62. # #PROG=`basename "$0" | tr -d '\n'` # normal setting
  63. # PROG="bashrc" # setting for bashrc due to errors
  64. #
  65. # function info() { echo ${PROG}\: info: "$@" 1>&2; }
  66. # function warn() { echo ${PROG}\: warning: "$@" 1>&2; }
  67. # function error() { echo ${PROG}\: error: "$@" 1>&2; }
  68. # function debug() { [[ -v DEBUG ]] && echo ${PROG}\: debug: "$@" 1>&2 || true ; }
  69. # function die() { echo ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
  70. # #+end_src
  71. #
  72. # ** Set a reasonable default prompt
  73. #
  74. # Here I set a reasonable default prompt that includes timestamp,
  75. # username, host and current directory:
  76. #
  77. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  78. # export PS1="\# [\t] \u@\h \W/ $ "
  79. # #+end_src
  80. #
  81. # ** Misc aliases
  82. #
  83. # Define various aliases that I use
  84. #
  85. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  86. # alias rm=' rm -i'
  87. # alias ag=' alias | grep -i'
  88. # alias eg=' printenv | grep -i'
  89. # alias hg=' history | grep -i'
  90. # alias ht=' history | tail'
  91. # alias fpg=' find . -print | egrep -i'
  92. # alias egi=' egrep -i'
  93. # alias psg=' /bin/ps -auxww | grep'
  94. # alias p8=' ping -c 3 8.8.8.8' # make sure routing works
  95. # alias pp=' ping -c 3 port111.com' # make sure dns and routing work
  96. # alias locate='locate -r'
  97. # #+end_src
  98. #
  99. # ** cd commands that use/print the directory stack
  100. #
  101. # These aliases support pushd/popd/dirs like functionality while
  102. # listing one directory per line
  103. #
  104. # I like to keep a "stack" of directories so I can work on one thing
  105. # then "pop" back to where I was. =pushd= an =popd= support this,
  106. # and =dirs= lists the directories, but I prefer to have them listed
  107. # one per line.
  108. #
  109. #
  110. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  111. # function dirl() {
  112. # # "DIR"ectory "L"ist directory stack, one per line
  113. # # Usage: dirl
  114. #
  115. # for d in `dirs`; do echo $d; done
  116. # }
  117. #
  118. # function dirc() {
  119. # # "DIR"ectory "C"onnect - connect to directory and list stack
  120. # # Usage: dirc [DIR
  121. #
  122. # pushd ${1:-"$HOME"} > /dev/null
  123. # dirl
  124. # }
  125. #
  126. # function dirp () {
  127. # # "DIR"ectory "P"op - pop N entries off the directory stack
  128. # # Usage: dirp [N]
  129. # #
  130. # # OLD:
  131. # # alias dirp='popd > /dev/null && dirl'
  132. # for i in `seq ${1:-"1"}`; do
  133. # debug "dirl: popd. i is $i"
  134. # popd > /dev/null;
  135. # done
  136. # dirl
  137. # }
  138. #
  139. # alias cd=pushd
  140. # #+end_src
  141. #
  142. # ** Misc functions
  143. #
  144. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  145. #
  146. # function gf() {
  147. # # grep-find: grep for patterins in files via find
  148. # #
  149. # # Usage: gf patterns [files [days]]
  150. # #
  151. # # Examples:
  152. # # gf findMeAnywhere
  153. # # gf findMeInTextFiles '*.txt'
  154. # # gf findMeInTextFiles .txt
  155. # # gf BEGIN\|END .org 30
  156. #
  157. # local files=""
  158. # local days="365"
  159. #
  160. # set -o noglob
  161. #
  162. # # First arg is pattern(s) for egrep
  163. # if [ -z ${1+x} ]; then
  164. # echo 'gf needs string(s) to search for ' 1>&2
  165. # info "Usage: gf patterns [files [days]]"
  166. # return 1
  167. # fi
  168. #
  169. # # Second arg (if present) is files for find. No globbing, so "*.txt" OK
  170. # if [ ! -z ${2+x} ]; then
  171. # if [[ "$2" =~ ^\. ]]; then
  172. # # Special case: treat ".foo" as "*.foo"
  173. # # Avoids needing to quote on command line
  174. # files="-name *$2"
  175. # else
  176. # files="-name ${2}"
  177. # fi
  178. # fi
  179. #
  180. # # $3 (if present) is find -mtime arg, default 365
  181. # if [ ! -z ${3+x} ]; then
  182. # days="${3}"
  183. # fi
  184. #
  185. # # set -x
  186. # find . -type f -mtime -${days} $files -exec egrep --color -H -i "${1}" \{\} \;
  187. # # set +x
  188. #
  189. # set +o noglob
  190. # }
  191. # #+end_src
  192. #
  193. # ** Bash history functions and settings
  194. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  195. #
  196. # # Preserve history across sesssions
  197. # #
  198. # # http://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows
  199. # #
  200. # export HISTCONTROL=ignoredups:erasedups # no duplicate entries
  201. # export HISTSIZE=100000 # big big history
  202. # export HISTFILESIZE=100000 # big big history
  203. # shopt -s histappend # append to history, don't overwrite it
  204. #
  205. # # Save and reload the history after each command finishes
  206. # export PROMPT_COMMAND="history -a; history -c; history -r;"
  207. #
  208. #
  209. # function hgt() {
  210. # # hgt == "history grep (for arg) tail"
  211. # #echo "Histroy Grep tail"
  212. #
  213. # if [ -z ${1+x} ]; then
  214. # echo 'hgt needs an argument' 1>&2
  215. # return 1
  216. # fi
  217. #
  218. # history | grep -i "$1" | tail
  219. # return 0
  220. # }
  221. # #+end_src
  222. #
  223. # ** Set the hostnane, timezone and local
  224. # Set HOSTNAME if ~/etc/hostname exists
  225. #
  226. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  227. # if [ -e ${HOME}/etc/hostname ]; then
  228. # export HOSTNAME=`cat ${HOME}/etc/hostname`
  229. # elif [ -e /etc/hostname ]; then
  230. # export HOSTNAME=`cat /etc/hostname`
  231. # else
  232. # export HOSTNAME="unknown"
  233. # fi
  234. #
  235. # # Set timezone if ~/bin/tz.sh exists
  236. #
  237. # # NEW, if neeeed?
  238. # #
  239. # # https://linuxize.com/post/how-to-set-or-change-timezone-in-linux/
  240. # #
  241. # # OLD:
  242. # #
  243. # # if [ -e ~/bin/tz.sh ]; then
  244. # # echo Setting timezone.
  245. # # source ~/bin/tz.sh # should be in ~/rc.local
  246. # # fi
  247. #
  248. # # STILL NEEDED?
  249. # #
  250. # # Set local for numeric output
  251. # LOCAL=`locale -a | grep -i en_us | head -1`
  252. # if [[ "$LOCAL" != "" ]]; then export LC_NUMERIC="$LOCAL"; fi
  253. # #+end_src
  254. #
  255. # ** Set up ssh agent
  256. # Add keys by hand if needed via
  257. #
  258. # #+begin_example
  259. # ssh-add ~/.ssh/id_*
  260. # #+end_example
  261. #
  262. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  263. # if [ -e ~/bin/sshagent ]; then
  264. # source ~/bin/sshagent
  265. # fi
  266. # #+end_src
  267. #
  268. # ** Copy stdin to clipboard
  269. #
  270. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  271. # if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  272. # alias 2clip='xclip -selection c'
  273. # alias 3clip='printf %s "$(cat /dev/stdin)" | xclip -selection c' # no final \n
  274. # elif [[ "$OSTYPE" == "darwin"* ]]; then
  275. # alias 2clip='pbcopy'
  276. # fi
  277. # #+end_src
  278. #
  279. # ** Path functions
  280. # These path* functions add and remove elements to PATH.
  281. # They insure that entries are unique.
  282. # They allow you to place a path first or last in the order (e.g.
  283. # so that ~/bin comes before /usr/local/bin)
  284. #
  285. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  286. # pathrm() {
  287. # # remove an item from the path
  288. # if [ -d "$1" ]; then
  289. # removeThis="`echo $1 | sed -e 's#/#\\\/#'g`"
  290. # newPath=`echo $PATH | awk -v RS=: -v ORS=: "/$removeThis/ {next} {print}" | sed 's/[ :]*$//g'`
  291. # export PATH=$newPath
  292. # fi
  293. # }
  294. #
  295. #
  296. # pathlast() {
  297. # # add path to the end if not there
  298. # if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  299. # export PATH="${PATH:+"$PATH:"}$1"
  300. # fi
  301. # }
  302. #
  303. #
  304. # pathfirst() {
  305. # # add path to the front if not there
  306. # if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  307. # export PATH="$1:${PATH}"
  308. # fi
  309. # }
  310. #
  311. # path() {
  312. # # show path
  313. # echo $PATH
  314. # }
  315. #
  316. # # show path, one entry per line
  317. # alias pathcat="echo $PATH | sed 's/:/\n/g'"
  318. #
  319. #
  320. # # Be sure we have a few specific paths if they exist
  321. #
  322. # pathlast $HOME/bin
  323. # pathlast /usr/local/bin
  324. # pathlast /opt/bin
  325. # #+end_src
  326. #
  327. # ** source ~/rc.local/*.sh to do non-general bash setup
  328. # Execute any .sh files in ~/rc.local/*.sh
  329. #
  330. # This allows me to split out setup for aliases and commands that
  331. # only get used on certian systems or in certian contexts (git, go,
  332. # mail, blog..)
  333. #
  334. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  335. #
  336. # if [ -d ${HOME}/rc.local ]; then
  337. # for rcfile in $(find ${HOME}/rc.local -name \*.sh); do
  338. # debug running localrc ${rcfile}
  339. # source ${rcfile}
  340. # done
  341. # fi
  342. # #+end_src
  343. #
  344. # ** Invoking emacs
  345. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  346. # alias emacs='setsid emacs'
  347. #
  348. # # from http://stuff-things.net/2014/12/16/working-with-emacsclient/
  349. #
  350. # if [ -z "$SSH_CONNECTION" ]; then
  351. # export EMACSCLIENT=emacsclient
  352. # alias ec="$EMACSCLIENT -c -n"
  353. # export EDITOR="$EMACSCLIENT -c"
  354. # export ALTERNATE_EDITOR=""
  355. # else
  356. # export EDITOR=$(type -P emacs || type -P ed)
  357. # fi
  358. # export VISUAL=$EDITOR
  359. # #+end_src
  360. #
  361. # ** ls aliases
  362. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  363. #
  364. # # coloring for ls functions
  365. #
  366. # if [[ "$OSTYPE" == "linux-gnu" ]]; then
  367. # color="--color";
  368. # else
  369. # color=""
  370. # fi
  371. #
  372. # BIN_LS=/bin/ls
  373. # alias ls=' ls '$color' -a'
  374. #
  375. #
  376. # # Long List Reverse Tail
  377. # function llrt() { ls -lrt $color ${*:-}; }
  378. #
  379. # # Long List Time
  380. # function llt() { ls -lt $color ${*:-}; }
  381. #
  382. # # Long List Time, More
  383. # function lltm() { ls -lt $color ${*:-} | more; }
  384. #
  385. # # Long List Time, Less
  386. # function lltl() { ls -alt $color ${*:-} | more; }
  387. #
  388. # # Long List Time, Head
  389. # function llth() { ls -lt $color ${*:-} | head; }
  390. #
  391. # # Long List Time, Tail
  392. # function lltt() { ls -alt $color ${*:-} | tail; }
  393. #
  394. #
  395. # # List Sort Size
  396. # function lss() { ls -a1s $color ${*:-} | sort -n; }
  397. #
  398. # # List Sort Size Reverse
  399. # function lssr() { ls -a1s $color ${*:-} | sort -nr; }
  400. #
  401. # #+end_src
  402. #
  403. # ** Aliases for viewing the newest file in a directoy
  404. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  405. #
  406. # function nf ()
  407. # {
  408. # # list the newest file in the current directory
  409. # NF=`find ${1:-.} -maxdepth 1 -type f -print0 | xargs -0 ls -1t | head -1;`;
  410. # echo ${NF:-/dev/null} | sed "s/ /\\\ /g"
  411. # }
  412. #
  413. #
  414. # # new file tail file
  415. # function nftf { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail -f ; }
  416. #
  417. # # new file tail
  418. # function nft { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail ; }
  419. #
  420. # # new file head
  421. # function nfh { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs head ; }
  422. #
  423. # # new file less
  424. # function nfl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs less ; }
  425. #
  426. # # new file cat
  427. # function nfc { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs cat ; }
  428. #
  429. # # new file ls
  430. # function nfls { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs ls -A1t ; }
  431. #
  432. # # new file ls -l
  433. # function nflsl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs
  434. # ls -Atl ; }
  435. # #+end_src
  436. #
  437. # ** viewing files
  438. # Notes on setting up file/mime type assiciatons
  439. # https://unix.stackexchange.com/questions/77136/xdg-open-default-applications-behavior
  440. #
  441. # So...
  442. #
  443. # locate -r 'emacs.*\.desktop'
  444. # xdg-mime default emacs.desktop text/plain
  445. #
  446. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  447. # if [[ ! -z "`which xdg-open`" ]]; then alias open='xdg-open '; fi
  448. # #+end_src
  449. #
  450. # ** All done
  451. # #+begin_src shell :tangle .bashrc.sh :noweb no-export
  452. # touch $HOME/.bashrc-ran
  453. # debug ".bashrc done"
  454. # #+end_src