EditorConfig Usage Guide for Code Consistency | Web Formatter Blog

EditorConfig Usage Guide for Code Consistency
Introduction to EditorConfig
EditorConfig is a file format and collection of text editor plugins that enable developers to maintain consistent coding styles between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles.
In this comprehensive guide, we'll explore how EditorConfig works, how to set it up for your projects, and best practices for maintaining consistent code formatting across your team.
Why Use EditorConfig?
When working on projects with multiple developers, maintaining consistent coding styles can be challenging. Different developers may use different editors or IDEs, each with their own default formatting settings. This can lead to inconsistent code formatting, which makes code reviews more difficult and can introduce unnecessary changes in version control.
EditorConfig solves this problem by:
- Defining coding styles in a standardized, editor-agnostic format
- Supporting a wide range of editors and IDEs through plugins
- Allowing for project-specific and file-type-specific settings
- Being lightweight and easy to implement
- Working alongside other code formatting tools
Getting Started
To start using EditorConfig in your project, you need to:
-
Create an
.editorconfig
file in your project's root directory - Install the EditorConfig plugin for your editor (if needed)
-
Define your coding style preferences in the{" "}
.editorconfig
file
Here's a basic example of an .editorconfig
file:
# 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
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
# 4 space indentation for Python files
[*.py]
indent_size = 4
# Tab indentation for Makefiles
[Makefile]
indent_style = tab
EditorConfig File Format
The .editorconfig
file uses an INI-like format with
sections and properties. Each section consists of a file pattern
in square brackets, followed by properties and their values.
The file pattern uses glob patterns to match files:
-
*
matches any string of characters, except path separators -
**
matches any string of characters -
?
matches any single character -
[name]
matches any single character in name -
{"{s1,s2,s3}"}
matches any of the strings given (separated by commas) -
{"{num1..num2}"}
matches any integer numbers between num1 and num2, inclusive
For example, [*.{"{js,py}"}]
would match all
JavaScript and Python files.
Common Properties
EditorConfig supports the following common properties:
Property | Possible Values | Description |
---|---|---|
indent_style
|
tab , space
|
Indentation style |
indent_size
|
integer, tab
|
Indentation size in spaces |
tab_width
|
integer | Width of a tab character |
end_of_line
|
lf , cr , crlf
|
Line ending style |
charset
|
latin1 , utf-8 ,{" "}
utf-16be , utf-16le ,{" "}
utf-8-bom
|
File character encoding |
trim_trailing_whitespace
|
true , false
|
Remove trailing whitespace on save |
insert_final_newline
|
true , false
|
Ensure file ends with a newline |
root
|
true , false
|
Special property that should be at the top of the file
outside any sections. Set to true to stop{" "}
.editorconfig files search on current file.
|
Language-Specific Settings
One of the strengths of EditorConfig is the ability to define different settings for different file types. Here are some common language-specific configurations:
JavaScript and TypeScript
[*.{js,jsx,ts,tsx}]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
Python
[*.py]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
HTML, CSS, and JSON
[*.{html,css,scss,json}]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
PHP
[*.php]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
Markdown
[*.md]
indent_style = space
indent_size = 2
trim_trailing_whitespace = false
insert_final_newline = true
IDE Integration
Many popular editors and IDEs have built-in support for EditorConfig or offer plugins:
Built-in Support
- Visual Studio Code
- IntelliJ IDEA (and other JetBrains IDEs)
- Visual Studio
- GitHub
- Atom
Plugin Required
- Sublime Text:{" "} EditorConfig Package
- Vim:{" "} editorconfig-vim
- Emacs:{" "} editorconfig-emacs
- Notepad++:{" "} EditorConfig Notepad++ Plugin
EditorConfig vs. Prettier
EditorConfig and Prettier are complementary tools that serve different purposes:
Feature | EditorConfig | Prettier |
---|---|---|
Primary Focus | Editor settings consistency | Code formatting |
Scope | Basic formatting rules | Comprehensive code style |
Language Support | Language-agnostic | Specific languages |
Integration | Editor/IDE plugins | CLI, editor plugins, pre-commit hooks |
Enforcement | During editing | During save or as a separate step |
In many projects, both tools are used together:
- EditorConfig ensures consistent editor settings
- Prettier handles code formatting
When using both, it's important to ensure that your EditorConfig
settings don't conflict with your Prettier configuration. For
example, if you set indent_size = 2
in EditorConfig,
you should also set tabWidth: 2
in your Prettier
configuration.
Real-World Examples
Let's look at some real-world examples of EditorConfig files from popular open-source projects:
React
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 80
[*.md]
trim_trailing_whitespace = false
Django
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{html,css,scss,json,yml,js}]
indent_size = 2
[*.md]
trim_trailing_whitespace = false
Laravel
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.{yml,yaml,json,js,css,scss,vue}]
indent_size = 2
Best Practices
Here are some best practices for using EditorConfig in your projects:
- Keep it simple: Only include settings that are necessary for your project. Too many settings can be confusing and harder to maintain.
-
Document your choices: Add comments to your{" "}
.editorconfig
file to explain why certain settings were chosen. - Be consistent: Use the same settings across all projects in your organization to reduce context switching for developers.
-
Include in version control: Always commit your{" "}
.editorconfig
file to version control so all team members have access to it. - Use with other tools: Combine EditorConfig with linters and formatters for comprehensive code style enforcement.
-
Set
root = true
: Always include this at the top of your file to prevent EditorConfig from looking for other.editorconfig
{" "} files in parent directories. - Be mindful of language-specific conventions: Respect the common conventions for each programming language.
Troubleshooting
If you're experiencing issues with EditorConfig, here are some common problems and solutions:
Settings Not Being Applied
- Ensure the EditorConfig plugin is installed for your editor
-
Check that your
.editorconfig
file is in the correct location (usually the project root) - Verify that your file patterns are correct
-
Restart your editor after making changes to the{" "}
.editorconfig
file
Conflicts with Other Tools
- If you're using Prettier, ESLint, or other formatting tools, ensure their configurations don't conflict with EditorConfig
- Consider using EditorConfig for basic editor settings and other tools for more advanced formatting
Multiple .editorconfig
Files
-
EditorConfig looks for
.editorconfig
files in the directory of the opened file and in all parent directories - Settings are applied from top to bottom, with closer files taking precedence
-
Use
root = true
to stop the search at a specific directory
Conclusion
EditorConfig is a simple yet powerful tool for maintaining
consistent coding styles across different editors and IDEs. By
defining a few basic settings in a .editorconfig
{" "}
file, you can ensure that all developers on your team follow the
same formatting conventions, regardless of their preferred
development environment.
When combined with other tools like Prettier and ESLint, EditorConfig forms part of a comprehensive code quality strategy that can help reduce formatting-related issues in code reviews and make your codebase more maintainable.
Start using EditorConfig in your projects today to improve code consistency and reduce unnecessary formatting changes in your version control system.