Sublime syntax highlighting for systemd unit files

Dan Bowling
Mar 4, 2016 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store