webta.st.ic
webta.st.ic

Reputation: 5169

EditorConfig extension for VS Code does nothing

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

Answers (6)

Eric
Eric

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

Zeth
Zeth

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

webta.st.ic
webta.st.ic

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

Valentine Shi
Valentine Shi

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.

  1. Install the node package (EditorConfig JavaScript Core) and the VS Code extension (EditorConfig for VS Code) as per @webta.st.ic answer here.
  2. In your .editorconfig file add root = true at the file top (read what it does).
  3. In VS Code switch off the automatic indentation detection: Editor: Detect Indentation rule (Ctrs+Shift+P -> Preferences: Open user settings -> search for detect indentation -> uncheck the checknbox).
  4. Your .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

dr0i
dr0i

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

Jordon Davidson
Jordon Davidson

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

Related Questions