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