Creating a Custom Syntax in Sublime

As I wrote in a previous post, about five years ago I completely transitioned from handwritten notes to digital notes. I ended up choosing Sublime (aka Sublime Text 3) as my text editor and have stuck with it. 

The file structure I use for notes is as follows:

  • A daily file named mmddyy.txt for all notes related to that day (mostly work-related, but can also include personal stuff like a random conversation with a school teacher that has no other home)
  • Topical files for notes relating to specific technologies, e.g. python-notes.txt
  • Topical files for notes relating to other topics, e.g. chess-notes.txt
Although the topical files don't need to be searched via the file system, the daily notes often need to be searched in a way that can search across all files to, for example, find a note regarding a specific client conversation that happened sometime in the year 2020.

Using a plain text format for these notes not only removes any dependence on a proprietary product, e.g. Evernote, but also makes the files searchable via the filesystem using a combination of the find, grep, and awk commands. In most cases, I am very quickly able to find what I am looking for.

And I save all these files in a folder structure linked to Dropbox. So, they are available in the cloud from any device and from anywhere I go (thereby addressing a big part of the value proposition of tools like Evernote).

Finally, coming to the topic of this post, I created a custom syntax for text files to make my notes more easily consumable. Nothing major, but as the following screenshot demonstrates, I have the ability to create orange color headers, yellow quoted strings, and pink keywords. Additionally, numbers show up in green.


Creating a custom syntax is facilitated by the PackageDev package in Sublime. It starts you off with a template for a new syntax file, which you can then customize to your needs. The custom syntax file I used to create the above syntax highlighting is shown below.


Since I don't want to lose all of my customizations to Sublime in case my Mac ever dies, I actually keep the master copies of the settings, syntaxes, themes, etc. in Dropbox and then create symbolic links to them in the Sublime folder where Sublime expects them to be. The only downside of this approach is that every time I make a change to the master file, I have to delete and recreate the symbolic link in order for Sublime to see the change. It's a small price to pay for having my customizations under source control.

After I made these changes, I noticed that Sublime's built-in spell checker, which highlights misspellings with red squiggly lines was no longer working once my custom syntax highlighting kicked in. To fix that, I had to take my custom syntax scope name (source.lamba) and add it to the front of the "spelling_selector" setting in Sublime > Preferences > Settings, as shown below (again, remember, that I edited my copy under source control and then deleted and recreated the symbolic link under /Users/lamba/Library/Application Support/Sublime Text 3/Packages/Preferences.sublime-settings), which is where Sublime keeps these settings.


Note that I said I added my custom scope (source.lamba) to the front of the setting. As you start to type into that setting, Sublime will usefully prompt you with the default values. After typing in your custom setting (in my case "source.lamba, "), select the default values you're prompted with so that they are also added to the setting as shown above. That will ensure that spell checking works with your custom syntax and also continues to work in all other default scenarios.

Happy typing!

Comments

Popular posts from this blog

Milwaukee Appliance Hand Truck

Utility trailer buying guide

My Experiments with the PICAXE 08M2+