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

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.
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 ()
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.