EditorConfig: The Unsung Hero of Consistent Code Formatting

Have you ever opened a codebase and found a mishmash of tabs and spaces, inconsistent indentation, or weird line endings? This is where EditorConfig can save the day.

Have you ever opened a codebase and found a mishmash of tabs and spaces, inconsistent indentation, or weird line endings?

This is where EditorConfig can save the day.

EditorConfig is a simple, language-agnostic way to maintain consistent coding styles across different editors and IDEs. It doesn’t replace linters or code formatters — instead, it ensures everyone on your team has the same basic formatting settings, no matter what tool they’re using.

Some editors need extra configuration or a plugin to take advantage of this tool, we will cover VS Code and PHPStorm in this article.

What is EditorConfig?

EditorConfig is a file that lets you define and enforce basic code formatting rules like:

  • Indentation style and size
  • Line endings
  • Character encoding
  • Final newline insertion
  • Trimming trailing whitespace

These rules are defined in a file named .editorconfig, usually placed in the root of your project.

What does it look like?

Here’s a sample .editorconfig for a typical PHP/Laravel project:

# Top-level .editorconfig file
root = true

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

[*.md]
trim_trailing_whitespace = false

[*.blade.php]
indent_size = 2

The breakdown for this file is as follows:

root = true tells EditorConfig that this is the top level configuration file, if you omit this line, it may search parent directories for other EditorConfig files and merge them.

The next section, [*], is a wild card definition and will match and apply the code style rules to all files, which in our example will:

  • Ensure UTF-8 encoding is used
  • The end of line marker is Unix style (LF)
  • Ensure a new line is added to the end of each file
  • Ensure extra spaces (white space) at the end of lines are removed
  • Force spaces instead of tabs for indentation
  • Set the indent size to 4 spaces

The next section, [*.md], will apply to only to Markdown files, here we want to override the rule which removes extra spaces at the end of lines, this is because in Markdown, double space at the end of a line will force a line break.

The final section, [*.blade.php], will only apply to Blade files and will override the indent size to 2, rather than 4.

Using in VS Code

To take advantage of EditorConfig in VS Code, you need to install the EditorConfig for VS Code plugin.

You may need to restart your editor, but once installed, it should pickup your .editorconfig file and start applying the rules upon saving files.

Using in PHPStorm

In PHPStorm, you do not need an additional plugin, you just need to enable it in the setting, to do this go to Settings -> Editor -> Code Style

Then ensure that Enable EditorConfig support is checked.

Wrapping Up

EditorConfig is one of those small dev tools that quietly improves your workflow in the background, especially in teams or open-source projects where multiple people using a range of operating systems touch the files.

It's not flashy or hard to setup, but it's powerful and once you start using it, you can enjoy a nice cup of tea with the time you saved from having to reset the indentation and line endings after a git pull.

Enjoyed this post?

If so, please consider buying me a chilled pint to sip while writing the next one!

Gift a pint

Subscribe to Sam::blog()

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
[email protected]
Subscribe