Format elixir on save VIM

Background

I am using VIM and I would like for vim to run mix format on save (:w).
However, I cannot find anything that does this.

Does someone know a plugin for this?
How many of you use VIM?

Autocommand BufPreWrite would do. Just remember to wrap it in augroup.

1 Like

I added this to my .vimrc:

" format elixir files when saving
augroup filetype_ex
	autocmd!
	autocmd BufPreWrite *.ex,*.exs mix format
augroup END

But I get this error:

Error detected while processing /home/pedro/.vim/vimrc:
line  140:
E216: No such group or event: BufPreWrite *.ex,*.exs mix format

What am I missing?

To answer your original problem:

autocmd BufWritePre *.ex,*.exs execute "!mix format %"

I use coc-elixir along coc.nvim… which provides coc-settings.json that can be used to configure formatting options.

2 Likes

I use LunarVim, comes with language-server, etc built-in.
Formatting is also included.
Easiest VIM-setup in years.

2 Likes

According to this the autocmd event should be BufWritePre.

4 Likes

I use nvim, with coc and elixir-ls.

I was not using format on save for elixir files (not sure why?), just turned it on, with coc, with Format elixir files on save · thenrio/dotfiles@264a9f9 · GitHub.

1 Like