Developing with Elixir in Vim

03/15/20191 Min Read — In DevTools, Vim, Elixir

Using (Neo)Vim is surprisingly enjoyable after the initial hurdle.

Language support for Elixir also works fine in Vim.

Plugins

I use minpac as my package manager. VimCasts has a good introduction video on minpac if you're interested.

Add these plugins to your ~/.vimrc:

Elixir Language Server

You need the Elixir Language Server as a backend/integration tool.

Either head over to the original elixir-ls (slow development at the moment) or the more current fork elixir-lsp elixir-ls fork.

You need to clone the repository.

$ git clone git@github.com:elixir-lsp/elixir-ls.git
$ cd elixir-ls && mix compile
$ MIX_EV=prod mix elixir_ls.release

This creates a folder release which includes an executable, for example release/language_server.sh (.bat for Windows).

You need the path to that file for ALE support.

ALE

ALE acts as the client for the Elixir Language Server. Setup for ALE also needs to go into your vim settings (~/.vimrc):

let g:ale_linters = {
\   'elixir': ['elixir-ls'],
\}

let g:ale_fixers = {
\   'elixir': ['mix_format'],
\}

You also have to tell ALE where the language server is:

let g:ale_elixir_elixir_ls_release='~/<PATH-TO-YOUR-RELEASE>'

Only specify the path, not the file itself (language_server.sh or language_sever.bat).

Now, you can use the command :ALEFix in Vim to fix your files.

I chose the following config options:

let g:ale_completion_enabled = 1
let g:ale_sign_error = '✘'
let g:ale_sign_warning = '⚠'
let g:ale_lint_on_enter = 0
let g:ale_lint_on_text_changed = 'never'
highlight ALEErrorSign ctermbg=NONE ctermfg=red
highlight ALEWarningSign ctermbg=NONE ctermfg=yellow
let g:ale_linters_explicit = 1
let g:ale_lint_on_save = 1
let g:ale_fix_on_save = 1

noremap <Leader>ad :ALEGoToDefinition<CR>
nnoremap <leader>af :ALEFix<cr>
noremap <Leader>ar :ALEFindReferences<CR>

"Move between linting errors
nnoremap ]r :ALENextWrap<CR>
nnoremap [r :ALEPreviousWrap<CR>

Further Reading