DEV Community

Sophia Brandt
Sophia Brandt

Posted on • Originally published at rockyourcode.com on

Developing with Elixir in Vim

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:

Configuration:

" in `.vimrc` or `~/.config/nvim/init.vim`
syntax on
filetype plugin indent on

set laststatus=2
set wildmenu
Enter fullscreen mode Exit fullscreen mode

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 deps.get
$ mix compile
$ MIX_ENV=prod mix elixir_ls.release
Enter fullscreen mode Exit fullscreen mode

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):

" in `.vimrc` or `~/.config/nvim/init.vim`

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

let g:ale_fixers = {
\   'elixir': ['mix_format'],
\}
Enter fullscreen mode Exit fullscreen mode

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

" in `.vimrc` or `~/.config/nvim/init.vim`

let g:ale_elixir_elixir_ls_release='~/<PATH-TO-YOUR-RELEASE>'
Enter fullscreen mode Exit fullscreen mode

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

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

I chose the following config options:

" in `.vimrc` or `~/.config/nvim/init.vim`

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>
Enter fullscreen mode Exit fullscreen mode

Better Language Client Support

ALE is great for linting and fixing files, but it can make VIM slow.

Alternatively, you can use a dedicated language server plugin, for example LanguageClient-neovim (works for Vim8 and NeoVim).

You can find a guide here.

Further Reading

Top comments (0)