.bashrc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # This is George Jone's .bashrc file
  2. #
  3. # These should be set but sometimes cause problems (shell exiting)
  4. # set -e
  5. # set -u
  6. # Don't exit out of login shell on error
  7. # set +e
  8. # Set to debug
  9. # export DEBUG=1
  10. export BASHRCRAN="``"
  11. #PROG=`basename "$0" | tr -d '\n'` # normal setting
  12. PROG="bashrc" # setting for bashrc due to errors
  13. function info() { echo ${PROG}\: info: "$@" 1>&2; }
  14. function warn() { echo ${PROG}\: warning: "$@" 1>&2; }
  15. function error() { echo ${PROG}\: error: "$@" 1>&2; }
  16. function debug() { [[ -v DEBUG ]] && echo ${PROG}\: debug: "$@" 1>&2 || true ; }
  17. function die() { echo ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
  18. function set_xtitle {
  19. # set X window title
  20. #
  21. # Usage:
  22. #
  23. # set_xtitle [name]
  24. WINDOW_NAME="${1:-`basename $PWD`}"
  25. if [[ -v INSIDE_EMACS ]]; then
  26. PROMPT_COMMAND="echo -ne ${WINDOW_NAME} $USER@$HOSTNAME:$PWD";
  27. else
  28. PROMPT_COMMAND="echo -ne \"\033]0;${WINDOW_NAME} $USER@$HOSTNAME:$PWD\007\"";
  29. fi
  30. }
  31. export PS1="\# [\t] \u@\h \W/ $ "
  32. #
  33. # Aliases from ages past
  34. #
  35. # shell related
  36. alias ag=' alias | grep -i'
  37. alias eg=' printenv | grep -i'
  38. alias hg=' history | grep -i'
  39. alias ht=' history | tail'
  40. alias egi=' egrep -i'
  41. # File related
  42. alias rm=' rm -i'
  43. alias locate='locate -r'
  44. alias fpg=' find . -print | egrep -i'
  45. # Process related
  46. alias psg=' /bin/ps -auxww | grep'
  47. # Network related
  48. alias listening='sudo lsof -i | grep LISTEN'
  49. alias ips='ifconfig | grep inet'
  50. alias p8=' ping -c 3 8.8.8.8' # make sure routing works
  51. alias pp=' ping -c 3 port111.com' # make sure dns and routing work
  52. # Disk related
  53. alias df="df -H" # commands mounting loopback filesystess?
  54. # utilities/statistics
  55. alias count='sort | uniq -c | sort -nr' # count unique things on stdin
  56. # Newer aliases
  57. # json pretty prett print
  58. alias jpp="python -m json.tool"
  59. alias h=head
  60. #
  61. # Shell-related functions
  62. #
  63. #
  64. # Directory stack functions
  65. #
  66. function dirl() {
  67. # "DIR"ectory "L"ist directory stack, one per line
  68. # Usage: dirl
  69. for d in `dirs`; do echo $d; done
  70. }
  71. function dirc() {
  72. # "DIR"ectory "C"onnect - connect to directory and list stack
  73. # Usage: dirc [DIR
  74. pushd ${1:-"$HOME"} > /dev/null
  75. dirl
  76. }
  77. function dirp () {
  78. # "DIR"ectory "P"op - pop N entries off the directory stack
  79. # Usage: dirp [N]
  80. #
  81. # OLD:
  82. # alias dirp='popd > /dev/null && dirl'
  83. for i in `seq ${1:-"1"}`; do
  84. debug "dirl: popd. i is $i"
  85. popd > /dev/null;
  86. done
  87. dirl
  88. }
  89. source $HOME/lib/bash/bashutils.sh # error,warning,...
  90. indirs ()
  91. # return "true" if there is a directory in the stack that contains "$1"
  92. (
  93. CONTAINS="${1:-NONE}"
  94. if [ "$CONTAINS" == "NONE" ]; then
  95. warn "no directory name given for indirs"
  96. return $FALSE
  97. fi
  98. for d in `dirs`;
  99. do
  100. if [[ "$d" == *"${CONTAINS}"* ]]; then
  101. return $TRUE
  102. fi
  103. done
  104. info ${CONTAINS} not found in dirs
  105. return $FALSE
  106. )
  107. p2 ()
  108. # pop to (p2) a directory in the directory stack that contains P2THIS
  109. {
  110. P2THIS="${1:-NONE}"
  111. if [ "$P2THIS" == "NONE" ]; then
  112. warn "no directory name given for p2"
  113. return $FALSE
  114. fi
  115. if indirs $P2THIS; then
  116. # pop until $PWD ends with $P2THIS
  117. while true;
  118. do
  119. if [[ "$PWD" == *"${P2THIS}"* ]]; then
  120. dirs
  121. return $TRUE
  122. else
  123. popd 1> /dev/null || return $FALSE
  124. fi
  125. done
  126. else
  127. echo $INDIRS not found in `dirs`
  128. fi
  129. }
  130. alias cd=pushd
  131. # Preserve history across sesssions
  132. #
  133. # http://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows
  134. #
  135. export HISTCONTROL=ignoredups:erasedups # no duplicate entries
  136. export HISTSIZE=100000 # big big history
  137. export HISTFILESIZE=100000 # big big history
  138. shopt -s histappend # append to history, don't overwrite it
  139. # Save and reload the history after each command finishes
  140. if [[ -v INSIDE_EMACS ]]; then
  141. # no escape characters
  142. export PROMPT_COMMAND="history -a; history -c; history -r;echo -ne \`whoami\`@\`hostname -s\`:"
  143. elif [[ -v DISPLAY ]]; then
  144. # in X-land, set window title to user@host:pwd
  145. export PROMPT_COMMAND="history -a; history -c; history -r;echo -ne \"\033]0;\`whoami\`@\`hostname -s\`:\`pwd\`\007\""
  146. else
  147. export PROMPT_COMMAND="history -a; history -c; history -r;echo -ne \"\033]0;$USER@$HOSTNAME:$PWD\007\""
  148. fi
  149. # for working in emacs shell
  150. if [[ "$TERM" == "dumb" ]]; then
  151. # No terminal escape characters for color, etc.
  152. # only include final element of pwd
  153. export PROMPT_COMMAND='echo -ne ${WINDOW_NAME} $USER@$HOSTNAME:`pwd | sed s#\.\*/##g`'
  154. export PS1=" \u@\$(prompt-hostname) \W $(git-branch-prompt) \$ "
  155. fi
  156. # modern GPG setup
  157. # https://stackoverflow.com/questions/17769831/how-to-make-gpg-prompt-for-passphrase-on-cli
  158. #
  159. GPG_TTY=$(tty)
  160. export GPG_TTY
  161. function hgt() {
  162. # hgt == "history grep (for arg) tail"
  163. #echo "Histroy Grep tail"
  164. if [ -z ${1+x} ]; then
  165. echo 'hgt needs an argument' 1>&2
  166. return 1
  167. fi
  168. history | grep -i "$1" | tail
  169. return 0
  170. }
  171. #
  172. # set hostname
  173. #
  174. if [ -e ${HOME}/etc/hostname ]; then
  175. export HOSTNAME=`cat ${HOME}/etc/hostname`
  176. elif [ -e /etc/hostname ]; then
  177. export HOSTNAME=`cat /etc/hostname`
  178. else
  179. export HOSTNAME="unknown"
  180. fi
  181. # Set timezone if ~/bin/tz.sh exists
  182. # NEW, if neeeed?
  183. #
  184. # https://linuxize.com/post/how-to-set-or-change-timezone-in-linux/
  185. #
  186. # OLD:
  187. #
  188. # if [ -e ~/bin/tz.sh ]; then
  189. # echo Setting timezone.
  190. # source ~/bin/tz.sh # should be in ~/rc.local
  191. # fi
  192. # STILL NEEDED?
  193. #
  194. # Set local for numeric output
  195. LOCAL=`locale -a | grep -i en_us | head -1`
  196. if [[ "$LOCAL" != "" ]]; then export LC_NUMERIC="$LOCAL"; fi
  197. #
  198. # Start SSH agent
  199. #
  200. if [ -e ~/bin/sshagent ]; then
  201. source ~/bin/sshagent
  202. fi
  203. #
  204. # Copy to clipbloard/paste buffer without using the mouse
  205. #
  206. if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  207. alias 2clip='xclip -rmlastnl -selection c'
  208. #alias 3clip='printf %s "$(cat /dev/stdin)" | xclip -selection c' # no final \n
  209. elif [[ "$OSTYPE" == "darwin"* ]]; then
  210. alias 2clip='pbcopy'
  211. fi
  212. #
  213. # Path aliases
  214. #
  215. pathrm() {
  216. # remove an item from the path
  217. if [ -d "$1" ]; then
  218. removeThis="`echo $1 | sed -e 's#/#\\\/#'g`"
  219. newPath=`echo $PATH | awk -v RS=: -v ORS=: "/$removeThis/ {next} {print}" | sed 's/[ :]*$//g'`
  220. export PATH=$newPath
  221. fi
  222. }
  223. pathlast() {
  224. # add path to the end if not there
  225. if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  226. export PATH="${PATH:+"$PATH:"}$1"
  227. fi
  228. }
  229. pathfirst() {
  230. # add path to the front if not there
  231. if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
  232. export PATH="$1:${PATH}"
  233. fi
  234. }
  235. path() {
  236. # show path
  237. echo $PATH
  238. }
  239. # show path, one entry per line
  240. alias pathcat="echo $PATH | sed 's/:/\n/g'"
  241. # Be sure we have a few specific paths if they exist
  242. pathrm $HOME/bin
  243. pathfirst $HOME/bin
  244. pathfirst $HOME/.local/bin
  245. pathlast /usr/local/bin
  246. pathlast /opt/bin
  247. #
  248. # Source scripts in ~/rc.local/*.sh
  249. #
  250. # These are for setup in different environments
  251. # .e.g. I only set up email on my laptop.
  252. #
  253. for rcfile in ${HOME}/rc.local/*.sh; do
  254. if [ -e "$rcfile" ]; then
  255. debug running localrc ${rcfile}
  256. source ${rcfile}
  257. fi
  258. done
  259. #
  260. # Emacs setup
  261. #
  262. # TODO: move to ~/rc.local
  263. # + I (sadly) don't urn emacs everwhere
  264. #
  265. alias emacs='setsid emacs'
  266. # from http://stuff-things.net/2014/12/16/working-with-emacsclient/
  267. if [[ -v SSH_CONNECTION ]]; then
  268. alias ec="emacsclient -c"
  269. export EDITOR=$(type -P emacs || type -P ed)
  270. else
  271. export EMACSCLIENT=emacsclient
  272. alias ec="$EMACSCLIENT -c -n"
  273. export EDITOR="$EMACSCLIENT -c"
  274. export ALTERNATE_EDITOR=""
  275. fi
  276. export VISUAL=$EDITOR
  277. #
  278. # Various ls functions
  279. #
  280. # coloring for ls functions
  281. if [[ "$OSTYPE" == "linux-gnu" ]]; then
  282. color="--color";
  283. else
  284. color=""
  285. fi
  286. BIN_LS=/bin/ls
  287. alias ls=' ls '$color' -a'
  288. #
  289. # ls aliases with "long", for humans
  290. #
  291. # Long List Reverse Tail
  292. function llrt() { ls -Alrt $color ${*:-}; }
  293. # Long List Time
  294. function llt() { ls -Alt $color ${*:-}; }
  295. # Long List Time, More
  296. function lltm() { ls -Alt $color ${*:-} | more; }
  297. # Long List Time, Less
  298. function lltl() { ls -Alt $color ${*:-} | more; }
  299. # Long List Time, Head
  300. function llth() { ls -Alt $color ${*:-} | head; }
  301. # Long List Time, Tail
  302. function lltt() { ls -Alt $color ${*:-} | tail; }
  303. # List Sort Size
  304. #
  305. # ls aliases for parsing/
  306. #
  307. # Long List Reverse Tail
  308. function lrt() { ls -1Art --color=never ${*:-}; }
  309. # Long List Time
  310. function lt() { ls -1At --color=never ${*:-}; }
  311. # Long List Time, More
  312. function ltm() { ls -1At --color=never ${*:-} | more; }
  313. # Long List Time, Less
  314. function ltl() { ls -1At --color=never ${*:-} | more; }
  315. # Long List Time, Head
  316. function lth() { ls -1At --color=never ${*:-} | head; }
  317. # Long List Time, Tail
  318. function ltt() { ls -1At --color=never ${*:-} | tail; }
  319. # List Sort Size
  320. #
  321. # ls functions sorting by size
  322. #
  323. function lss() { ls -A1s $color ${*:-} | sort -n; }
  324. # List Sort Size Reverse
  325. function lssr() { ls -A1s $color ${*:-} | sort -nr; }
  326. if [[ ! -z "`which xdg-open`" ]]; then
  327. alias open='xdg-open ';
  328. export OPEN='xdg-open ';
  329. else
  330. export OPEN='echo OPEN not defined';
  331. fi
  332. export BROWSER=brave
  333. # new file tail file
  334. function nftf { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail -f ; }
  335. # new file tail
  336. function nft { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs tail ; }
  337. # new file head
  338. function nfh { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs head ; }
  339. # new file less
  340. function nfl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs less ; }
  341. # new file cat
  342. function nfc { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs cat ; }
  343. # new file cat
  344. function nfo { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs $OPEN ; }
  345. # new file ls
  346. function nfls { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs ls -A1t ; }
  347. # new file ls -l
  348. function nflsl { NF=`nf ${1:-.}`; debug NF $NF; echo "$NF" | xargs
  349. ls -Atl ; }
  350. #
  351. # Don't exit the shell casually
  352. #
  353. export IGNOREEOF=42
  354. #
  355. # "...footprints on the sands of time..." (I exist[ed]...)
  356. #
  357. touch $HOME/.bashrc-ran
  358. debug ".bashrc done"