Visual Studio Code (VSCode) has a tons of file format supported. Based on the filename and extension, VSCode finds language mode. Often, provided sensible defaults are okay. But what if you want different language mode for different folders?
VSCode has in its .vscode/settings.json file a files.associations object. It's a map of glob pattern to language mode. See supported glob pattern syntax in VSCode docs.
The simplest variant. All files need different language mode.
For example, when styling with Tailwind CSS and you install Tailwind CSS extension for VSCode, you want to change all *.css files to tailwindcss mode instead of plain css.
{ "files.associations": { "*.css": "tailwindcss" } ... }
It's also not uncommon, that files under certain subfolder needs different language mode.
For example, when theming Sphinx documentation, you create a lot of HTML files which are not pure HTML, but Jinja HTML. I install Better Jinja extension which provides jinja-html mode.
"source/_templates/*.html": "jinja-html``
Safer is use ** meaning anywhere - at the path or any subpath.
{ "files.associations": { "*.css": "tailwindcss", "source/_templates/**/*.html": "jinja-html` } ... }
Now the tricky part which I found during preparing starter template new Sphinx Themes based on Cookiecutter template generator.
Cookiecutter projects uses folder named {{ cookiecutter.project_slug }}. Only under it I want to change association, e.g. for *.py from py to jinja-py.
Escaping glob pattern special characters (like *, {, etc.) isn't described in the VSCode docs, but it's possible.
Firstly, these will NOT work:
The escape the backslash will do the trick ("\{\{ cookiecutter.project_slug \}\}/**/*.css": "tailwindcss"). For example:
{ "files.associations": { // CSS files are Tailwind "\{\{ cookiecutter.project_slug \}\}/**/*.css": "tailwindcss", // HTML files are Jinja "\{\{ cookiecutter.project_slug \}\}/**/*.html": "jinja-html", "\{\{ cookiecutter.project_slug \}\}/**/*.py": "jinja-py", "\{\{ cookiecutter.project_slug \}\}/**/*.toml": "jinja-toml", "\{\{ cookiecutter.project_slug \}\}/**/*.json": "jinja-json" }, ... }
The above is the detailed content of VSCode language mode association for subfolders and special characters folders. For more information, please follow other related articles on the PHP Chinese website!