iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💮

Manage Indentation and Line Endings with EditorConfig

に公開

EditorConfig is a mechanism for sharing settings such as character encoding, indentation, and line endings across text editors and IDEs (Integrated Development Environments). It is either built into major editors and IDEs by default or can be introduced via extensions. This helps suppress coding style confusion caused by differences in development environments or personal preferences.

Settings with EditorConfig

To enable EditorConfig, simply place an .editorconfig file in the root folder of your project. For example, here is the content of an .editorconfig file I frequently use:

.editorconfig
root = true

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

[*.go]
trim_trailing_whitespace = true

[*.md]
indent_style = space
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

EditorConfig searches for .editorconfig files by moving up the folder hierarchy and evaluates them starting from the top. If root = true is not specified, it will continue searching up through parent directories indefinitely, so be sure to include this in the .editorconfig file at the root of your project.

  • [...] specifies the target files. For example, [*] targets all files, while [*.go] targets files with the go extension.
  • indent_style specifies the indentation style. You can choose tab or space.
  • indent_size specifies the width of the indentation. If indent_style is tab, it seems to be determined by tab_width.
  • end_of_line specifies the line ending character. You can choose from lf, cr, or crlf.
  • charset specifies the character encoding. You can choose from latin1, utf-8, utf-8-bom, utf-16be, or utf-16le. Unfortunately, other encodings depend on the implementation of the editor.
  • If trim_trailing_whitespace is set to true, it will remove any whitespace at the end of lines.
  • If insert_final_newline is set to true, it will ensure the file ends with a newline character if it doesn't already have one.

For detailed specifications, please refer to the specification page. Note, however, that some editors or IDEs may not cover all features.

Standardizing Coding Rules with EditorConfig

With a mechanism like this, there is no need to inform others of "coding rules" through documentation; simply including an .editorconfig file in the repository will suffice[1]. In fact, it should be a habit to set up an .editorconfig file as soon as a repository is created.

Well, since some languages like Go and Rust now provide official formatting tools[2], the demand may not be as high as it used to be.

References

脚注
  1. Still, for languages that allow ambiguous descriptions, you might still run into trouble without coding conventions. ↩︎

  2. Some Go packages found on GitHub require formatting with tools like gofmt before issuing a pull request. ↩︎

GitHubで編集を提案

Discussion