Why .editorconfig Still Matters Even with Prettier Around
The .editorconfig
file is used to set rules for code formatting and file styling. These rules ensure consistent code styles across different developers and editors. The .editorconfig
file mainly configures attributes such as indentation style, indentation size, line endings, character encoding, trailing whitespace, and final newline.
Below are the configuration options available in a .editorconfig
file and their details.
Detailed Explanation of the .editorconfig
File
root
Specifies whether the current .editorconfig
file is the root configuration file for the project. If set to true
, the editor will stop searching for .editorconfig
files in parent directories. This is useful when there are multiple .editorconfig
files in a project to ensure the current file acts as the final configuration.
root = true
[pattern] - File Matching Patterns
Defines the file types the rules apply to. Wildcards like *
(matches any characters), ?
(matches a single character), and {}
(matches multiple file types) are supported. For example, [*.js]
matches all JavaScript files, and [*.{html,css}]
matches both HTML and CSS files.
[*.js]
indent_style
Defines the indentation style as either space
or tab
. This ensures consistent indentation style across different editors, improving code readability.
indent_style = space
indent_size
Specifies the size of the indentation, typically a positive integer. If set to tab
, the indentation size depends on tab_width
. Common values are 2 or 4 spaces.
indent_size = 4
tab_width
Defines the display width of a tab character, which affects the visual appearance of tab-based indentation. It is often used alongside indent_size
to ensure consistent indentation display.
tab_width = 4
end_of_line
Specifies the format of line endings. lf
represents Line Feed (\n
), crlf
represents Carriage Return and Line Feed (\r\n
), and cr
represents Carriage Return (\r
) (rarely used). Uniform line endings help prevent version control conflicts in cross-platform development.
end_of_line = lf
charset
Defines the character encoding for files. Common options include utf-8
, utf-16
, and latin1
. utf-8
is recommended because it supports multiple languages and has good compatibility across platforms.
charset = utf-8
trim_trailing_whitespace
Determines whether to automatically remove unnecessary whitespace at the end of lines. This helps maintain clean code and prevents irrelevant changes in version control.
trim_trailing_whitespace = true
insert_final_newline
Specifies whether to add a newline at the end of the file. Many compilers and toolchains require a final newline, making this a good coding practice.
insert_final_newline = true
max_line_length
Sets the maximum length for each line to ensure code readability in narrower viewports. If set to off
, no line length restrictions are enforced.
max_line_length = 80
unset
Cancels a previously set property, reverting it to the default value. This can be used to override global settings for specific file types.
indent_size = unset
Example .editorconfig
File
Here’s a complete example showing specific configurations for different file types:
# Mark the current directory as the root
root = true
# General settings for all files
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
# Special settings for Markdown files
[*.md]
trim_trailing_whitespace = false
max_line_length = off
# Use tab indentation for Makefile
[Makefile]
indent_style = tab
# Set indentation size to 2 for JavaScript files
[*.js]
indent_size = 2
This example .editorconfig
file demonstrates how to set consistent coding styles and formatting for different file types, ensuring that team members using various editors maintain a unified code style.
How Does .editorconfig
Complement Prettier?
- Basic File Format Rules (Non-Code Files)
.editorconfig
applies to all file types (e.g., configuration files, Markdown, Makefile) by providing basic rules for indentation, character encoding, and line endings. Prettier primarily focuses on code files. - Character Encoding and Line Ending Management
.editorconfig
can standardize character encoding and line ending styles (e.g., LF or CRLF), which are not managed by Prettier. - Cross-Editor Compatibility
.editorconfig
is supported by most editors and IDEs. Even without Prettier enabled, editors can maintain consistent file formatting. - Support for Non-Programming Files
.editorconfig
provides basic formatting rules for non-programming files, filling a gap where Prettier does not support plain text files.
Why Use Both .editorconfig
and Prettier?
.editorconfig
and Prettier serve different purposes and complement each other to meet diverse needs.
.editorconfig
:
Focuses on fundamental file rules like indentation style, character encoding, and trailing whitespace, applying to all file types. These rules ensure consistency across various editors, even when Prettier is not in use.- Prettier:
Specializes in automated code formatting, handling more advanced aspects like the placement of empty lines, bracket styles, and other language-specific formatting.
Using both together ensures consistency in both basic file formatting and code style.
In cross-platform development, different operating systems (e.g., Windows, macOS, Linux) use varying default line endings. .editorconfig
provides a straightforward way to standardize line endings across all files in a project, avoiding conflicts caused by system differences.
For some file types (e.g., configuration files or documentation), .editorconfig
allows finer-grained control with specific rules, which can be very practical for certain projects. Moreover, not all projects use Prettier—especially legacy projects or those not requiring automated formatting. .editorconfig
serves as a universal configuration method supported by nearly all mainstream editors and IDEs, making it valuable for team collaboration even without Prettier.
Conclusion
.editorconfig
provides file-level control over basic formatting rules for all file types, while Prettier focuses on automated code styling. Combining both tools ensures comprehensive consistency in file styles and code formatting.
We are Leapcell, your top choice for hosting Node.js projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ
Author Of article : Leapcell Read full article