630

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.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
John Deev
  • 7,921
  • 3
  • 12
  • 10

11 Answers11

765

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:

// settings.json
// 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 User 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.

Josien
  • 13,079
  • 5
  • 36
  • 53
  • 8
    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
  • 4
    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
  • 2
    @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
  • 1
    @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
  • @Alex that's essential information, I did an edit suggestion to add it to the answer. – Carl Aug 04 '21 at 12:01
  • 1
    @alex and Carl, there is a much easier way using the "configure file association" menu which does not need any ID names at all. See the full answer with steps here: https://stackoverflow.com/a/51228725/3307796 – JoelAZ Sep 26 '21 at 13:39
  • 1
    @JoelAZ sorry for noticing your answer only now - it's a much nicer solution indeed! I've added a link to your answer to this one, I hope your answer will be upvoted to become the top answer. – Josien Sep 29 '21 at 18:57
  • No worries fren, we are all here to learn smth or to help someone. I appreciate your update and hope this keeps on helping people. – JoelAZ Oct 12 '21 at 20:14
  • 1
    You're a class act @Josien, linking to a better answer! – Zephaniah Grunschlag Jan 01 '22 at 16:55
  • @ZephaniahGrunschlag Honestly, I upvoted only because of mentioning a better answer. It saves a lot of time since I do not need to start parsing the answer. Instead, I jump to a better solution. Kudos to @Josien! – Péter Szilvási Apr 21 '23 at 12:26
  • Despite there being a generally accepted "better" answer, this one is still useful or required in certain scenarios. For example, I needed to add `"Makefile.*": "makefile"` which cannot be done with Joel's answer. – Seth Falco May 19 '23 at 06:54
  • To associates Makefile.suse and Makefile.debian to the makefile editor, use "Makefile*": "makefile" in "files.associations": { block. – Aubin Jun 01 '23 at 06:04
371

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.

Josien
  • 13,079
  • 5
  • 36
  • 53
JoelAZ
  • 3,875
  • 1
  • 11
  • 15
  • 5
    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
  • 1
    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
  • 1
    @VincentYin exactly right. This is the easiest of the solutions for that reason and the same reason I found this solution in first place - didn't know what the lang. identifier was that I needed. Glad ppl finding it helpful. – JoelAZ Jul 03 '21 at 16:32
  • 1
    I tried the answer from @Josien with the most vote, for `.gpx` files to be treated as `.xml` files, but VSC doesn't syntax highlight the file still after doing that. But this solution above works well – Stefanie Gauss Aug 10 '21 at 18:25
  • This worked for me. Any idea how to make this work for code formatting too, not only for syntax highlighting? – bit Jul 24 '22 at 20:16
  • https://code.visualstudio.com/docs/languages/html#_formatting this provides the code formatting I needed. – bit Jul 24 '22 at 20:23
162

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.

pjpscriv
  • 866
  • 11
  • 20
John Deev
  • 7,921
  • 3
  • 12
  • 10
63

eg:

// .vscode/settings.json in workspace

{
  "files.associations": {
    "*Container.js": "javascriptreact",
    "**/components/*/*.js": "javascriptreact",
    "**/config/routes.js": "javascriptreact"
  }
}
B.Ma
  • 839
  • 7
  • 9
  • 3
    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
24

This works for me.

enter image description here

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

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,460
  • 28
  • 34
12

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
  • 167
  • 1
  • 4
11

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
  • 31,714
  • 9
  • 78
  • 100
tonda13
  • 127
  • 1
  • 4
11

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
  • 6,242
  • 7
  • 41
  • 65
Frank N
  • 9,625
  • 4
  • 80
  • 110
8

The easiest way:

  1. Go to File > Preferences > Settings
  2. Search "File associations"
  3. Click on "Add Item"
  4. Add your extension (*.ext) and the preferred language.

That's all.

enter image description here

Guscie
  • 2,278
  • 2
  • 26
  • 33
  • Biggest problem with this method is you have to guess what the exact language string needs to be to enter into the Value column. For example, enter "HTML" and this probably won't work as expected. – gb96 May 17 '23 at 23:46
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
  • 3,097
  • 2
  • 14
  • 45
mastazi
  • 1,623
  • 26
  • 41