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