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