Buffer Yard
Language setup

Emacs Elixir Setup: A Language Server, IEx and ExUnit

Updated

mix new tally && cd tally && emacs lib/tally.ex

That file opens, and the first decision is already waiting: Emacs offers two Elixir major modes, and the rest of the setup depends a little on which one you take. After that comes the language server, where Elixir has more options than most languages and a history that explains why.

Two major modes

elixir-mode lives on MELPA and has been the Elixir major mode for many years. It uses regular-expression highlighting and SMIE for indentation. It is mature and it works on any Emacs version you are likely to have.

elixir-ts-mode is built into Emacs since 30.1 and uses tree-sitter. Highlighting is more accurate, especially in sigils, pipes and nested do blocks, and it comes with heex-ts-mode for Phoenix templates. It needs two grammars installed:

(setq treesit-language-source-alist
      '((elixir "https://github.com/elixir-lang/tree-sitter-elixir")
        (heex "https://github.com/phoenixframework/tree-sitter-heex")))
;; then once: M-x treesit-install-language-grammar RET elixir
;;            M-x treesit-install-language-grammar RET heex

On Emacs 30 or later, use elixir-ts-mode. On 29, elixir-mode is the simpler choice, although the same tree-sitter mode is also packaged on MELPA for 29.

The language server question

Elixir spent several years with three community language servers competing for the same job, and that history still shapes what you install today.

ElixirLS is the oldest and most widely used. It bundles Dialyzer integration, a debugger adapter and broad editor support. Its weakness has been start-up time on large projects and occasional trouble when your project’s dependencies conflict with its own.

Lexical was written to address those problems. It runs in its own isolated environment, so your dependencies cannot break it, and it comes up quicker. Its repository was archived in August 2025, when its developers moved to the joint project below.

That joint project is Expert, the official language server announced on the Elixir blog, built by maintainers of the earlier servers working together. Its README still called it alpha when this page was written, so check the release status there before relying on it. Eglot does not care which server you pick; it only needs a command.

Each server ships a launch script. After building or downloading a release, point Eglot at it:

(use-package elixir-ts-mode
  :mode ("\\.exs?\\'" . elixir-ts-mode)
  :hook (elixir-ts-mode . eglot-ensure)
  :config
  (with-eval-after-load 'eglot
    (add-to-list 'eglot-server-programs
                 `((elixir-ts-mode heex-ts-mode)
                   . (,(expand-file-name "~/src/elixir-ls/release/language_server.sh"))))))

For Expert, replace the path with the expert binary followed by --stdio, as its README describes; an existing Lexical install uses start_lexical.sh from its build output instead. The command is the only thing that changes.

Eglot decides where a project starts through the built-in project.el, which in practice means the root of your Git repository. For a normal Mix project that is exactly right. Umbrella projects are the case to watch: the server should run from the umbrella root, not from one app inside apps/, and as long as the umbrella is the repository root that happens by itself.

The first time the server sees a project it compiles it, and ElixirLS also builds a Dialyzer PLT, which can take several minutes. Let it finish before deciding the setup is broken. Diagnostics, go-to-definition, hover docs and completion arrive afterwards through Eglot’s usual commands, and completion appears in whatever front end you set up in the completion guide.

mix format on save

Every Elixir project formats with mix format and reads its rules from .formatter.exs. Both ElixirLS and Lexical answer Eglot’s formatting request by running the project’s formatter, so you get project-correct results without a separate package:

(add-hook 'elixir-ts-mode-hook
          (lambda ()
            (add-hook 'before-save-hook #'eglot-format-buffer nil t)))

If you prefer formatting without a server running, the apheleia package runs formatters asynchronously and knows about mix format. It also avoids the small pause that synchronous formatting causes on save in larger files.

IEx in a buffer

Elixir developers spend a lot of time in IEx, and running it inside Emacs means you can send code to it without copying.

The inf-elixir package does this. M-x inf-elixir starts a plain IEx; M-x inf-elixir-project starts iex -S mix from the project root, which is the one you want almost every time, since it loads your application. From a source buffer, inf-elixir-send-line, inf-elixir-send-region and inf-elixir-send-buffer push code across.

(use-package inf-elixir
  :ensure t
  :after elixir-ts-mode
  :bind (:map elixir-ts-mode-map
              ("C-c i i" . inf-elixir-project)
              ("C-c i r" . inf-elixir-send-region)
              ("C-c i b" . inf-elixir-send-buffer)))

Sending a whole module recompiles it in the running node, and IEx prints a redefinition warning. That is normal. For changes across several files, recompile typed at the IEx prompt is still quicker than sending buffers one at a time.

ExUnit from the editor

Tests are where the loop closes. The exunit package wraps mix test in a compilation buffer, so failures become clickable links to the failing assertion:

  • exunit-verify-all runs the whole suite
  • exunit-verify runs the current test file
  • exunit-verify-single runs the test at point
  • exunit-rerun repeats the last run

It works the same with either major mode. Because the output buffer is a compilation buffer, M-g n jumps to the next failure, the same way it does for compiler errors in the C setup.

Where to go from here

Phoenix projects add HEEx templates, which is the main reason to have heex-ts-mode in the Eglot server list above. ElixirLS and Lexical both handle .heex files, though support for completion inside templates varies.

For what else is worth installing, the packages guide covers the general-purpose additions. Elixir sits close to Erlang, and if you are coming from another functional language the Haskell page shows the same Eglot pattern with a compiler that complains far more. Every other language is listed on the languages overview.

If a server starts misbehaving after a dependency upgrade, delete its build directory inside your project (.elixir_ls for ElixirLS, .lexical for Lexical) and reconnect with M-x eglot-reconnect.

Questions readers ask

Which Elixir language server should I start with?

ElixirLS is the most widely used and the safest choice if you just want things to work. Expert, the official server that carries Lexical's work forward, is the one to watch, but check its release status first. Whichever you choose, give Eglot the full launch command, because ElixirLS in particular is usually not on PATH.

Why does elixir-ts-mode not highlight anything?

The mode needs the Elixir and HEEx tree-sitter grammars installed. Run M-x treesit-install-language-grammar for elixir and heex, which requires a C compiler, then reopen the file.

Can I run a single ExUnit test from the buffer?

Yes. The exunit package provides exunit-verify-single, which runs the test at point through mix test file:line. Without the package you can do the same from M-x compile with that command typed in.