Shell Scripts Mode Help
Name:Shell Scripts Mode
Version:2.4
Last update:2021-03-03 12:23:44





This document describes the Shell Scripts mode for the software Alpha. This mode is useful for editing Unix, Linux, or Mac OS X shell scripts.

General Notes About sh Mode

  1. This mode serves primarily to make it easier to write unix environment shell scripts -- it colorizes keywords, and sets some indentation, electric, word break, and commenting preferences.
  2. sh mode contains a full set of electric completions, as well as a Completions Tutorial via the Alpha ↣ Shell Scripts Mode Setup menu item.
  3. The sh mode allows for user customization of keyword dictionaries via the Alpha ↣ Shell Scripts Mode Setup ↣ Mode Preferences… dialog. New keywords and color changes will take effect immediately, without the need for a restart or changing windows.
  4. Instructions for editing a shPrefs.tcl file are given below. Any keywords added will not only be colorized, but included in the completions routines as well.
  5. sh mode has Mark File and Parse Function capabilities. See the Marks popup (📖) and the Parse popup (⚙).
  6. Finally, on-line help is available for the sh mode through Shift-Command-Double-Clicking. The URL to which the keywords are sent can be configured in the Mode Preferences dialog.

Specific Notes About sh Mode

Keywords

Keyword dictionaries are available in the shScriptsMode.tcl file, along with any relevant annotation. Commands, flags, program names, common system directories, file suffixes and a few symbols (including the $ magic character) can all be colorized.
Keywords and functions are based on System V and Solaris 2.0, as found in O'Reilly's Unix in a Nutshell, and OS version 5.7 bin directories. Example syntax file: sh Example.
For a different sh script example, which uses block style formatting, try the tcsh example. Toggle the Navigate Blocks preference to change sh mode's file marking and navigation routines. Click here to open the sh Mode Preferences.
See also the completions Tutorial: sh Mode Tutorial.

Shift-Command-Double-Click

On-line help is available for Unix commands through Shift-Command-Double-Clicking, by sending the selected keyword to the menu item Www Command Help. The URL to which the keywords are sent can be changed through the Mode Preferences dialog. See the Unix Commands Help section below for more details.
You can also Shift-Command-Double-Click on keyword defined programs, such as
    spss
    stata
    emacs
etc. You will receive a dialog asking if you would like your browser to attempt to find the host name of https://www.<program>.com . This is probably of limited utility, but you can define additional programs through the preferences as well.

File Marking

The default file marking routine recognizes two kinds of items using the Marks popup (📖): Section marks look like this:
In the document windowIn the Marks menu
### Section Heading• Section Heading
#### Subsection Heading•• Subsection Heading
If the mode preference called Mark Headers Only is turned on, then these section marks will be the only marks that appear. Otherwise, the case | for | if | until | while blocks are marked as well.

Functions Parsing

The Sh mode support functions parsing (this is the Parse popup (⚙) of document windows).
This popup menu contains a list of user defined functions. The bash, ksh, zsh and fish shells support user defined functions. The tcsh shell does not. There are two different syntaxes (fish supports only the second one) in order to define a function in a shell script:
    foo () {...}
or
    function foo {...}
Both syntaxes are recognized by the Sh mode. If the global preference Sort Funcs Menu is set to 1, the function names are displayed in alphabetical order. Otherwise they are in position order.

Adding a sh Prefs file

To add additional commands or arguments in an shPrefs.tcl file, select Alpha ↣ Shell Scripts Mode Setup ↣ Edit Prefs File, and add these lines:
    set shUserCommands {
    	blah bladdity
    }
    
    set shUserPrograms {
    	blah2 bladdity2
    }
    
    set shUserFlags {
    	blah3 bladdity3
    }
    
    sh::colorizesh
Include as many keywords as desired within the braces, separating each keyword by at least one space or carriage return. Alpha will automatically load this preferences file the first that the sh mode is invoked.
Any keywords contained in these categories will not only be colorized, but will also be included as electric completions.

Unix Commands Help

