How to make VS Code treat a file extensions as a certain language?

Visual Studio-CodeVscode Settings

Visual Studio-Code Problem Overview


Or is there a way to switch the current file's language so that the syntax is highlighted correctly?

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

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Update

Please note that JoelAZ's answer is much easier and results in the same setting changes! The answer below is still valid, just more steps & more fuss.

Old answer

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 (or View -> Command Palette from the menu) and then type settings JSON. Choose Preferences: Open Settings (JSON) to open your settings.json.

To find the proper language ID, use Ctrl+Shift+P (or View -> Command Palette from the menu) and then type Change Language Mode. You can see the language ID in the list, e.g. type docker to find the language ID for Docker files (dockerfile). In the first entry in the example above, .thor is the file ending, ruby is the language ID.

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.

Solution 2 - Visual Studio-Code

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 the option "Configure File Association for 'x' " (whatever file type - see image attached). Selecting this gives you the option to choose the language and will then make 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.

Solution 3 - Visual Studio-Code

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.

Solution 4 - Visual Studio-Code

eg:

// .vscode/settings.json in workspace

{
  "files.associations": {
    "*Container.js": "javascriptreact",
    "**/components/*/*.js": "javascriptreact",
    "**/config/routes.js": "javascriptreact"
  }
}

Solution 5 - Visual Studio-Code

This works for me.

enter image description here

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

Solution 6 - Visual Studio-Code

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)

Solution 7 - Visual Studio-Code

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

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

Solution 8 - Visual Studio-Code

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"]
        }]
    }
}

Solution 9 - Visual Studio-Code

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.

Solution 10 - Visual Studio-Code

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn DeevView Question on Stackoverflow
Solution 1 - Visual Studio-CodeJosienView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeJoelAZView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeJohn DeevView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeB.MaView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeIsura AmarasingheView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeFrank NockeView Answer on Stackoverflow
Solution 7 - Visual Studio-Codetonda13View Answer on Stackoverflow
Solution 8 - Visual Studio-CodeMicMroView Answer on Stackoverflow
Solution 9 - Visual Studio-Codedirkk0View Answer on Stackoverflow
Solution 10 - Visual Studio-CodemastaziView Answer on Stackoverflow