Developing with Elixir in Vim
Tags: editors, vim, elixir
Published: 2019-09-20 08:29:40

---
title: Developing with Elixir in Vim
published: true
tags: editors,Vim,Elixir
canonical_url: https://www.rockyourcode.com/developing-with-elixir-in-vim
---

Using (Neo)Vim is surprisingly enjoyable after the [initial hurdle](https://www.rockyourcode.com/use-the-terminal-from-neovim-and-vim-is-hard).

Language support for Elixir also works fine in Vim.

## Plugins

I use [minpac](https://github.com/k-takata/minpac) as my package manager. VimCasts has a good [introduction video on minpac](http://vimcasts.org/episodes/minpac/) if you're interested.

Add these plugins to your `~/.vimrc`:

- [elixir-editors/vim-elixir](https://github.com/elixir-editors/vim-elixir)
- _optional_: [tpope/endwise](https://github.com/tpope/vim-endwise) for automatically adding `end` after `def`, `do`, etc.
- _optional_: [dense-analysis/ale](https://github.com/w0rp/ale) for fixing and linting files

Configuration:

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

set laststatus=2
set wildmenu
```

## Elixir Language Server

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

Either head over to the original [elixir-ls](https://github.com/JakeBecker/elixir-ls) (slow development at the moment) or the more current fork [elixir-lsp elixir-ls fork](https://github.com/elixir-lsp/elixir-ls).

You need to clone the repository.

```bash
$ 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
```

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](https://github.com/w0rp/ale/) also needs to go into your vim settings (`~/.vimrc`):

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

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:

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

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

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:

```vimscript
" 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>
```

# 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][languageclient]** (works for Vim8 and NeoVim).

You can find a guide [here](https://www.rockyourcode.com/neo-vim-and-language-client-for-elixir).

### Further Reading

- [How to use Elixir LS with Vim](https://www.mitchellhanberg.com/post/2018/10/18/how-to-use-elixir-ls-with-vim/) by Mitchell Hanberg
- [Emacs - Elixir Setup Configuration Wiki](https://elixirforum.com/t/emacs-elixir-setup-configuration-wiki/19196)
- [How I configure Vim to work with Elixir without much clutter](https://medium.com/@hauleth/dumb-elixir-visual-and-improved-editor-53c23a0800e4) by Hauleth - and [forum post](https://elixirforum.com/t/how-i-configure-vim-to-work-with-elixir-without-much-clutter/21610)

[languageclient]: https://github.com/autozimu/LanguageClient-neovim