Trey Hunner

I help developers level-up their Python skills

Hire Me For Training

Maintaining consistent coding conventions with EditorConfig

| Comments

I often have indentation issues when coding because I work with many open source (and closed source) projects that have different identation styles. For example, my jQuery formrestrict uses 4 spaces for indentation, jQuery expander uses 2 spaces, and jQuery pagination uses hard tabs. I don’t want to change my text editor’s indentation settings every time I open one of these files and using a plugin to auto-detect indentation is only a partial solution. To solve this problem I started a project I call EditorConfig.

EditorConfig defines coding conventions for groups of files and instructs text editors to adhere to these conventions. To solve my problem, I just create a file called .editorconfig that sets indentation for my files and with my EditorConfig plugin installed Vim takes care of the rest. Here’s an example .editorconfig file I could add to my project that uses jQuery expander and jQuery pagination:

.editorconfig
1
2
3
4
5
6
7
8
9
10
11
[*.js]
indent_style = space
indent_size = 4

[jquery.expander.js]
indent_style = space
indent_size = 2

[jquery.pagination.js]
indent_style = tab
indent_size = 4

With this .editorconfig file, all JavaScript files use 4 spaces for indentation except for jquery.expander.js which uses 2 spaces and jquery.pagination.js which uses hard tabs with a column width of 4. If I put my .editorconfig file under version control, other developers working on my project can see the coding conventions I defined and if their text editor has an EditorConfig plugin installed their editor will automatically use the correct indentation as well.

Example EditorConfig files can be seen in my own projects (such as in my dotfiles repo) and in the various EditorConfig plugin codebases. More information on EditorConfig can be found at the EditorConfig website.

EditorConfig plugins are available for 6 different editors now. If you like the idea, try out EditorConfig for your favorite editor and tell us what you think. If you don’t like the idea or have a problem with EditorConfig please submit an issue, add a thread to the mailing list, or send me an email voicing your concern.

Comments

Write more Pythonic code

Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.