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