VSCode doesn't syntax highlight all .env files

121 Views Asked by At

VSCode being strange about environment files. Even though it shows the correct file icon for the example.env as a dotenv file in the file view/tab, only .env is syntax-highlighted correctly when opening the file.

.env (correct):

enter image description here

example.env (no syntax highlighting):

enter image description here

What's the best way to get all .env files syntax highlighted properly in VSCode?

3

There are 3 best solutions below

1
M69k65y On BEST ANSWER

VS Code has a way to manually define the file association of certain extension types.

To do so for .env files:

  1. Open Settings (Ctrl + , or File > Preferences > Settings.
  2. Search for file asssociation.
  3. You should see Files: Associations.
  4. In the table under this setting, click on Add Item.
  5. Under Item, add *.env and under Value, add properties.
  6. Click on OK once you're done.

You can also do the same for .env.example files.

Switching back to your file, you should see the syntax highlighted.

1
Usman Latif On

I guess vscode env formetter extension is build to get highlights all files starting with ".env" not anything before the name in your case "example.env". I guess it is treating a separate language file which definations is not installed. If you want to have separate env for local and production you can name them as ".env.local" for local and ".env" for production. Vscode will hightlights syntax for these.

0
ffrosch On

I use the extension DotENV. Works flawlessly for me.