Skip to content

Instantly share code, notes, and snippets.

@jozsefsallai
Last active September 8, 2025 13:32
Show Gist options
  • Select an option

  • Save jozsefsallai/5b09fb0099158344512aaec8121220a1 to your computer and use it in GitHub Desktop.

Select an option

Save jozsefsallai/5b09fb0099158344512aaec8121220a1 to your computer and use it in GitHub Desktop.
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
@vistad
Copy link

vistad commented Jun 27, 2025

Error loading syntax file "Packages/User/GoHTML.sublime-syntax”: Apparent
recursion within a with_prototype action: 25000 context sanity limit hit

Here is an error-free version:

%YAML 1.2

name: 'GoHTML'
file_extensions:

  • gohtml
  • html.go
    scope: text.html.gohtml
    contexts:
    main:
    • include: html

html:
- match: '{{'
scope: punctuation.section.embedded.begin.gotemplate
push:
- meta_scope: source.gotemplate.embedded.html
- match: '}}'
scope: punctuation.section.embedded.end.gotemplate
pop: true
- include: gotemplate

- include: scope:text.html.basic

gotemplate:
- match: '[a-zA-Z_][a-zA-Z0-9_]'
scope: variable.other.gotemplate
- match: '"[^"]
"'
scope: string.quoted.double.gotemplate
- match: "'[^']*'"
scope: string.quoted.single.gotemplate
- match: '.'
scope: variable.language.this.gotemplate
- match: '|'
scope: keyword.operator.pipe.gotemplate
- match: '\s+'
scope: text.whitespace

@mpcabd
Copy link

mpcabd commented Sep 8, 2025

@vistad could you paste your fix as a Gist, or wrap it in code marks like this:

```yaml

your code here

```

Thanks.

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