Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorConfig: applying rules to multiple, specific folders

Tags:

editorconfig

Let's say I have a project with the following folders:

/
  /folder1
  /folder2
  /folder3
  /folder4
  /folder5

Question

Is there a way to have my .editorconfig file at root (/) level but only apply the same rules to (all files within) folder1, folder3 and folder5 but not the other two?

What I could do (but doesn't seem ideal)

  • Have one copy of the .editorconfig file within folder1, folder3 and folder5. But duplicating it seems very ugly/inconvenient.
  • Create an .editorconfig within the root folder with all the rules, then another one with just root = true within all the "excluded" folders (folder2 and folder4). It's better, but not ideal because we have to remember to do it on every single folder rules should not be applied to (in reality, there may be dozens of them).

What I would like to (be able to) do

Create an .editorconfig at root level, and list the folders rules should be applied to. I didn't find a way to do something like this:

[/folder1/**]
[/folder3/**]
[/folder5/**]
end_of_line = lf
insert_final_newline = true

Invalid because you cannot combine sections like that.

Or, using the {s1,s2,s3} format:

[{/folder1/**, /folder3/**, /folder5/**}]
end_of_line = lf
insert_final_newline = true

But it doesn't seem to be valid either (only simple file name patterns seem allowed).

Is there any other way to achieve this? I didn't find anything on the docs or elsewhere, but I may have missed something obvious.

like image 845
Jeto Avatar asked Jan 18 '26 12:01

Jeto


2 Answers

It was introduced to EditorConfig Core 0.11.0 in 2013. The way to do it is as follows (Source):

[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

I tested the following configuration in Visual Studio Code and can confirm it works.

root = true

[{packages/**,plugins/**}]
indent_size = 8

If this does not work for you, it likely depends on the implementation/client. It's best to make a feature request to support the feature.

like image 80
Seth Falco Avatar answered Jan 21 '26 08:01

Seth Falco


You can apply rules to a directory like this:

[foobar/*]
trim_trailing_newline = false

If you want to apply rules to all files within a directory and its subdirectories recursively, you can do so like this:

[foobar/**/*]
trim_trailing_newline = false

To check to see if your rules are being applied as expected, run the editorconfig command:

$ editorconfig /home/user/project/foobar/example/example.txt
trim_trailing_whitespace=false

If you want to unset a property, use the keyword unset:

[foobar/**/*]
trim_trailing_newline = unset
like image 27
Flimm Avatar answered Jan 21 '26 08:01

Flimm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!