Samples

Pick from some of these sample files and add as .editorconfig in your project. Some are smaller pieces that can be combined as you mix languages.

Own

I find this a good starting point for adding a file to a project and then refining it.

Focus on 2 spaces

Set the default to 2 spaces and then override certain files to 4 spaces. For example for a Node project like node-project-template .editorconfig.

  • .editorconfig
      root = true
    
      [*]
      indent_style = space
      indent_size = 2
    
      [*.{html,md}]
      indent_size = 4
    
      [Makefile]
      indent_style = tab
      indent_size = 4
    

Even if most of the files in a repo are markdown or HTML (needing 4 spaces), I find this setup elegant and useful for most of my projects. Since most file types actually do use 2 spaces (shell, JS, Ruby, CSS, JSON, YAML), but they get covered by * without having to name separately.

Notes

  • The Makefile part is clearer on its own. Take it out if you don’t use a Makefile.
  • For Python projects, use py in the html section.
  • Set the max line length to 80 or 100, as you like. I can’t see a difference in JS files. But I can see an HTML file wrapping differently as I change this to short values like 10.
      max_line_length = 80
    

Focus on 4 spaces

If most of your file types use 4 spaces, use this. For example for a Python project like py-project-template .editorconfig.

  • .editorconfig
      root = true
    
      [*]
      indent_style = space
      indent_size = 4
    
      [*.{js,json}]
      indent_size = 2
    
      [Makefile]
      indent_style = tab
    

If your 2-space block below starts growing to handle shell, YAML and more, consider using the 2-space section covered above.

Shared projects

If you have other people contributing your project with a different choice of IDE, IDE configuration or OS, then you may want to extend these settings to include this:

  • .editorconfig
      [*]
      # ...
      end_of_line = lf
      charset = utf-8
      trim_trailing_whitespace = true
      insert_final_newline = true
    

Options for end_of_line:

  • lf - for Linux.
  • cr - for old macOS.
  • crlf - for Windows.

Python

  • .editorconfig
      [*.py]
      indent_style = space
      indent_size = 4
    

Optionally set the size. Though, PyLint CLI, the VS Code extensions and VS Code settings handle this fine without setting this.

max_line_length = 80

Two spaces

If needed, also add this to the samples below.

indent_style = space

Configs

[*.{json,yml}]
indent_size = 2

Shell

[*.sh]
indent_size = 2

JavaScript

[*.js]
indent_size = 2
[*.{js,ts}]
indent_size = 2
[*.{js,json}]
indent_size = 2

Ruby

[*.rb]
indent_size = 2

Tabs

Make

Without EditorConfig settings, VS Code knows that a Makefile must have tabs only (running a file with spaces gives an error).

But, if you use a * glob change it in your EditorConfig, then also need a section Makefile. The size is just for visibility in the IDE, not how it is stored.

[Makefile]
indent_style = tab
indent_size = 4

See section at the start of this page on mixing Makefile with other extensions.

GraphQL

[*.gql]
indent_style = tab
indent_size = 4

Markdown

[*.md]
indent_style = space
indent_size = 4

HTML

[*]
charset = utf-8
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

For Jekyll markdown support, you could add this. But only if you use the double space at the end of a line to signify a line break.

[*.md]
trim_trailing_whitespace = false

Jekyll

For a Jekyll project.

Using HTML, Markdown, JSON, YAML, JS, Ruby, CSS, SCSS and Makefile. Only the first two are actually 4 spaces, so a glob for 2 spaces makes sense.

  • .editorconfig
      root = true
    
      [*]
      indent_style = space
      indent_size = 2
    
      [*.{md,html}]
      indent_size = 4
    
      [Makefile]
      indent_style = tab
      indent_size = 4
    

Generated

The VS Code EditorConfig extension can generate a file for you if you right-click on a file on a folder. It won’t create one if one already exists. You could also create them them at multiple levels if you need to.

Here is output generated by the extension. Perhaps too verbose. Also I had to tweak the whitespace ones as below and VS Code does not honor those - it has its own IDE settings.

  • .editorconfig
      root = true
    
      [*]
      indent_style = space
      indent_size = 4
      charset = utf-8
      trim_trailing_whitespace = true
      insert_final_newline = true
    

Generic

Some devs like to set this before going into more formats. I don’t see it as needed. The final newline I handle in my global IDE settings, though other users might not.

  • .editorconfig
      [*]
      charset = utf-8
      insert_final_newline = true
    

Demo

Based on the sample on the EditorConfig homepage.

  • .editorconfig
      # EditorConfig is awesome: https://EditorConfig.org
    
      # top-most EditorConfig file
      root = true
    
      # Unix-style newlines with a newline ending every file
      [*]
      end_of_line = lf
      insert_final_newline = true
    
      # Matches multiple files with brace expansion notation
      # Set default charset
      [*.{js,py}]
      charset = utf-8
    
      # 4 space indentation
      [*.py]
      indent_style = space
      indent_size = 4
    
      # Tab indentation (no size specified)
      [Makefile]
      indent_style = tab
    
      # Indentation override for all JS under lib directory
      [lib/**.js]
      indent_style = space
      indent_size = 2
    
      # Matches the exact files either package.json or .travis.yml
      [{package.json,.travis.yml}]
      indent_style = space
      indent_size = 2