visual-studio-codeenvironment-variablessyntax-highlighting.env

VSCode doesn't syntax highlight all .env files


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?


Solution

  • 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.