VGTech is a blog where the developers and devops of Norways most visited website share code and tricks of the trade… Read more



Are you brilliant? We're hiring. Read more

EditorConfig: Consistent coding style

QA

If you’ve worked on more than one project in your coding career, you’ll probably have come across different coding styles. While some projects prefer an indentation of four spaces, others might prefer to only use two. Some may even use tabs. Then there’s line-endings, charsets and trailing whitespace.

When you are contributing to a project, it’s a good idea to try and follow the coding style already in place. This makes it more likely that your contributions will get accepted, and is in general a Nice Thing™ to do. So how can we simplify the task of setting up our editor to use a project coding style? Enter: EditorConfig.

EditorConfig

Not sure why the logo is a mouse.

EditorConfig introduces a simple way to synchronize the coding style across different IDEs and editors. Basically, when you open a file, it will look for a .editorconfig-file in the same directory and every parent directory until one is found. It then parses the file and applies the settings to your editor. Simple, eh?

The syntax is simple and the settings fairly self-explanatory. Here’s an example .editorconfig from one of my projects:

Show code
root = true

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

[*.md]
trim_trailing_whitespace = false

There are plugins available for many of the most popular editors and IDEs such as Vim, Emacs, Sublime Text, TextMate etc. Take a look at the official page for a full listing.

My advice: Install the plugin for your editor today and create an .editorconfig for your projects.

Developer at VG with a passion for Node.js, React, PHP and the web platform as a whole. espen.codes - @rexxars


1 comments

  • Bjarne Øverli

    Intressangt. Dette skal jeg sjekke nærmere ut!


Leave your comment