Mix-format daemon

Is there anyway to keep “mix format” running and watching files on a specific directory?

I tried to run it automatically whenever I save a file, but it takes some time and I’m that kind of dev that saves pretty much everytime. :stuck_out_tongue:

I might be wrong, but I feel that a great deal of the time it takes to format the file is related to starting mix up.

1 Like

I’m not aware of a daemon, but I do know that editors can take advantage of ElixirLS which is a server running in the background (like a daemon I suppose) and perform tasks like formatting your code. You can adjust settings in your editor to format on save or on-demand. Might be faster to use ElixirLS than spinning up mix to format each time.

Which editor do you use?


The problem with that is that it would modify file twice. You can “hand craft” something like that by using entr(1), however I would say that you should use pre-save hook in your editor.


Thanks, @dbern!

I’m using NeoVim, with coc-elixir using elixir-ls to lint and autocomplete my code. I didn’t know that elixir-ls could also handle code formatting.

So, turns out coc has a feature to run the format command offered by the language servers. Here’s how to do it:

1- Run the command :CocConfig
2- Add elixir to the following configuration:

  coc: {
     preferences : {
       "formatOnSaveFiletypes": ["html", "javascript", "elixir"]

You can also use :call CocAction('format') to format on demand.

Thanks again, @dbern and @hauleth!

Your client need to support the code format action.

I know nothing about which features are supported by coc.vim. You need to consult its documentation.

Or perhaps @hauleth can help you. I know he uses vim + elixir-ls, though I do not know which client he uses.

1 Like

Thanks, @NobbZ.

I was doing just that :slight_smile:

After I made the change formatting became instantaneous. mix format was taking more than a couple seconds sometimes.

Yep you got it. I do something really similar

My vim config has a lot of trash in it but feel free to peruse it to find other tips

Here’s what I’m using (auto format and re-run affected tests):

watchexec -e 'ex,exs,eex,leex' -- 'mix format && mix test --stale'

You need to configure your editor to properly pick up external changes automatically and not nag you with a popup.


This worked brilliantly! Amazing! Thank you very much.

if you have a program like say or speak, you can do really fun stuff like:

ls **/*.{ex,exs,heex} | entr sh -c "mix format; mix test && say 'tests all green' || say 'we have a problem, some test failed'" 
1 Like