Reputation: 5169
I use VS Code as editor. We have a .editorconfig
file with format configs within. We all use in our editors the extenion EditorConfig to format our HTML and CSS general. I have installed the extension EditorConfig for VS Code from here: https://github.com/editorconfig/editorconfig-vscode
Our .editorconfig
file looks like this:
# This is the top-most .editorconfig file (do not search in parent directories)
root = true
### All files
[*]
# Force charset utf-8
charset = utf-8
# Indentation
indent_style = tab
indent_size = 4
# line breaks and whitespace
insert_final_newline = true
trim_trailing_whitespace = true
# end_of_line = lf
### Frontend files
[*.{css,scss,less,js,json,ts,sass,php,html,hbs,mustache,phtml,html.twig}]
### Markdown
[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
### YAML
[*.yml]
indent_style = space
indent_size = 2
### Specific files
[{package,bower}.json]
indent_style = space
indent_size = 2
I can't find any keyboard shortcut, setting or else. How to get my extension do the stuff from the .editorconfig
file?
Upvotes: 73
Views: 53179
Reputation: 2729
I have the same issue that VSCode cannot pick up the my .editorconfig
config for my SQL scripts.
Turns out I missed the *
character to wildcard all SQL files in my directory.
Here is an example of my .editorconfig
file.
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
end_of_line = lf
insert_final_newline = true
# Indentation override for all SQL files
# make sure you add the * to wildcard all your files.
[*.sql]
indent_style = space
indent_size = 2
Hope it helps.
Upvotes: 0
Reputation: 2588
To me, none of above was the answer. In my case, I had a couple of lines like this:
[*.{json}]
indent_style = space
indent_size = 2
And for some reason, VS Code didn't understand that. But if I changed it to either:
[*.json]
indent_style = space
indent_size = 2
or
[*.{json,someotherformat}]
indent_style = space
indent_size = 2
Then it worked. ¯\_(ツ)_/¯
Upvotes: 0
Reputation: 5169
The problem I had was that I added the extension EditorConfig.EditorConfig
to my VS Code, but didn't install the npm package for it. It's not enough to add the extension only to your VS Code, you have also to install the package, so it could run.
I installed the npm package globally like this: npm install -g editorconfig
After that I added the extension and enabled it. Now it works perfectly.
Required npm package: https://www.npmjs.com/package/editorconfig
Required VS Code extension: https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
Upvotes: 106
Reputation: 7800
Late 2022 I have to set up the VS Code 2 spaces indentation per custom file extenstion (*.openapi.json
) keeping the other .json
files indentation 4 spaces.
The other solutions here did not work as written. Here is how I managed to make it work.
.editorconfig
file add root = true
at the file top (read what it does).Editor: Detect Indentation
rule (Ctrs+Shift+P
-> Preferences: Open user settings -> search for detect indentation
-> uncheck the checknbox)..editorconfig
should look as follows. Note that unlike in the Editor Config documentation, the extension wildcards should have an asterisk *
. More details here.
root = true
[*.openapi.json]
indent_style = space
indent_size = 2
Now you get it working.
There is another way to assign different formatting to files with custom extensions via Prettier, its VS Code plugin and Prettier config overrides
section. Following the given docs it is pretty easy to get set up.
Upvotes: 7
Reputation: 2470
I got the error "Extension EditorConfig cannot format ..." (at the bottom of the IDE, disappearing after some seconds).
Changing the preferences (ctrl+,
, search for format
and set the Editor:DefaultFormatter
to None
(instead of EditorConfig
)) solved the problem for me (and yes, funny enough, this indeed did enable the usage of the .editorconfig
- not any other formatting rules)
Codium: 1.60.2 with EditorConfig: v0.16.6
Upvotes: 1
Reputation: 143
In addition all of the above I also needed to turn on the Editor Format on Save option.
{
"editor.formatOnSave": true
}
Upvotes: 12