sublime text syntax files
- User
sublime text syntax files
- Assistant
sublime text syntax files
Sublime Text uses syntax files to define the highlighting and other syntax-related features for different programming languages and file types. These files help the editor understand the structure and elements of the code so that it can provide features like syntax highlighting, code folding, and sometimes even basic linting.
.tmLanguage and .sublime-syntax Files:
.tmLanguage is an older format based on XML..sublime-syntax is a newer, YAML-based format introduced in Sublime Text 3, which is more readable and easier to manage.This is the preferred format for defining syntax. Here is an example of a simple .sublime-syntax file for a fictional "Toy" programming language:
%YAML 1.2
---
name: Toy
file_extensions:
- toy
scope: source.toy
contexts:
main:
- match: \b(if|else|while|return)\b
scope: keyword.control.toy
- match: \b(function|var)\b
scope: keyword.declaration.toy
- match: '"'
scope: punctuation.definition.string.begin.toy
push:
- meta_scope: string.quoted.double.toy
- match: '"'
scope: punctuation.definition.string.end.toy
pop: true
- match: '\\.'
scope: constant.character.escape.toy
- match: '[^"\\]+'
scope: string.quoted.double.toy
- match: '[a-zA-Z_]\w*'
scope: identifier.toy
- match: '\d+'
scope: constant.numeric.toy
- match: '[+\-*/=<>!]+'
scope: keyword.operator.toy
This is the older XML-based format. Here's an example of defining syntax for the same Toy language in a .tmLanguage file:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>name</key>
<string>Toy</string>
<key>scopeName</key>
<string>source.toy</string>
<key>fileTypes</key>
<array>
<string>toy</string>
</array>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\b(if|else|while|return)\b</string>
<key>name</key>
<string>keyword.control.toy</string>
</dict>
<dict>
<key>match</key>
<string>\b(function|var)\b</string>
<key>name</key>
<string>keyword.declaration.toy</string>
</dict>
<dict>
<key>begin</key>
<string>"</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.begin.toy</string>
</dict>
</dict>
<key>end</key>
<string>"</string>
<key>endCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.end.toy</string>
</dict>
</dict>
<key>name</key>
<string>string.quoted.double.toy</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\\.</string>
<key>name</key>
<string>constant.character.escape.toy</string>
</dict>
</array>
</dict>
<dict>
<key>match</key>
<string>[a-zA-Z_]\w*</string>
<key>name</key>
<string>identifier.toy</string>
</dict>
<dict>
<key>match</key>
<string>\d+</string>
<key>name</key>
<string>constant.numeric.toy</string>
</dict>
<dict>
<key>match</key>
<string>[+\-*/=<>!]+</string>
<key>name</key>
<string>keyword.operator.toy</string>
</dict>
</array>
</dict>
</plist>
To add a syntax file to Sublime Text:
Using Package Control:
Preferences > Package Control and search for the desired language or syntax.Manual Installation:
.sublime-syntax or .tmLanguage file to:
Packages/User/ directory.Packages/ and place your files there.Packages directory can be accessed by going to Preferences > Browse Packages.Developer Options:
Package Control: Satisfy Dependencies or Package Control: Upgrade/Overwrite All Packages commands.By defining syntax files, you can extend Sublime Text to support any programming language, configuration file, or text format you work with.
Not yet included in a published release.