Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go HTML template syntax highlighting for Sublime Text

Standalone Go HTML template syntax highlighting for Sublime Text

These files are part of the GoSublime package by DisposaBoy.

Hello friends. It is always very annoying to write HTML templates for a Go application in Sublime Text, and only have the HTML part of the code highlighted. Those white lines of code -- which are the most important parts of the template files -- are just not pleasant to the eye.

Luckily, DisposaBoy has made a syntax highlighter for Go HTML templates in the GoSublime package (which can be installed using Package Control), but what if you don't actually want to install the whole package just to have syntax highlighting?

The files from this gist do all the job for you and you won't even have to install the whole package! (I highly recommend you do, though -- it's really useful!)

Just save these two files into the Packages/User folder (Preferences -> Browse Packages, and then the User folder) and you're all set!

Example

GoSublime (c) DisposaBoy.

%YAML 1.2
---
name: 'GoHTML'
file_extensions:
- gohtml
- html.go
scope: text.html.gohtml
contexts:
main:
- match: ''
push: 'Packages/HTML/HTML.sublime-syntax'
with_prototype:
- match: '{{'
captures:
0: punctuation.section.embedded.begin.gotemplate
push: Packages/User/GoTemplate.sublime-syntax
with_prototype:
- match: '}}'
captures:
0: punctuation.section.embedded.end.gotemplate
pop: true
%YAML 1.2
---
name: 'GoTemplate'
scope: source.gotemplate
hidden: true
contexts:
main:
- match: ":="
scope: keyword.operator.initialize.gotemplate
- match: \|
scope: keyword.operator.pipe.gotemplate
- match: '[.$][\w]*'
scope: variable.other.gotemplate
- match: \b(if|else|range|template|with|end|nil|define)\b
scope: keyword.control.gotemplate
- match: \b(and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b
scope: support.function.builtin.gotemplate
- match: /\*
push:
- meta_scope: comment.block.gotemplate
- match: \*/
pop: true
- match: '"'
captures:
0: punctuation.definition.string.begin.gotemplate
push:
- meta_scope: string.quoted.double.gotemplate
- match: '"'
captures:
0: punctuation.definition.string.end.gotemplate
pop: true
- include: string_placeholder
- include: string_escaped_char
- match: "`"
captures:
0: punctuation.definition.string.begin.gotemplate
push:
- meta_scope: string.quoted.raw.gotemplate
- match: "`"
captures:
0: punctuation.definition.string.end.gotemplate
pop: true
- include: string_placeholder
string_escaped_char:
- match: '\\(\\|[abfnrtv''"]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[0-7]{3})'
scope: constant.character.escape.gotemplate
- match: \\.
scope: invalid.illegal.unknown-escape.gotemplate
string_placeholder:
- match: |-
(?x)%
(\d+\$)? # field (argument #)
[#0\- +']* # flags
[,;:_]? # separator character (AltiVec)
((-?\d+)|\*(-?\d+\$)?)? # minimum field width
(\.((-?\d+)|\*(-?\d+\$)?)?)? # precision
[diouxXDOUeEfFgGaAcCsSqpnvtTbyYhHmMzZ%] # conversion type
scope: constant.other.placeholder.gotemplate
- match: "%"
scope: invalid.illegal.placeholder.gotemplate
@maxkwallace
Copy link

maxkwallace commented Feb 11, 2021

Thank you so much for this! I'm working on a site with Hugo (no actual Go development, but Go templating) and this is exactly what I needed.

I can't find GoSublime in the 'Package Control: Install Package' search dropdown, plus there's this https://github.com/DisposaBoy/GoSublime#installation--support so my impression is that GoSublime no longer supports installation via Package Control though I could be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment