iTranslated by AI
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:
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 thegoextension. -
indent_stylespecifies the indentation style. You can choosetaborspace. -
indent_sizespecifies the width of the indentation. Ifindent_styleistab, it seems to be determined bytab_width. -
end_of_linespecifies the line ending character. You can choose fromlf,cr, orcrlf. -
charsetspecifies the character encoding. You can choose fromlatin1,utf-8,utf-8-bom,utf-16be, orutf-16le. Unfortunately, other encodings depend on the implementation of the editor. - If
trim_trailing_whitespaceis set totrue, it will remove any whitespace at the end of lines. - If
insert_final_newlineis set totrue, 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
- EditorConfigで文字コード設定を共有して喧嘩しなくなる話。(Frontrend Advent Calendar 2014 – 14日目) | Ginpen.com
- どんなエディタでもEditorConfigを使ってコードの統一性を高める - Qiita
- 【ATOM Editor】 EditorConfig を使うなら Whitespace は不要 — しっぽのさきっちょ | text.Baldanders.info
Discussion