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