Editorconfig

Learn how to use .editorconfig files to maintain consistent coding styles across different editors and IDEs, ensuring uniform formatting for Markdown, YAML and Hugo content.

Editorconfig is a powerful tool for maintaining consistent coding styles across different editors and IDEs. It helps ensure that your Markdown, YAML, and Hugo content is uniformly formatted, regardless of the individual developer's setup. This guide will walk you through the basics of `.editorconfig`, how it works with VS Code, and best practices for using it effectively.


πŸ“ What is .editorconfig

.editorconfig is a configuration file used to define and enforce consistent coding styles across different editors and IDEs. It helps teams maintain formatting standards regardless of individual developer setups.


🧰 How It Works with VS Code

VS Code supports .editorconfig natively, and full compatibility is available via the EditorConfig extension.

When you open a file, VS Code:

  1. Searches for a .editorconfig file in the file’s directory.
  2. Walks up the folder tree until it finds one.
  3. Applies the formatting rules defined in the file.

You can enable formatting on save in your VS Code settings:

1"editor.formatOnSave": true,
2"editorconfig.generateAuto": true    # Optional: Automatically generate .editorconfig files

🏠 Global vs Local Usage

πŸ”Ή Local .editorconfig

  • Placed in the project root (e.g. /my-hugo-site/.editorconfig)
  • Applies only to files within that project
  • Should include root = true to prevent fallback to global settings

πŸ”Ή Global .editorconfig

  • Placed in your home directory:
    • macOS/Linux: ~/.editorconfig
    • Windows: C:\Users\<YourUsername>\.editorconfig
  • Acts as a fallback when no project-level config is found
  • Should not include root = true to allow overrides

βœ… Advantages of Using .editorconfig

  • πŸ”„ Consistency: Ensures uniform formatting across editors and contributors
  • 🧩 Modularity: Supports per-project overrides for different tech stacks
  • πŸ› οΈ Toolchain Harmony: Works alongside Prettier, markdownlint, and other formatters
  • πŸ‘₯ Team-Friendly: Reduces friction in code reviews and merges
  • πŸ“¦ Lightweight: No runtime dependencies, just a plain text config

πŸ“ Sample Top most .editorconfig file without root being true

 1# EditorConfig helps maintain consistent coding styles
 2# across different editors and IDEs
 3# https://editorconfig.org
 4
 5# Top-most EditorConfig file
 6
 7[*]
 8end_of_line = lf
 9insert_final_newline = true
10charset = utf-8
11trim_trailing_whitespace = true
12indent_style = space
13indent_size = 2
14
15# Markdown files
16[*.{md,markdown}]
17trim_trailing_whitespace = false
18indent_style = space
19indent_size = 2
20
21# YAML files
22[*.{yml,yaml}]
23indent_style = space
24indent_size = 2
25
26# HTML, CSS, SCSS, JavaScript files
27[*.{html,css,scss,js}]
28indent_style = space
29indent_size = 2
30
31# Go files for Hugo modules
32[*.go]
33indent_style = tab
34indent_size = 4
35
36# Shortcodes
37[layouts/shortcodes/**.html]
38insert_final_newline = false
39
40# Hugo templates
41[layouts/**.html]
42[*.{html,go}]
43indent_size = 2

πŸ› οΈ Best Practice

  1. Use the Latest Version: Always use the latest version of the EditorConfig plugin for your editor to ensure compatibility and access to the latest features.
  2. Define Project-Specific Rules: Create a .editorconfig file in the root of each project to define rules specific to that project.
  3. Document Your Rules: Add comments in your .editorconfig file to explain the purpose of specific rules, making it easier for team members to understand and follow them.
  4. Combine with Other Tools: Use .editorconfig alongside other formatting tools like Prettier and markdownlint for comprehensive code quality management.
  5. Regularly Review and Update: Periodically review and update your .editorconfig files to ensure they meet the evolving needs of your projects and team.

Conclusion

In conclusion, .editorconfig is a powerful tool for maintaining consistent coding styles across different editors and IDEs. By following best practices and leveraging the capabilities of .editorconfig, teams can improve code quality, reduce friction in collaboration, and create a more enjoyable development experience. Implementing .editorconfig in your projects is a step towards a more organized and efficient workflow, ensuring that everyone adheres to the same formatting standards regardless of their individual setups.