• MetaDescription: How to add themes, snippets and colorization and bracket matching to Visual Studio Code. TextMate .tmLanguage files are supported.
  • Themes, Snippets and Colorizers - 主题,片段与调色板
    • Adding a new Color Theme - 添加一个新的颜色主题
      • Workbench colors
      • Syntax highlighting colors
      • TextMate theme rules
    • Create a new color theme
    • Test a new color theme
    • Publishing a Theme to the Extension Marketplace
    • Adding a new Icon Theme
      • Icon Set File
      • Icon definitions
      • File association
      • Font definitions
      • Folder icons in File Icon Themes
    • Using TextMate Snippets
    • Sharing Your Snippets in the Marketplace
    • Adding a New Language (Colorizer)
    • Publishing Language Support to the Extension Marketplace
    • Add to your Language Support Extension
    • Language Identifiers
      • New identifier guidelines
    • Next Steps
    • Common Questions

    Order: 7
    Area: extensions
    TOCTitle: Themes, Snippets and Colorizers
    ContentId: 448E9027-3AD0-420D-9A58-D428D1B1067D
    PageTitle: Add Themes, Snippets and Colorizers to Visual Studio Code
    DateApproved: 12/14/2017

    MetaDescription: How to add themes, snippets and colorization and bracket matching to Visual Studio Code. TextMate .tmLanguage files are supported.

    Themes, Snippets and Colorizers - 主题,片段与调色板

    Custom color and icons themes, snippets and language syntax colorizers bring an editor to life. There are lots of existing TextMate customization files available and VS Code lets you easily package and reuse these. You can directly use .tmTheme, .tmSnippets, and .tmLanguage files in your extensions and share them in the extension Marketplace. This topic describes how to reuse TextMate files as well as create and share your own themes, snippets and colorizers.

    颜色、图标主题、片段和语法高亮的自定义调色板可以给编辑器以生命力。已经存在大量可用的TextMate自定义文件,并且VS Code让你可以轻易的打包和重用它们。你可以直接使用扩展名为.tmTheme.tmSnippets.tmLanguage的文件,也可以在扩展市场中分享你的主题。 这个标题描述了如何重用TextMate的文件,以及如何创建或分享你自己的主题、片段和调色板。

    Adding a new Color Theme - 添加一个新的颜色主题

    Colors visible in the VS Code user interface fall in two categories:

    VS Code用户界面中的可见颜色分为两类:

    • Workbench colors used in views and editors, from the Activity Bar to the Status Bar. A complete list of all these colors can be found in the color reference.
    • Syntax highlighting colors used for source code in the editor. The theming of these colors is different as syntax colorization is based on TextMate grammars and TextMate themes.

    Workbench colors

    The easiest way to create a new workbench color theme is to start with an existing color theme and customize it:

    • Switch to the color theme that you want to modify.
    • Open the settings and make changes to view and editor colors using the workbench.colorCustomizations. Changes are applied live to your VS Code instance and no refreshing or reloading is necessary.
    • A complete list of all themable colors can be found in the color reference.

    Syntax highlighting colors

    For syntax highlighting colors, there are two approaches. You just simply reference an existing TextMate theme (.tmTheme file) from the community, or you can come up with your own theming rules. The easiest way is to start with an existing theme and customize it:

    • Switch to the color theme to customize and use the editor.tokenColorCustomizations settings. Changes are applied live to your VS Code instance and no refreshing or reloading is necessary.
    • The setting supports a simple model with a set of common token types such as ‘comments’, ‘strings’ and ‘numbers’ available. If you want to color more than that, you need to use textMate theme rules directly.

    TextMate theme rules

    To write TextMate theme rules, you need to know about TextMate grammars and scopes.

    TextMate grammars consist of a set of regular expression that are used create a syntax tree out of the source code. Each tree node spans a source range and represents a scope. Scopes have a name and stand for either code sections (such as functions, blocks, comments) or symbols (for example keywords, numbers, operators).

    Here’s an example of the scope hierarchy generated for a JavaScript code sample:

    1. function f1() {

    TextMate Scopes

    Each scope name consists of segments separated by dots. The last segment is the name of the language the symbol belongs to: entity.name.function.js.

    A good overview of scope names that TextMate grammars typically generate can be found here.

    The list of scope names active at a given offset are the input for syntax highlighting.

    Text Mate themes describe the theming rules used for syntax highlighting. Each rule consists of one or more scope selectors and a set of styles: colors (foreground & background) and font styles (bold, italics and underline).

    To evaluate the style of a symbol at a given offset, the scopes at that offset are computed. The theming rules are then processed first to last. The rule’s scope selectors are matched against that set of scopes. The rule with the most specific match wins.

    Here are some example theming rules. The scope property lists the rules scope selectors. The setting property describes the styles to apply when the rule wins. The name is just used for documentation.

    • TextMate themes assign a set of styles to one or more scopes. The styles are the foreground color, the background color and bold, italics and underline. A theme consist of a set of rules. To evaluate the style of a symbol, the rules are processed first to last and each scope selector is matched against to symbols scope and parent scopes. The most specific rule is used for styling the symbol.

    • Scope selector support prefix matching and matching against parent scopes

    1. {
    2. "name": "Variables",
    3. "scope": "variable",
    4. "settings": {
    5. "foreground": "#dc3958",
    6. "fontStyle": "bold underline"
    7. }
    8. },
    9. {
    10. "name": "Functions",
    11. "scope": [
    12. "entity.name.function",
    13. "meta.selector.css entity.name.tag",
    14. "entity.name.method - source.java"
    15. ],
    16. "settings": {
    17. "foreground": "#8ab1b0"
    18. }
    19. }
    • variable matches all scopes that start with variable: variable.js, variable.parameter.java
    • meta.selector.css entity.name.tag matches all scopes that start with entity.name.tag and have a parent scope that matches meta.selector.css
    • entity.name.method - source.java matches all scopes that start with entity.name.method but are not inside a parent scope that matches source.java
    • Learn more about scope selectors here.

    You can use the Developer Tools: Inspect TM Scopes command from the Command Palette (kb(workbench.action.showCommands)) to inspect the scopes of a token at the cursor and to see which theming rule has been applied.

    inspect scoped

    Create a new color theme

    • Generate a theme file using the Generate Color Theme from Current Settings command from the Command Palette
    • Use VS Code’s Yeoman extension generator, yo code, to generate a new theme extension:

      1. npm install -g yo generator-code
      2. yo code
    • If you customized a theme as described above, select ‘Start fresh’.

    yo code theme

    • Copy the theme file generated from your settings to the new extension.
    • To use a existing TextMate theme, you can tell the extension generator to import a TextMate theme file and package it for use in VS Code. Alternatively, if you have already downloaded the theme, replace the tokenColors section with a link to the .tmTheme file to use.
    1. {
    2. "type": "dark",
    3. "colors": {
    4. "editor.background": "#1e1e1e",
    5. "editor.foreground": "#d4d4d4",
    6. "editorIndentGuide.background": "#404040",
    7. "editorRuler.foreground": "#333333",
    8. "activityBarBadge.background": "#007acc",
    9. "sideBarTitle.foreground": "#bbbbbb"
    10. },
    11. "tokenColors": "./Diner.tmTheme"
    12. }

    Tip: ColorSublime has hundreds of existing TextMate themes to choose from. Pick a theme you like and copy the Download link to use in the Yeoman generator or into your extension. It will be in a format like "https://raw.githubusercontent.com/Colorsublime/Colorsublime-Themes/master/themes/(name).tmTheme"

    Test a new color theme

    To try out the new theme, copy the generated theme folder to a new folder under your .vscode/extensions folder and restart VS Code.

    Open the Color Theme picker theme with File > Preferences > Color Theme and you can see your theme in the dropdown. Arrow up and down to see a live preview of your theme.

    select my theme

    After making changes to any theme file, it is necessary to reload VS Code with Reload Window.

    Publishing a Theme to the Extension Marketplace

    If you’d like to share your new theme with the community, you can publish it to the Extension Marketplace. Use the vsce publishing tool to package your theme and publish it to the VS Code Marketplace.

    Tip: To make it easy for users to find your theme, include the word “theme” in the extension description and set the Category to Theme in your package.json.

    We also have recommendations on how to make your extension look great on the VS Code Marketplace, see Marketplace Presentation Tips.

    Adding a new Icon Theme

    You can create your own icon theme from icons (preferably SVG) and from icon fonts. As example, check out the two built-in themes: Minimal and Seti.

    To begin, create a VS Code extension and add the iconTheme contribution point.

    1. "contributes": {
    2. "iconThemes": [
    3. {
    4. "id": "turtles",
    5. "label": "Turtles",
    6. "path": "./fileicons/turtles-icon-theme.json"
    7. }
    8. ]
    9. }

    The id is the identifier for the icon theme. It is currently only used internally. In the future, it might be used in the settings, so make it unique but also readable. label is shown in the icon theme picker drop-down. The path points to a file in the extension that defines the icon set. If your icon set name follows the *icon-theme.json name scheme, you will get completion support and hovers in VS Code.

    Icon Set File

    The icon set file is a JSON file consisting file icon associations and icon definitions.

    An icon association maps a file type (‘file’, ‘folder’, ‘json-file’…) to an icon definition. Icon definitions define where the icon is located: That can be an image file or also glyph in a font.

    Icon definitions

    The iconDefinitions section contains all definitions. Each definition has an id, which will be used to reference the definition. A definition can be referenced also by more than one file association.

    1. "iconDefinitions": {
    2. "_folder_dark": {
    3. "iconPath": "./https://code.visualstudio.com/assets/docs/extensions/Folder_16x_inverse.svg"
    4. }
    5. }

    This icon definition above contains a definition with the identifier _folder_dark.

    The following properties are supported:

    • iconPath: When using a svg/png: the path to the image.
    • fontCharacter: When using a glyph font: The character in the font to use.
    • fontColor: When using a glyph font: The color to use for the glyph.
    • fontSize: When using a font: The font size. By default, the size specified in the font specification is used. Should be a relative size (e.g. 150%) to the parent font size.
    • fontId: When using a font: The id of the font. If not specified, the first font specified in font specification section will be picked.

    File association

    Icons can be associated to folders, folder names, files, file extensions, file names and language ids.

    Additionally each of these associations can be refined for ‘light’ and ‘highContrast’ color themes.

    Each file association points to an icon definition.

    1. "file": "_file_dark",
    2. "folder": "_folder_dark",
    3. "folderExpanded": "_folder_open_dark",
    4. "folderNames": {
    5. ".vscode": "_vscode_folder",
    6. },
    7. "fileExtensions": {
    8. "ini": "_ini_file",
    9. },
    10. "fileNames": {
    11. "win.ini": "_win_ini_file",
    12. },
    13. "languageIds": {
    14. "ini": "_ini_file"
    15. },
    16. "light": {
    17. "folderExpanded": "_folder_open_light",
    18. "folder": "_folder_light",
    19. "file": "_file_light",
    20. "fileExtensions": {
    21. "ini": "_ini_file_light",
    22. }
    23. },
    24. "highContrast": {
    25. }
    • file is the default file icon, shown for all files that don’t match any extension, filename or language id. Currently all properties defined by the definition of the file icon will be inherited (only relevant for font glyphs, useful for the fontSize).
    • folder is the folder icon for collapsed folders, and if folderExpanded is not set, also for expanded folders. Icons for specific folder names can be associated using the folderNames property.
      The folder icon is optional. If not set, no icon will be shown for folder.
    • folderExpanded is the folder icon for expanded folders. The expanded folder icon is optional. If not set, the icon defined for folder will be shown.
    • folderNames associates folder names to icons. The key of the set is the folder name, not including any path segments. Patterns or wildcards are not supported. Folder name matching is case insensitive.
    • folderNamesExpanded associates folder names to icons for expanded folder. The key of the set is the folder name, not including any path segments. Patterns or wildcards are not supported. Folder name matching is case insensitive.
    • languageIds associates languages to icons. The key in the set is the language id as defined in the language contribution point. The language of a file is evaluated based on the file extensions and file names as defined in the language contribution. Note that the ‘first line match’ of the language contribution is not considered.
    • fileExtensions associates file extensions to icons. The key in the set is the file extension name. The extension name is a file name segment after a dot (not including the dot). File names with multiple dots such as lib.d.ts can match multiple extensions; ‘d.ts’ and ‘ts’. Extensions are compared case insensitive.
    • fileNames associates file names to icons. The key in the set is the full file name, not including any path segments. Patterns or wildcards are not supported. File name matching is case insensitive. A ‘fileName’ match is the strongest match, and the icon associated to the file name will be preferred over an icon of a matching fileExtension and also of a matching language Id.

    A file extension match is preferred over a language match, but is weaker than a file name match.

    The light and the highContrast section have the same file association properties as just listed. They allow to override icons for the corresponding themes.

    Font definitions

    The ‘fonts’ section lets you declare any number of glyph fonts that you want to use.
    You can later reference these font in the icon definitions. The font declared first will be used as the default if an icon definition does not specify a font id.

    Copy the font file into your extension and set the path accordingly.
    It is recommended to use WOFF fonts.

    • Set ‘woff’ as the format.
    • the weight property values are defined here.
    • the style property values are defined @font-face/font-style#Values">here.
    • the size should be relative to the font size where the icon is used. Therefore always use percentage.
    1. "fonts": [
    2. {
    3. "id": "turtles-font",
    4. "src": [
    5. {
    6. "path": "./turtles.woff",
    7. "format": "woff"
    8. }
    9. ],
    10. "weight": "normal",
    11. "style": "normal",
    12. "size": "150%"
    13. }
    14. ],
    15. "iconDefinitions": {
    16. "_file": {
    17. "fontCharacter": "\\E002",
    18. "fontColor": "#5f8b3b",
    19. "fontId": "turtles-font"
    20. }
    21. }

    Folder icons in File Icon Themes

    File Icon themes can instruct the File Explorer not to show the default folder icon (the rotating triangles or “twisties”) when the folder icons are good enough to indicate the expansion state of a folder. This mode is enabled by setting "hidesExplorerArrows":true in the File Icon theme definition file.


    Using TextMate Snippets

    You can also add TextMate snippets (.tmSnippets) to your VS Code installation using the yo code extension generator. The generator has an option New Code Snippets which lets you point to a folder containing multiple .tmSnippets files and they will be packaged into a VS Code snippet extension. The generator also supports Sublime snippets (.sublime-snippets).

    The final generator output has two files: an extension manifest package.json which has metadata to integrate the snippets into VS Code and a snippets.json file which includes the snippets converted to the VS Code snippet format.

    1. .
    2. ├── snippets // VS Code integration
    3. └── snippets.json // The JSON file w/ the snippets
    4. └── package.json // extension's manifest

    Copy the generated snippets folder to a new folder under your .vscode/extensions folder and restart VS Code.

    Sharing Your Snippets in the Marketplace

    Once you have created your snippets and tested them out, you can share them with the community.

    To do this, you need to create a snippet extension. If you’ve used the yo code extension generator, your snippet extension is ready to be published.

    If you want to share user snippets, you’ll need to package your snippet json file along with an extension manifest which has the necessary metadata to integrate the snippets into VS Code.

    Depending on your platform, your user snippets file is located here:

    • Windows %APPDATA%\Code\User\snippets\(language).json
    • Mac $HOME/Library/Application Support/Code/User/snippets/(language).json
    • Linux $HOME/.config/Code/User/snippets/(language).json

    where (language).json depends on the targeted language of the snippets (e.g. markdown.json for Markdown snippets). Create a new folder for your extension and copy your snippet file to a snippets subdirectory.

    Now add an extension manifest package.json file to the extension folder. The snippet extension manifest follows the structure defined in the Extension Manifest reference and provides a snippets contribution.

    Below is an example manifest for Markdown snippets:

    1. {
    2. "name": "DM-Markdown",
    3. "publisher": "mscott",
    4. "description": "Dunder Mifflin Markdown snippets",
    5. "version": "0.1.0",
    6. "engines": { "vscode": "0.10.x" },
    7. "categories": ["Snippets"],
    8. "contributes": {
    9. "snippets": [
    10. {
    11. "language": "markdown",
    12. "path": "./snippets/markdown.json"
    13. }
    14. ]
    15. }
    16. }

    Note that snippets need to be associated with a language identifier. This can be a language supported directly by VS Code or a language provided by an extension. Make sure the language identifier is correct.

    You then use the vsce publishing tool to publish the snippet extension to the VS Code Extension Marketplace.

    Tip: To make it easy for users to find your snippet, include the word “snippet” in the extension description and set the Category to Snippets in your package.json.

    We also have recommendations on how to make your extension look great on the VS Code Marketplace, see Marketplace Presentation Tips.


    Adding a New Language (Colorizer)

    Using the ‘code’ Yeoman generator, you can create an extension that adds syntax highlighting and bracket matching for a language to your VS Code installation.

    Central to language support is a TextMate language specification file (.tmLanguage) that describes the colorizer rules. The yeoman generator either takes an existing TextMate language specification file or lets you start with a fresh one.

    A good place to look for existing TextMate .tmLanguage files is on GitHub. Search for a TextMate bundle for the language you are interested in and then navigate to the Syntaxes folder. The ‘code’ Yeoman generator can import either .tmLanguage or .pList files. When prompted for the URL or file location, pass the raw path to the .tmLanguage file, for example https://raw.githubusercontent.com/textmate/ant.tmbundle/master/Syntaxes/Ant.tmLanguage. Make sure that the path points to the content of the file, not the HTML file showing the content.

    yo code language support

    The generator will prompt you for other information such a unique name (this should be unique to avoid clashing with other extensions) and the language name, aliases and file extensions. You also have to provide the top level scope name of the grammar. That scope name must match the scope name in the tmLanguage file.

    When the generator is finished, open the created folder in Visual Studio Code. Have a look at the generated language-configuration.json file: It contains more language settings such as the tokens used for comments and brackets. Make sure the configurations are accurate.

    Here is an example for a language with XML-like brackets:

    1. {
    2. "comments": {
    3. "lineComment": "",
    4. "blockComment": ["<!--", "-->"]
    5. },
    6. "brackets": [
    7. ["<", ">"]
    8. ],
    9. "autoClosingPairs": [
    10. ["<", ">"],
    11. ["'", "'"],
    12. ["\"", "\""]
    13. ],
    14. "surroundingPairs": [
    15. ["<", ">"],
    16. ["'", "'"],
    17. ["\"", "\""]
    18. ]
    19. }

    For more details check out the languages contribution point documentation.

    The generated vsc-extension-quickstart.md file also contains more information on how to run and debug your extension.

    To use your extension in your stable VS Code installation, copy the complete output folder to a new folder under your .vscode/extensions folder and restart VS Code. When you restart VS Code, your new language will be visible in the language specifier drop-down and you’ll get full colorization and bracket/tag matching for files matching the language’s file extension.

    select ant language

    Publishing Language Support to the Extension Marketplace

    If you’d like to share your new language with the community, you can publish it to the Extension Marketplace. Use the vsce publishing tool to package your extension and publish it to the VS Code Marketplace.

    Tip: To make it easy for users to find your language support, include the language name and words “language” or “language support” in the extension description and set the Category to Languages in your package.json.

    We also have recommendations on how to make your extension look great on the VS Code Marketplace, see Marketplace Presentation Tips.

    Add to your Language Support Extension

    When you’re adding a new language to VS Code, it is also great to add language snippets to support common editing actions. It is easy to combine multiple extensions like snippets and colorizers into the same extension. You can modify the colorizer extension manifest package.json to include a snippets contribution and the snippets.json.

    1. {
    2. "name": "language-latex",
    3. "description": "LaTeX Language Support",
    4. "version": "0.0.1",
    5. "publisher": "someone",
    6. "engines": {
    7. "vscode": "0.10.x"
    8. },
    9. "categories": [
    10. "Languages",
    11. "Snippets"
    12. ],
    13. "contributes": {
    14. "languages": [{
    15. "id": "latex",
    16. "aliases": ["LaTeX", "latex"],
    17. "extensions": [".tex"]
    18. }],
    19. "grammars": [{
    20. "language": "latex",
    21. "scopeName": "text.tex.latex",
    22. "path": "./syntaxes/latex.tmLanguage"
    23. }],
    24. "snippets": [
    25. {
    26. "language": "latex",
    27. "path": "./snippets/snippets.json"
    28. }
    29. ]
    30. }
    31. }

    Language Identifiers

    In VS Code, each language mode has a unique specific language identifier. That identifier is rarely seen by the user except in the settings, e.g. when associating file extensions to a language:

    1. "files.associations": {
    2. "*.myphp": "php"
    3. }

    Note that casing matters for exact identifier matching (‘Markdown’ != ‘markdown’)

    The language identifier becomes essential for VS Code extension developers when adding new language capabilities or when replacing a language support.

    Every language defines its id through the languages configuration point:

    1. "languages": [{
    2. "id": "java",
    3. "extensions": [ ".java", ".jav" ],
    4. "aliases": [ "Java", "java" ]
    5. }]

    Language supports are added using the language identifier:

    1. "grammars": [{
    2. "language": "groovy",
    3. "scopeName": "source.groovy",
    4. "path": "./syntaxes/Groovy.tmLanguage"
    5. }],
    6. "snippets": [{
    7. "language": "groovy",
    8. "path": "./snippets/groovy.json"
    9. }]

    New identifier guidelines

    When defining a new language identifier, use the following guidelines:

    • Use the lowercased programming language name.
    • Search for other extensions in the Marketplace to find out if a language identifier has already been used.

    You can find a list of known language identifiers in the language identifier reference.

    Next Steps

    If you’d like to learn more about VS Code extensibility, try these topics:

    • Extending Visual Studio Code - Learn about other ways to extend VS Code
    • Additional Extension Examples - Take a look at our list of example extension projects.

    Common Questions

    Q: What parts of VS code can I theme with a custom color theme?

    The VS Code color themes affect the editor input area (text foreground, background, selection, lineHighlight, caret, and the syntax tokens) as well as some of the custom UI (see the list in Creating a Theme). When contributing a theme, you also specify a base theme: light (vs), dark (vs-dark) and high contrast (hc-black). The base theme is used for all other areas in the workbench such as the File Explorer. Base themes are not customizable or contributable by extensions.

    Q: Is there a list of scopes that I can use in my custom color theme?

    VS Code themes are standard TextMate themes and the tokenizers used in VS code are well established TextMate tokenizers, mostly maintained by the community and in use in other products.

    To learn about what scopes are used where, check out the TextMate documentation and this useful blog post. A great place to examine themes is here.

    Q: I created a snippets extension but they aren’t showing up in the VS Code editor?

    A: Be sure you have correctly specified the language identifier for your snippet (e.g. markdown for Markdown .md files, plaintext for Plain Text .txt files). Also verify that the relative path to the snippets json file is correct.

    Q: Can I add more file extensions to my colorizer?

    A: Yes, the yo code generator provides the default file extensions from the .tmLanguage file but you can easily add more file extensions to a languages contribution extensions array. In the example below, the .asp file extension has been added to the default .asa file extension.

    1. {
    2. "name": "asp",
    3. "version": "0.0.1",
    4. "engines": {
    5. "vscode": "0.10.x"
    6. },
    7. "publisher": "none",
    8. "contributes": {
    9. "languages": [{
    10. "id": "asp",
    11. "aliases": ["ASP", "asp"],
    12. "extensions": [".asa", ".asp"]
    13. }],
    14. "grammars": [{
    15. "language": "asp",
    16. "scopeName": "source.asp",
    17. "path": "./syntaxes/asp.tmLanguage"
    18. }]
    19. }
    20. }

    Q: Can I add more file extensions to an existing colorizer?

    A: Yes. To extend an existing colorizer, you can associate a file extension to an existing language identifier with the files.associations setting. IntelliSense will show you the list of currently available language ids.

    For example, the setting below adds the .mmd file extension to the markdown colorizer:

    1. "files.associations": {
    2. "*.mmd": "markdown"
    3. }

    Q: What if I want to completely override an existing colorizer?

    A: Yes. You override the colorizer by providing a new grammars element for an existing language id. Also, add a extensionDependencies attribute that contains the name of the extension that defines the grammar that you want to replace.

    1. {
    2. "name": "override-xml",
    3. "version": "0.0.1",
    4. "engines": {
    5. "vscode": "0.10.x"
    6. },
    7. "publisher": "none",
    8. "extensionDependencies": [
    9. "xml"
    10. ],
    11. "contributes": {
    12. "grammars": [{
    13. "language": "xml",
    14. "scopeName": "text.xml",
    15. "path": "./syntaxes/BetterXML.tmLanguage"
    16. }]
    17. }
    18. }