A Better BBEdit Syntax Colorer for Bash Scripts

BBEdit has been one of my favorite applications since I begin using it in the early nineties, or was it the late 80’s? At any rate, it just keeps getting better and if you do any kind of serious text editing on the Mac, be it HTML, CSS, Java, etc you really should be using BBEdit. Now at version 11 it still doesn’t suck and in fact is still insanely great. It does have one small limitation that I was hoping version 11 would address and that is handling bash scripts better than just “unix shell scripts”. The unix shell scripts language module does an OK job of coloring certain things like comments, strings and keywords, but it doesn’t recognize functions and therefore doesn’t offer code-folding or listing of your functions in the Navigation bar. So, I set out to fix this.

The first thing I had to learn was the extra power, and complexity, of PCRE (Perl Compatible Regular Expressions.) Power users of BBEdit already know that the find and replace function can use grep. But what I didn’t realize is that it doesn’t just use grep, but PCRE, which has some really powerful additions to grep.

At any rate, I won’t bore you with the details. I did create a Codeless Language Module (CLM) which can be dropped into ~/Library/Application Support/Language Modules/ and will do the following for your bash scripts.

  • Color code keywords (like “echo”)
  • Color code comments including block comments (what? how do you do a block comment in bash? — read on)
  • Color coding strings in double or single quotes
  • And BEST OF ALL, code folding and inclusion in the navigation menu of your script’s functions.

Block Quotes in Bash Scripts.

I discovered a neat way to create block quotes while developing my CLM posted by sunny256 on stackoverflow.com (Block Comments in a Shell Script – Stack Overflow) There are probably similar ways invented by equally talented coders, but this is the first that popped up. For my CLM a block quote must look like this:

: <<"EOC"
this is a block
comment that bash
will ignore
EOC

I used the string “EOC” to stand for End Of Comment. If you want to use a different string, then you’ll have to edit the CLM bash.plist.

Long, Multi-line Strings

BBEdit does have a bothersome limitation in color coding long (multi-line) strings. I ran into this trying to get my script’s help text identified as a string. The long bash string looks something like this:

echo "Here is a long
string that utilizes the bash feature of
not ending a string at a newline character
but only at the closing quotation mark.
.
.
MANY LINES OR CHARACTERS LEFT OUT
.
.
here's the end of the long string"

If your long string contains too many (how many???) characters the color coding encounters a stack overflow and stops recognizing strings.

Functions and Code Folding

This is really what I wanted, and finally achieved. Take a function like:

doit ()
{
#this function doesn't do much.
echo "$1"
echo "$1"
echo "$1"
echo "$1"
echo "$1"
echo "$1"
echo "$1"
echo "$1"
echo "$1"
}
# some more code after the function.

When folded this will look like this:

1|doit ()
13|(…)
14| # some more code after the function

Here’s copy of the plist embedded in a Word doc file… I can’t include .txt or .plist files in a WordPress post.

bash_plist

Leave a comment

11 Comments

  1. I tried installing this plist into BBEdit 11.5.2 but it didn’t seem to work (after restarting BBEdit and selecting it for .sh files). For example, I didn’t see any code folding triangles.

    Reply
    • drpeterj

       /  May 15, 2016

      Make sure the plist file is not a word document, but a plain text file.

      Reply
  2. drpeterj

     /  May 15, 2016

    Does it appear in the list of languages? Are your .sh files recognized automatically as Unix Shell Scripts? Here is a snippet that works for me in 11.5.2. The comments, reserved words such as “echo” and “select”, and literal text are all colorized and the cont() block can be folded.

    #!/bin/bash
    #This is just a test file.
    #deployto: /home/peter/.ssh
    #set -x
    cont ()
    {
    echo $1
    select ans in ‘Yes’ ‘No’ ; do
    case $ans in
    “Yes” )
    echo “Continuing”;
    break;;
    “No”) exit 1 ;;
    esac
    done
    }

    Reply
  3. Mike

     /  January 12, 2017

    Extremely cool! Thank you.

    I had to manually select Bash script (vs. Unix Shell Script).

    I also had to change my function formatting as it’s a little unforgiving, e.g. “myfunc () }” is not supported. And extraneous whitespace throws it off, but that’s a small cost for function folding.

    Do you have any idea how to solve automatic Markers or functions search from the Go Menu for Bash?

    Perhaps host the plist file on zippyshare.com or some other less odious free hosting site to overcome the WordPress restrictions?

    Thanks again.

    Reply
  4. Mike

     /  January 12, 2017

    I did find bugs with “” and \”. They throw off color coding for me.

    I also can’t use the Un/Comment Lines function. When I switch back to Unix Shell Script it works again.

    I’m using the last BBEdit version: version 11.6.3 (397042). Any ideas?

    Thanks again

    Reply
    • drpeterj

       /  January 13, 2017

      Mike,
      Glad you like it. I haven’t tried it with the latest version of BBEdit. I’ll see what I can figure out.

      Reply
  5. Mike

     /  January 12, 2017

    Also broke autocompletion for me.

    Are these known issues or am I messing something up?

    Thanks

    Reply
  6. Mike

     /  February 19, 2017

    I spent some time checking out several other text editors looking for better shell script editing support. While all of them had something they did well, Textmate (currently 2.0rc4) is far and away the best one I’ve found. It solved my code folding issues and provides function jump navigation. I was even able to easily add pragma mark support.

    In addition to great built-in support for shell editing, Textmate is also highly customizable (like BBEdit) and has good community support for sharing bundles.

    Like BBEdit, it supports running scripts (Textmate also supports running selected lines) in the editor. I actually like Textmate’s implementation a little better. BBEdit was always a little clunky to me for running scripts and I used Code Runner too for prototyping. Now Textmate handles that function for me as well.

    I’ve used (and owned) BBedit for decades, but Textmate is now my default shell script editor. I still use BBEdit for one-off text editing functions it handles better like version diff.

    Honorable mentions: Code Runner 2, Textastic. (Atom and Sublime just didn’t appeal to me.)

    Reply
  7. Hi!! Thanks for good bash module.

    here’s for someone who write script like my style…
    I changed line 802 to 806 as follows since I prefer ‘function myfunc(){‘ style.

    (?x:
    (?P<function>
    (?P<function_name>[a-z0-9 _]+)\s*\x28\x29
    \r*(?s:{\s*\r.*?\r})
    )

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: