There Are a few challenges when it comes to automatically mix format-ing your files on save in Vim.
- Needs to happen async, so it doesn't block you from typing while it's working.
- You want to be able to keep your cursor position, not be reset to the top of the file after formatting completes.
- Formatting should happen smoothly whether doing
:w, or:wq. Formatting after:wqgave me trouble in the past and would fail formatting. I think this is becausemix formatwas called via the editor, but then the editor process exited, so it was getting terminated (I think).
This simple config seems to do a pretty good job at solving these challenges:
First, in $nvim_config_dir/ftplugin/elixir.vim (and eelixir.vim) I have an autocommand set up. It uses BufWritePost because the work is going to happen async. I think BufWritePre should be OK too.