Use EditorConfig to Maintain Consistent Coding Styles between Different Editors and IDEs

Usually for a project with more than one developer involved, it is essentially important for the project to define and maintain a consistent coding style. Most code editors and IDEs, such as Vim, Emacs, Code::Blocks, provide settings related to coding styles, such as the width of tab, the size of indentation, end of line, etc. However, it is hard to provide the same settings for different Editors and IDEs: we have to maintain many config files for different editors and IDEs, such as .vimrc for Vim, .emacs for Emacs. In order to solve this, EditorConfig was born. By defining coding style in files named .editorconfig, the EditorConfig plugins for different editors and IDEs will automatically adjust your coding style.

Download EditorConfig Plugin for Your Editor/IDE

To use EditorConfig, you have to download the corresponding EditorConfig plugins for your Editor or IDE. Follow the installation instructions to install them.

Create an .editorconfig File for Your Project

An .editorconfig file is an INI format file, which contains the settings of your coding style. You could use one or more .editorconfig files to indicate the coding style of your project. Let’s try a simple example. Create a file named .editorconfig at the root of your project source tree (if you are working on Windows and using Windows Explorer, you will find Window Explorer reports such file name as illegal. Don’t worry, just create a file named .editorconfig., which will be renamed to .editorconfig automatically by Windows Explorer):

This is a comment line. All lines starting with a # or ; will be regarded as comment lines.

root = true

This line tells EditorConfig this is the root of the project, thus EditorConfig won’t apply the settings of .editorconfig outside this directory.

[*]
indent_style = space
end_of_line = lf

These 3 lines indicates for all files, if not specially specified, we use spaces for indentation, and use lf as the EOL marker for all files.

[*.c]
indent_size = 4

These two lines means, for any C source files, the size of indentation is 4.

[Makefile]
indent_style = tab
indent_size = 8

These 3 lines tell EditorConfig that, for any file named Makefile, we use tab for indentation, and the size of indentation is 8.

After saving this file, open a C source file and a Makefile in your source tree with your editor/IDE. If everything goes well, you will find that the buffer-local coding style settings have been adjusted. (Note: EditorConfig is not a code formatter. That is to say, the existing codes in your source files will not be reformatted. Only the style of your newly input code is adjusted.)