Published: February 17, 2018 • 2 min read
I’ve been working with the Elixir 1.6 autoformatter a lot this year. Part of that journey was adding the following to my local Vim configuration file:
" ~/.vimrc autocmd BufWritePost *.exs,*.ex silent :!mix format --check-equivalent %
This is one of several ways to make autoformatting a thoughtless process. Here’s a breakdown of each piece of the Vim language:
autocmdtells Vim to run the code automatically on matching files
BufWritePostis the action where this applies: after the Vim buffer (file) has been written. Some folks are applying this after text entry, making it a much more frequent event. I’m keeping it simple for now
*.exs,*.exare the extensions we match, the two common extensions for Elixir files
silent :!mix format --check-equivalent %tells Vim the following:
mix formatoutside the editor against the current file. Make sure that the AST before and after are the same so we aren’t changing what the code does, which would indicate there’s a bug in the formatter.
I like text editor integrations. Integrations are where an autoformatter lives or dies— programmatic code changes must be automated to guarantee the rules are always applied.
What are your thoughts on this? Let me know!
Get better at programming by learning with me! Join my 100+ subscribers receiving weekly ideas, creations, and curated resources from across the world of programming.