412

Or is there a way to switch the current file's language to use syntax highlight feature?

For example, *.jsx actually uses JavaScript but VS Code doesn't recognize it.

Gama11
  • 24,825
  • 7
  • 56
  • 81
John Deev
  • 5,471
  • 3
  • 10
  • 10

10 Answers10

600

In Visual Studio Code, you can add persistent file associations for language highlighting to your settings.json file like this:

// Place your settings in this file to overwrite the default settings
{
  "some_setting": custom_value,
  ...

  "files.associations": {
    "*.thor": "ruby",
    "*.jsx": "javascript",
    "Jenkinsfile*": "groovy"
  }
}

You can use Ctrl+Shift+p and then type settings JSON. Choose Preferences: Open Settings (JSON) to open your settings.json.

The Files: Associations feature was first introduced in Visual Studio Code version 1.0 (March 2016). Check the available wildcard patterns in the release notes and the known language strings in the documentation.

Josien
  • 10,821
  • 5
  • 35
  • 48
  • 6
    The value for the association needs to be the ID of the language/plugin, not the name. For example the VBScript plugin I installed, the ID is vbs. "*.vms" : "vbs" gets the custom extension to associate properly. – Matt M Mar 01 '18 at 16:40
  • Just faced a similar issue. If adding a file association does not seem to work, make sure you don't have a `.editorconfig` file close, or align the configurations between VSCode and `.editorconfig`, the latter will take precedence – RecuencoJones Jun 22 '18 at 08:38
  • 1
    You can also put these settings in a project specific `${projectdir}/.vscode/settings.json` file. – Jason Jan 17 '20 at 22:27
  • I added .ps1 as asscociation, and enabled the ps extension, but no syntax complete, I use windows 10 and vscode 1.51.1 built 14 days ago.. – Timo Nov 24 '20 at 13:20
  • ctrl + shit + p doesn't do anything for me. what is the editor menu path? – TatiOverflow Feb 07 '21 at 19:38
  • @TatiOverflow From the menu it is *View* -> *Command Palette*. (But you might wanna try the Shift button instead of the shit button, that one doesn't do much for me either.) – Josien Feb 09 '21 at 07:50
  • @MattM But to find the proper ID name you have to: Ctrl+Shift+P (or Cmd on Mac) and select "Change Language Mode" and in the list that is displayed you will the the ID of the language (ex: "dockerfile" for Docker or "bat" for Batch) – Alex Apr 19 '21 at 06:14
135

Hold down Ctrl+Shift+P (or cmd on Mac), select "Change Language Mode" and there it is.

But I still can't find a way to make VS Code recognized files with specific extension as some certain language.

CenterOrbit
  • 5,371
  • 1
  • 22
  • 34
John Deev
  • 5,471
  • 3
  • 10
  • 10
109

The easiest way I've found for a global association is simply to ctrl+k m (or ctrl+shift+p and type "change language mode") with a file of the type you're associating open.

In the first selections will be "Configure File Association for 'x' " (whatever file type - see image attached) Selecting this makes the filetype association permanent

enter image description here

This may have changed (probably did) since the original question and accepted answer (and I don't know when it changed) but it's so much easier than the manual editing steps in the accepted and some of the other answers, and totaly avoids having to muss with IDs that may not be obvious.

JoelAZ
  • 1,251
  • 1
  • 8
  • 12
  • 2
    Thanks - this worked for me. It was not clear when manually editing the `settings.json` file what the extension ID should have been, but this method sorted it! – ccbunney Oct 15 '19 at 10:45
  • 1
    You're welcomed @ccbunney, glad it helps. That was exactly the same problem I had - and I never did figure out the extension ID I needed, lol. Anyway, I was real glad to find this solution for myself and it's cool that it's helping other ppl! :D – JoelAZ Dec 03 '19 at 01:38
  • Just curios, never heard of **.ps1data** as seen in your img. – Timo Nov 24 '20 at 13:18
  • @timo ¯\_(ツ)_/¯ Me neither and I never really noticed before yet there it is ... Perhaps a result of a typo during some earlier tweaking, idk. Sry can't help more. – JoelAZ Nov 25 '20 at 03:59
  • There is a significant advantage to this solution. Other solutions require you to know in advance what the language identifier is. That is OK for predefined values such as `java` as listed in https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers, but we'll be at a loss for VS Code extensions installed. E.g., I installed an Apache extension for Apache `*.conf` files, and the language identifier turns out to be `apacheconf` -- how'd I guess that? I found that out by following the above solution. Then I can edit `settings.json` to further hack the mapping. – Vincent Yin May 16 '21 at 19:59
45

eg:

// .vscode/settings.json in workspace

{
  "files.associations": {
    "*Container.js": "javascriptreact",
    "**/components/*/*.js": "javascriptreact",
    "**/config/routes.js": "javascriptreact"
  }
}
B.Ma
  • 649
  • 6
  • 9
  • 2
    Nice. This comes in handy if you have the same extension, but different language parsers based on path. E.g. you can have yml to handle Concourse pipelines in one folder and Ansible files in another. – Christian Maslen Jul 17 '17 at 23:16
  • I'd upvote this twice if I could. Been trying to persist the syntax for my Nanoc layouts and partials with an .html extension, this solved it: `"**/layouts/**/*.html": "erb"` - worth noting that the VSCode "language mode" dropdown shows the actual name of the syntax highlighter in brackets e.g. `Ruby ERB (erb)` – Dave Everitt Dec 31 '19 at 18:41
  • This works for me, however as a side issue, `equinusocio.vsc-material-theme-icons` doesn't properly map icons to these types. – ux.engineer Aug 20 '20 at 11:15
21

This works for me.

enter image description here

{
"files.associations": {"*.bitesize": "yaml"}
 }
12

I found solution here: https://code.visualstudio.com/docs/customization/colorizer

Go to VS_CODE_FOLDER/resources/app/extensions/ and there update package.json

Gama11
  • 24,825
  • 7
  • 56
  • 81
tonda13
  • 129
  • 1
  • 4
12

This, for example, will make files ending in .variables and .overrides being treated just like any other LESS file. In terms of code coloring, in terms of (auto) formatting. Define in user settings or project settings, as you like.

(Semantic UI uses these weird extensions, in case you wonder)

ifconfig
  • 4,454
  • 7
  • 33
  • 54
Frank Nocke
  • 7,493
  • 3
  • 58
  • 89
8

Following the steps on https://code.visualstudio.com/docs/customization/colorizer#_common-questions worked well for me:

To extend an existing colorizer, you would create a simple package.json in a new folder under .vscode/extensions and provide the extensionDependencies attribute specifying the customization you want to add to. In the example below, an extension .mmd is added to the markdown colorizer. Note that not only must the extensionDependency name match the customization but also the language id must match the language id of the colorizer you are extending.

{
    "name": "MyMarkdown",
    "version": "0.0.1",
    "engines": {
        "vscode": "0.10.x"
    },
    "publisher": "none",
    "extensionDependencies": [
        "markdown"
    ],
    "contributes": {
        "languages": [{
            "id": "markdown",
            "aliases": ["mmd"],
            "extensions": [".mmd"]
        }]
    }
}
MicMro
  • 107
  • 1
  • 3
5

I have followed a different approach to solve pretty much the same problem, in my case, I made a new extension that adds PHP syntax highlighting support for Drupal-specific files (such as .module and .inc): https://github.com/mastazi/VS-code-drupal

As you can see in the code, I created a new extension rather than modifying the existing PHP extension. Obviously I declare a dependency on the PHP extension in the Drupal extension.

The advantage of doing it this way is that if there is an update to the PHP extension, my custom support for Drupal doesn't get lost in the update process.

xilpex
  • 2,716
  • 2
  • 10
  • 38
mastazi
  • 1,279
  • 21
  • 34
1

You can add the md.html extension to your settings.json file associations to enable markdown formatting for markdeep files like this:

    "files.associations": {
        "*.md.html": "markdown"
    },

The settings.json file lives in various locations, depending on your OS. For instance it is ~/Library/Application Support/Code/User/settings.json in macOS. You can open and edit it with Ctrl+Shift+p in VS Code.

dirkk0
  • 2,130
  • 23
  • 32