1#----------------------------------------------------------------------------
  2#
  3#  TITLE
  4#
  5#      .bashrc
  6#
  7#  DESCRIPTION
  8#
  9#
 10#     Bourne Again Shell (bash) startup file for Unix systems.  Executed 
 11#     everytime we start a subshell.  Install into your home directory ~.
 12#     Put aliases and functions here.
 13#    
 14#     Use source .bashrc to reset the environment after you are in a
 15#     terminal window.  Place the line "source .bashrc" into .bash_profile
 16#     to execute this file's commands upon login.
 17#
 18#     To debug, use sh -x .bashrc
 19#
 20#  DATE
 21#
 22#      20 Jul 25
 23#
 24#  AUTHOR
 25#
 26#      Sean E. O'Connor
 27#
 28#----------------------------------------------------------------------------
 29
 30#------------- Aliases ------------------------------------------------------
 31#
 32#   Be sure to put useful scripts and executables into the home bin directory, ~/bin or global /usr/local/bin
 33#
 34
 35alias desk='cd ${desk_dir}'
 36
 37alias app='cd ${app_dir}'
 38
 39alias art='cd ${arts_dir}/Visual/Painting/OriginalWorks'
 40alias bus='cd ${business_dir}'
 41alias acc='cd ${business_dir}/Accounts'
 42alias fam='cd ${family_dir}'
 43alias lit='cd ${arts_dir}/Literature'
 44alias sci='cd ${science_dir}'
 45alias math='cd ${science_dir}/Mathematics'
 46alias comp='cd ${science_dir}/ComputerScience'
 47alias fuk='cd ${science_dir}/ComputerScience/ObsoleteSoftware/Fruit/MTWikiNew'
 48
 49#------------- Git ------------------------------------------------
 50
 51# Location of git repository.
 52export GITREPOS="${web_dir}/private/repos"
 53
 54# These are all my git repositories.
 55alias weba='cd ${web_dir}/Art'
 56alias webcrc='cd ${web_dir}/CommunicationTheory/ChannelCoding/CyclicRedundancyCheckCodes'
 57alias webc='cd ${web_dir}/ComputerScience/DevelopmentEnvironment'
 58alias webfft='cd ${web_dir}/Mathematics/SignalProcessing/FastFourierTransform'
 59alias webf='cd ${web_dir}/Finance'
 60alias webl='cd ${web_dir}/ComputerScience/Automata/Life'
 61alias webp='cd ${web_dir}/ComputerScience/Compiler/ParserGeneratorAndParser'
 62alias webpp='cd ${web_dir}/Mathematics/AbstractAlgebra/PrimitivePolynomials'
 63alias webps='cd ${web_dir}/CommunicationTheory/PseudoNoiseSequences'
 64alias webu='cd ${web_dir}/private'
 65alias webd='cd ${web_dir}/WebPageDesign'
 66alias web='cd ${web_dir}'
 67alias webj='cd ${web_dir}/mathjax'
 68
 69# Subdir of WebPageDesign git repo.
 70alias webm='cd ${web_dir}/WebPageDesign/MaintainWebPage'
 71
 72# View the status of a git repository.
 73function gitshow()
 74{
 75    if [ $# == 0 ]
 76    then
 77        echo "Usage:  gitshow <git directory>"
 78    fi
 79
 80    # Grab the function argument, bash style.
 81    gitdir=$1
 82
 83    # Parent directory on my machine.
 84    parentDir=${HOME}/Desktop/Sean
 85
 86    printf "\n\n"
 87    printf "${blueonyellow} git repository ${gitdir} ${resetcolor}\n"
 88    printf "\n\n"
 89    
 90    # Go into the git local snapshot.
 91    pushds ${parentDir}/${gitdir}
 92
 93    # Pull the remote branch to get local snapshot up to date.
 94    printf "${redongreen}pulling:${resetcolor}\n\t"
 95    git pull
 96
 97    # Clean and recurse into directories.  Don't babble the status.
 98    git clean -f -d --quiet
 99
100    # Collect garbage.  Don't babble the status.
101    git gc --quiet
102
103    # Show the branches, status, stash lists, most recent commit.
104    printf "${redongreen}branches:${resetcolor}\n\t"
105    git branch
106
107    printf "${redongreen}status:${resetcolor}\n\t"
108    git status
109
110    printf "${redongreen}stashes:${resetcolor}\n\t"
111    git stash list
112    printf "\n"
113
114    printf "${redongreen}last commit${resetcolor}:\n\t"
115    git log -1 --name-only
116    printf "${redongreen}---${resetcolor}\n\n"
117    popds
118}
119
120# View all my Git repositories.
121function gita()
122{
123    # How did I find them all on my system?
124    #     find . -name '*.git' 
125
126    # Ascii terminal colors.   Example: printf "\u1b[31;42mRedOnGreen\u1b[0mNormal"
127    redongreen="\u1b[31;42m"
128    redonblue="\u1b[31;44m"
129    blueonyellow="\u1b[34;43m"
130    whiteonblue="\u1b[37;44m"
131    resetcolor="\u1b[0m"
132
133    clear
134
135    # Show each repository and its status.
136    gitshow WebSite;                                                                                    continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
137    gitshow WebSite/Art ;                                                                               continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
138    gitshow WebSite/CommunicationTheory/ChannelCoding/CyclicRedundancyCheckCodes;                       continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
139    gitshow WebSite/CommunicationTheory/PseudoNoiseSequences;                                           continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
140    gitshow WebSite/ComputerScience/DevelopmentEnvironment;                                             continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
141    gitshow WebSite/Mathematics/SignalProcessing/FastFourierTransform;                                  continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
142    gitshow WebSite/Finance;                                                                            continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
143    gitshow WebSite/ComputerScience/Automata/Life;                                                      continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
144    gitshow WebSite/ComputerScience/Compiler/ParserGeneratorAndParser;                                  continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
145    gitshow WebSite/Mathematics/AbstractAlgebra/PrimitivePolynomials;                                   continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
146    gitshow WebSite/private;                                                                            continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
147    gitshow WebSite/WebPageDesign;                                                                      continueOrQuit ; if [ $? == 1 ] ; then return ; else clear ; fi
148}
149
150
151#------------- Blender ------------------------------------------------
152
153# Developer version compiled from source.
154alias blender='/Users/seanoconnor/blender-git/build_darwin/bin/Blender.app/Contents/MacOS/Blender'
155
156#------------- Set prompt ---------------------------------------------
157#
158# Define colors for the text in a prompt.
159#
160startcolor="\[\e["
161black="30"
162red="31"
163green="32"
164yellow="33"
165blue="34"
166magenta="35"
167teal="36"
168white="37"
169separator=";"
170blackbackground="40"
171redbackground="41"
172greenbackground="42"
173yellowbackground="43"
174bluebackground="44"
175magentabackground="45"
176tealbackground="46"
177whitebackground="47"
178reset="0"
179boldtext="1"
180underline="4"
181blink="5"
182inverted="7"
183endcolor="m\]"
184resetcolor="\e[0m"
185whiteonblue="${startcolor}${white}${separator}${bluebackground}${endcolor}"
186redonblue="${startcolor}${red}${separator}${bluebackground}${endcolor}"
187
188# Set the prompt to
189# time \@, date \d, user name \u, host name \h, current directory \w
190# \W basename of current directory, \$ if UID = 0 (root), use # instead of $
191export PS1="${redonblue}\u:${whiteonblue}\w${resetcolor}\$ "
192###echo ${PS1}
193
194
195#------------- Shell options ------------------------------------------------
196#
197# Set vi edit mode for the command line.
198# Hit <ESC> to go into vi's edit command mode:
199#   h   Move cursor left
200#   l   Move cursor right
201#   A   Move cursor to end of line and put in insert mode
202#   0   (zero) Move cursor to beginning of line (doesn't put in insert mode)
203#   i   Put into insert mode at current position
204#   a   Put into insert mode after current position
205#   dd  Delete line (saved for pasting)
206#   D   Delete text after current cursor position (saved for pasting)
207#   p   Paste text that was deleted
208#   j   Move up through history commands
209#   k   Move down through history commands
210#   u   Undo
211set -o vi
212
213# Don't wait for job termination notification
214set -o notify
215
216# Don't use ^D to exit
217set -o ignoreeof
218
219# Use case-insensitive filename globbing
220shopt -s nocaseglob
221
222# Make bash append rather than overwrite the history on disk
223shopt -s histappend
224
225# When changing directory small typos can be ignored by bash
226# for example, cd /vr/lgo/apaache would find /var/log/apache
227shopt -s cdspell
228
229shopt -s cdable_vars
230
231#------------- Completion options ------------------------------------------------
232#
233# These completion tuning parameters change the
234# default behavior of bash_completion:
235
236# Define to avoid stripping description in --option=description of './configure --help'
237COMP_CONFIGURE_HINTS=1
238
239# Define to avoid flattening internal contents of tar files
240COMP_TAR_INTERNAL_PATHS=1
241
242# If this shell is interactive, turn on programmable completion enhancements.
243# Any completions you add in ~/.bash_completion are sourced last.
244case $- in
245  *i*) [[ -f /etc/bash_completion ]] && . /etc/bash_completion ;;
246esac
247
248
249#------------- History options ------------------------------------------------
250#
251# Don't put duplicate lines in the history.
252export HISTCONTROL="ignoredups"
253
254# Ignore some controlling instructions
255export HISTIGNORE="ls:ls *:[   ]*:&:cd:cd ..:exit:hi:s:f:m:um"
256
257# Whenever displaying the prompt, write the previous line to disk
258export PROMPT_COMMAND="history -a"
259
260
261#------------- Aliases ------------------------------------------------
262#
263#   If these are enabled they will be used instead of any instructions
264#   they may mask.  For example, alias rm='rm -i' will mask the rm
265#   application.
266#
267#   To override the alias instruction use a \ before, ie
268#   \rm will call the real rm not the alias.
269#
270#   To see all aliases, type alias.
271#   Use unalias to remove a definition.
272
273# Interactive operation...
274alias rm='rm -i'
275alias cp='cp -i'
276alias mv='mv -i'
277alias up='cd ..'
278
279# Default to human readable figures
280alias df='df -h'
281alias du='du -hac'
282
283# Misc :)
284alias less='less -r'                          # raw control characters
285alias whence='type -a'                        # where, of a sort
286alias grep='grep --color'                     # show differences in colour
287alias hi=history
288
289# Some shortcuts for different directory listings
290alias ls='ls -hF ${ls_color_option}'
291alias dir='ls --color=auto --format=vertical'
292alias ll='ls -l'                              # long list
293alias la='ls -A'                              # all but . and ..
294alias l='ls -CF'                              #
295
296
297#------------- Utility functions ------------------------------------------------
298
299# Push and pop directory without error messages.
300function pushds
301{
302    command pushd "$1" > /dev/null
303}
304
305function popds
306{
307    command popd "$1" > /dev/null
308}
309
310# Recursive search for a string in a file.
311function grepall()
312{
313    if [ $# == 0 ]
314    then
315        echo "Usage:  grepall <string>"
316    fi
317
318    # Grab the function argument, bash style.
319    pat=$1
320
321    echo "Searching all subdirectories for pattern ${pat}"
322
323    find . -name '*.[ch]'   -exec grep -iH "${pat}" {} ';'
324    find . -name '*.hpp'    -exec grep -iH "${pat}" {} ';'
325    find . -name '*.cpp'    -exec grep -iH "${pat}" {} ';'
326    find . -name '*.py'     -exec grep -iH "${pat}" {} ';'
327    find . -name '*.lsp'    -exec grep -iH "${pat}" {} ';'
328    find . -name '*.m'      -exec grep -iH "${pat}" {} ';'
329    find . -name '*.js'     -exec grep -iH "${pat}" {} ';'
330    find . -name '*.java'   -exec grep -iH "${pat}" {} ';'
331    find . -name '*.pl'     -exec grep -iH "${pat}" {} ';'
332    find . -name '*.prl'    -exec grep -iH "${pat}" {} ';'
333    find . -name '*.html'   -exec grep -iH "${pat}" {} ';'
334    find . -name '*.css'    -exec grep -iH "${pat}" {} ';'
335    find . -name 'makefile' -exec grep -iH "${pat}" {} ';'
336    find . -name '*.dat'    -exec grep -iH "${pat}" {} ';'
337    find . -name '*.txt'    -exec grep -iH "${pat}" {} ';'
338}
339
340function touchall()
341{
342    find . -exec touch {} ';'
343}
344
345function testOptions()
346{
347    if [ $# == 0 ]
348    then
349        echo "Number of arguments to testOptions is $#"
350    fi
351
352    # No spaces around the equals allowed in bash!
353    a1=$1
354
355    echo "You said |${a1}|"
356
357    # Compare the first 3 letters.
358    if [ "${a1:0:3}" == "tes" ]
359    then
360        echo "You said testOptions tes"
361    else
362        echo "What did you say?"
363    fi
364}
365
366# Launch gvim editor.
367function gvim()
368{
369    # No file name given?
370    if [ $# == 0 ]
371    then
372        # Remove the old file.
373        fileName="${HOME}/temp.txt"
374        if [ -f "${fileName}" ] ; then
375            echo "Removing file ${fileName}"
376            rm -rf ${fileName}
377        fi
378
379        # Remove any swap file.
380        fileNameSwap="${HOME}.vim/.swp/temp.txt.swp"
381        if [ -f "${fileNameSwap}" ] ; then
382            echo "Removing swap file ${fileNameSwap}"
383            rm -rf ${fileNameSwap}
384        fi
385
386        # Create a new file.
387        echo -n > ${fileName}
388        echo "Opening temporary file ${fileName}"
389    else
390        fileName=$1
391    fi
392
393    # Find out which operating system we are running on:  macOS, Linux, Windows/Cygwin, etc.
394    uname=`uname -s`
395
396    # macOS.  Tested on my MacBook Pro laptop mid-2015 model with Intel x86_64 architecture.
397    if [ "${uname}" == "Darwin" ] ; then
398        platform="macos"
399    # Linux.  Tested on my Ubuntu Linux system running on my Cyperpower PC with a 64-bit AMD CPU.
400    elif [ "${uname}" == "Linux" ] ; then
401        platform="linux"
402    # Cygwin.  For cygwin 2.2 64-bit on Windows 10 64-bit.  Not tested.  From https://en.wikipedia.org/wiki/Uname
403    elif [ "${uname}" == "CYGWIN_NT-10.0" ] ; then
404        platform="cygwin"
405    fi
406    #echo "Using platform = ${platform}"
407
408    # Launch GUI Vim on my macOS machine.
409    if [ "${platform}" == "macos" ] ; then
410        open -a MacVim "${fileName}"
411    # Launch GUI Vim on my Ubuntu Linux machines.
412    elif [ "${platform}" == "linux" ] ; then
413        /usr/bin/vim "${fileName}"
414    # Cygwin
415    elif [ "${platform}" == "cygwin" ] ; then
416        /usr/bin/vim "${fileName}"
417    else
418        echo "Could not get a platform.  Guessing Linux."
419        /usr/bin/vim "${fileName}"
420    fi
421}
422
423# Remove temporary files.
424function cleanall()
425{
426    if [ $# != 0 ]
427    then
428        echo "Usage:  cleanall"
429    fi
430
431    find . -name '*~'          -print -exec rm -f {} \;
432    find . -name '._*'         -print -exec rm -f {} \;
433    find . -name '.DS_Store*'  -print -exec rm -f {} \;
434    find . -name 'Thumbs.db'   -print -exec rm -f {} \;
435    find . -name '*.swp'       -print -exec rm -f {} \;
436    find . -name '*.o'         -print -exec rm -f {} \;
437    find . -name '*.class'     -print -exec rm -f {} \;
438    find . -name '*.o~$'       -print -exec rm -f {} \;
439    find . -name '*.o~>'       -print -exec rm -f {} \;
440    find . -name '*.dSYM'      -print -exec rm -rf {} \;
441    find . -name '*.obj'       -print -exec rm -rf {} \;
442    find . -name '*.ncb'       -print -exec rm -rf {} \;
443    find . -name '*.suo'       -print -exec rm -rf {} \;
444    find . -name '*.idb'       -print -exec rm -rf {} \;
445    find . -name '*.pdb'       -print -exec rm -rf {} \;
446    find . -name '*.manifest'  -print -exec rm -rf {} \;
447    find . -name '*.Spotlight-V100'  -print -exec rm -rf {} \;
448    find . -name '*.Trash*'          -print -exec rm -rf {} \;
449    find . -name '*.fseventsd'       -print -exec rm -rf {} \;
450}
451
452# Return status 0 if we hit the SPACE BAR or 1 if we hit q for QUIT.
453function continueOrQuit()
454{
455    # See https://www.computerhope.com/unix/bash/read.htm
456    #  -n1   Read only one character.
457    #   -s   Don't echo keystrokes.
458    #   -r   Raw input.  read backslashes and don't interpret them as escape characters.
459    #   -p   Print the string prompt first before reading the line.
460    read -n1 -s -r -p $'Press space to continue or q to quit...\n' key
461    if [ "$key" = ' ' ]; then
462        return 0
463    elif [ "$key" = 'q' ]; then
464        return 1
465    fi
466    printf "\n\n"
467}