How to Configure Syntax Highlighting in SQLcl

Starting with SQLcl version 21.2, we can now configure SQLcl for syntax highlighting.

That means that the code you write in SQLcl can be color coded, based on the syntax highlighting rules you apply.

The Default (Syntax Highlighting Off)

Here’s a query that I wrote in my Terminal without syntax highlighting:

Screenshot of query in SQLcl without syntax highlighting

So, no color coding has occurred. This uses the colors that I configured in my Terminal’s preferences, which was basically just white text on a black background.

Enable Syntax Highlighting

You can enable syntax highlighting with the following line:

set highlighting on

This provides a basic level of syntax highlighting.

Here’s what the same query looks like now:

Screenshot of a query with a basic level of syntax highlighting

Basically, SQL keywords are highlighted in cyan, but nothing else is.

Add More Color Coding

Fortunately, we can add some more color coding.

For example:

set highlighting identifier foreground magenta

This colors all identifiers with a foreground color of magenta:

Screenshot of the query with more colour coding

Save Settings to a login.sql File

You can save your syntax highlighting settings to a login.sql file.

By doing this, you’ll save yourself from having to configure these settings every time you connect using SQLcl. This is because you can configure it in such a way that the login.sql file runs every time you connect using SQLcl.

Here’s an example of a login.sql file that contains syntax highlighting commands:

set sqlformat ansiconsole

set highlighting on
set highlighting keyword foreground blue
set highlighting identifier foreground magenta
set highlighting string foreground green
set highlighting number foreground cyan
set highlighting comment foreground yellow

Here, my first line isn’t related to syntax formatting – it just sets up the sqlformat to ansiconsole.

The remaining lines enable syntax highlighting and specify which colors to use.

Once you’ve added syntax highlighting to your login.sql file, exit SQLcl and reconnect. When it reconnects, it will run the login.sql file, and all those commands will take effect.

Having added the above commands to my login.sql file, here’s what my query looks like now (after I exited SQLcl and reconnected):

Screenshot of query with full syntax highlighting

Maybe I should have left the keywords at cyan?

Other Syntax Highlighting Options

There are also other options, such as the ability to set a background color, set a bold flag, etc.

The best way to discover these is to run the following command:

help set highlighting

Here’s what that returns for me at the time of this writing:

set highlighting <flag>
                | <type> RESET
                | <type> FOREGROUND <color>
                | <type> BACKGROUND <color>
                | <type> BOLD <flag>
                | <type> ULINE <flag>
                | <type> INVERSE <flag>

	<type>  = DEFAULT | COMMENT | STRING | NUMBER | PUNCTUATION
           | KEYWORD | IDENTIFIER
	<color> = RED | BLUE | BLACK | CYAN | GREEN | MAGENTA | WHITE
           | YELLOW
	<flag>  = ON | OFF | RESET

EXAMPLES
	set highlighting on

The list of colors is quite small (at least at the time of writing), but it’s still enough to provide some decent syntax highlighting.

Given syntax highlighting is a new feature, it’s likely that the above help options will change and be expanded upon in the future.