Sublime syntax highlighting for systemd unit files

Sublime Text is my go-to editor for system administration duties (since I automate configuration management, I don’t edit config files over SSH with vim too often.)

I was surprised to see just how much I missed syntax highlighting when I was managing systemd unit files. It turns out writing a sublime-syntax file isn’t too hard.

Feel free to drop this in your Sublime packages folder as Systemd.sublime-syntax and you should get some basic formatting help!

%YAML 1.2
---
name: Systemd
file_extensions: [service]
scope: source.c

contexts:
main:
- match: '^([A-Za-z]+(?=([\=][\-]?)))'
scope: keyword.other
- match: '^\[[A-Za-z]+\]'
scope: entity.name.section
- match: '(\\\w)?'
scope: entity.name
- match: '\\'
scope: constant.character.escape
- match: '\#\w+'
scope: comment.number-sign

Here’s what the matches are hitting against (in order of appearance):

  • Directives (ex “ExecStartPre=”)
  • Unit sections (ex “[Service]”)
  • Multiple line command escape (ex “\” where newlines are inserted for formatting reasons)
  • Comments (starting with a “#”)

A couple sites helped me get to the point where I could write this. First, the Sublime Text Unofficial Documentation project. Also, because it’s based on Macromates, their documentation on language grammars.

As far as I can tell, this is the only systemd unit file syntax highlighting file out there, so please leave a comment if you have improvements. If others find this useful, I’ll package it so you can install with Package Control.