On-line help is available for any sh mode keyword, both through Shift-Command-Double-Clicking and by using the sh Menu ↣ Www Command Help menu item. The behavior of these routines can be re-configured as desired by toggling the sh mode preference Help Opens Man Page.
Here's an example of the difference between the two:
University of Edinburgh, Scotland
https://unixhelp.ed.ac.uk/cgi-bin/unixhelp_search?search_term=rmdir&max_hits=20
https://unixhelp.ed.ac.uk/CGI/man-cgi?rmdir
If the Help Opens Man Page preference is turned on, then keywords are sent to an on-line man pages cgi-bin site. There are a number of different sites available, as listed here: https://www.w3.org/Tools/Man_faq_filters.html
Here are some examples: There are several mirrors of these sites available. If you change the url, be sure to include everything up to the actual command name, including any preceding question marks or equals signs.
If this preference is not set, then keywords are instead sent to the search engine for Unix Help for Users. This site was developed at the University of Edinburgh, and is freely distributed around the world for local installation. The mode is set up to send your query to the UK, but you can change this in the Mode Preferences if want want a site that is closer (or further away).
You'll find a list of mirror sites here: https://www.google.com/search?q=unixhelp
Once you've chosen one, do a search for your favorite command, and then pay attention to the URL that you ended up with. Include the address up to the question mark, as in https://www.dmu.ac.uk/cgi-bin/unixhelp_search?
Here's some examples.

The sh Scripts Menu

The menu associated with this mode contains the following items:
sh Scripts Home Page
Opens the url specified in the sh Scripts Home Page url preference.
Www Command Help
Opens a dialog like this allowing you to enter a unix command to send to the various unix help urls. See the section above on Unix Commands Help for more information.
sh Mode Options
This is a submenu allowing you to set some sh mode flag preferences, or to open this file.
New Comment
Inserts a new paragraph style comment just prior to the current block of commands, such as this
      ## 
       # 
       ##
The template can be customized using the Comment Template menu item.
Note that if the Navigate Blocks preference is set, then the new comment will be inserted at the beginning of the current block, as defined by an empty line.
    ## 
     # 
     ##
    •	rm -f    ../data-temp
    	ln -s    /var/scratch/benefits-cps/  ../data-temp/
Comment Template
Creates (or changes) the sh Custom Template preference. Simply type in the custom template, highlight it, and then use this menu item to save the selected text as the template. To create bullets that you can navigate with the tab key, be sure to insert two bullets for each stop.
These next items will change depending on the Navigate Blocks preference.
Next Block/Command
Jump to the next block of commands, as defined by being separated by at least one empty line, or to the next command as determined by indentation.
Prev Block/Command
Jump to the previous block of commands, or the previous command as determined by indentation.
These items are also bound to Control-Shift-Arrow keys. The Up and Down arrows will simply move the cursor to the start of the next/prev block (or command). Right and Left Arrows will also place this entry at the top of the window.
These items are also bound to 1 and 3 on the keypad when Num-Lock is turned off. These key bindings will place the next/prev block/command in the center of the window. The Num Lock preference in Alpha is located in the General panel of the Global Preferences. It can also be toggled by holding down the Shift key when pressing Num-Lock on the keypad.
If any text is currently highlighted, all of these navigation items will extend the current selection to the next/previous entry.
Select Block/Command
Select the entire block/command in which the cursor currently resides.
Reformat Block
Reformat the entire block/command in which the cursor currently resides. This is essentially a shortcut for the menu items Edit ↣ Select Paragraph and Text ↣ Indent Selection.

Future Versions

I submit this mode to the Alpha community with the hopes that (i) somebody else will find it useful, (ii) any errors or updates will be reported to me, and (iii) I might inspire somebody to take it even further. I will continue to maintain it and post updates as necessary, but any contributions are certainly welcome.

Known problems

Please report any problem or bug you encounter in Alpha's Bug Tracker.

License and Disclaimer

This document has been placed in the public domain.
Original Author: Craig Barton Upright.
Copyright (c) 2000-2021, Craig Barton Upright.
All rights reserved.
The Shell Scripts Mode package is free software and distributed under the terms of the new BSD license:
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CRAIG BARTON UPRIGHT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.