BartOtten

BartOtten

Doom Emacs - Elixir Wiki

A wiki for Doom Emacs

Doom is a configuration framework for GNU Emacs. It focuses on performance and customizability. It can be a foundation for your own config or a resource for Emacs enthusiasts to learn more about our favorite OS.

Doom Emacs is considered the fastest Emacs ‘distribution’ at the expense of being more ‘hardcore’ than some alternatives (for example: Spacemacs)

This wiki will provide guides and tips to get you up and running with Emacs for Elixir development.

https://github.com/hlissner/doom-emacs - Official Doom Emacs site
https://github.com/tonini/alchemist.el - Alchemist Elixir site
https://github.com/elixir-lsp/elixir-ls - Elixir LS site


Prelude

There is no such thing as a ‘Doom Emacs’ app. You just run regular Emacs which loads the configuration generated by Doom using doom sync.

Doom Emacs configures Emacs by default uses keybindings like Vim. If you are not familiar with Vim, it’s best to have some basic understanding about the keys, actions and motions.

For now, just remember that

  • you can save a file using :w (press colon, press w)
  • SPC means spacebar
  • You can search in a file with /
  • You can search in all files of a project with SPC /
  • You can search and start a lot of functionality using SPC : In the list you will also see which keys are bound to certain actions. For example type SPC : find.

Installation

1) Emacs and Doom Emacs installation

Instructions how to install Doom Emacs can be found at it’s own Getting Started Guide.


2) Enabling Elixir support

To enable the support for Elixir, you have to adapt the ~/.doom.d/init.yml. You can easily open this file in Emacs by pressing SPC f p and selecting it from the list.

Uncomment the Elixir recipe by removing the ;; from the beginning of the line:

elixir

tip: Search for elixir using SPC /, put the cursor on the ; press x to remove the character, then save with :w

Important
Every time you make changes to the init.el file or packages.el file, you have to run ~/.emacs.d/bin/doom sync in the terminal and restart Emacs. This synchronizes your config with Doom Emacs. It ensures that needed packages are installed, orphaned packages are removed and necessary metadata correctly generated.

tip: you can also press SPC h r r to initiate doom sync && emacs restart in the background.


3) Optional: Enabling Elixir Language Server support

The Elixir Language Server provides a server that runs in the background, providing Emacs information about Elixir Mix projects. This enables code completion and more.

3a. Install the LSP server

First we install Elixir LSP by cloning the repo, compiling the package and creating a release.

git clone https://github.com/elixir-lsp/elixir-ls.git ~/.elixir-ls
cd ~/.elixir-ls
mix deps.get
mix compile
mix elixir_ls.release -o release

Next we add elixir-ls to the PATH variable. This is usually done in ~/.bashrc or ~/.zshrc. Add this line:

export PATH=$PATH:$HOME/.elixir-ls/release

When you start Emacs from another environment (for example: the desktop GUI) the shell environment variables are not available. Doom can sync shell environment variables to Emacs environment variables in ~/.emacs.d/.local/env. Run:

~/.emacs.d/bin/doom env

3b. Enable LSP in Doom Emacs

To enable LSP mode when editing Elixir files, you have to do open init.el again using SPC f p and change the line of elixir to

(elixir +lsp)

Also uncomment lsp in the tools section. I recommend to also enable the LSP peak function so the line looks like:

(lsp +peek) ; M-x vscode

Now sync the configuration and restart Emacs (using SPC h r r) and it should be working as expected. That is: once you open an Elixir file the LSP server will be started and you get code completion etc. As the LSP server has to index your project code first, it might take up to 15 minutes before it kicks in.

WARNING: Using Credo with LSP
If you use Credo, you should add this snippet in config.el so it works together with LSP:

;; Workaround to enable running credo after lsp
(defvar-local my/flycheck-local-cache nil
(defun my/flycheck-checker-get (fn checker property)
  (or (alist-get property (alist-get checker my/flycheck-local-cache))
      (funcall fn checker property)))
(advice-add 'flycheck-checker-get :around 'my/flycheck-checker-get)
(add-hook 'lsp-managed-mode-hook
          (lambda ()
            (when (derived-mode-p 'elixir-mode)
              (setq my/flycheck-local-cache '((lsp . ((next-checkers . (elixir-credo)))))))
            ))

Optional: Configuration snippets

This snippet or parts of it can be placed in config.el without installing additional packages.

;; Configure elixir-lsp
;; replace t with nil to disable.
(setq lsp-elixir-fetch-deps nil)
(setq lsp-elixir-suggest-specs t)
(setq lsp-elixir-signature-after-complete t)
(setq lsp-elixir-enable-test-lenses t)

;; Compile and test on save
(setq alchemist-hooks-test-on-save t)
(setq alchemist-hooks-compile-on-save t)

;; Disable popup quitting for Elixir’s REPL
;; Default behaviour of doom’s treating of Alchemist’s REPL window is to quit the
;; REPL when ESC or q is pressed (in normal mode). It’s quite annoying so below
;; code disables this and set’s the size of REPL’s window to 30% of editor frame’s
;; height.
(set-popup-rule! "^\\*Alchemist-IEx" :quit nil :size 0.3)

;; Do not select exunit-compilation window 
(setq shackle-rules '(("*exunit-compilation*" :noselect t))
      shackle-default-rule '(:select t))

;; Set global LSP options
(after! lsp-mode (
setq lsp-lens-enable t
lsp-ui-peek-enable t
lsp-ui-doc-enable nil
lsp-ui-doc-position 'bottom
lsp-ui-doc-max-height 70
lsp-ui-doc-max-width 150
lsp-ui-sideline-show-diagnostics t
lsp-ui-sideline-show-hover nil
lsp-ui-sideline-show-code-actions t
lsp-ui-sideline-diagnostic-max-lines 20
lsp-ui-sideline-ignore-duplicate t
lsp-ui-sideline-enable t))

Installing packages
This is done differently than native Emacs and Spacemacs!

Package management Doom Emacs

Most Liked

slouchpie

slouchpie

The OP is a good intro to setting up emacs.

One other thing I would add is this. If you are getting some warning like “lsp is scanning 5000 files in current project…consider adding to ignore list”, try adding this to config.el:

(setq lsp-file-watch-ignored
      '(".idea" ".ensime_cache" ".eunit" "node_modules"
        ".git" ".hg" ".fslckout" "_FOSSIL_"
        ".bzr" "_darcs" ".tox" ".svn" ".stack-work"
        "build" "_build" "deps" "postgres-data")
      )

That’s just some files/directories that LSP will now ignore.

Note that in doom emacs you can easily get to config.el with key command SPC f P (doom/open-private config). You could have found this key command using ALT+X and typing, e.g. “doom config”.

Once you’re up and running, there are some really really nice things you can do.

For example, in an elixir file you should be in elixir-mode and you can use this key command: SPC m t b to run all tests in project (it runs mix test).

This is pretty cool - it will run the tests in a “compilation window” which you can browse like a normal buffer. Press enter on any file:line_number stacktrace to jump to that part of the code.

One final thing - the main reason I still love emacs so much. Press SPC u. This is a special command called universal-argument. It lets you add arbitrary arguments after any command.

For example, if you do SPC u SPC m t b then it will bring up a prompt where you can enter extra args, e.g. “–failed --max-failures=1”. This will then run mix test --failed --max-failures=1.

You can re-run the previous test command with SPC m t r.

This can all be done headlessly, because emacs can run in the shell.

BartOtten

BartOtten

As my girlfriend has just released son 3.0 and I want to finalize a lib for this young man I postpone further extension of the OP for a week. Just so you all know I don’t forget this topic, just have to limit my investments :slight_smile:

tj0

tj0

A bit of a necro, but I’ve been using Doom for a while. And I use Emacs for almost everything - The Way of Emacs .

Been using alchemist since 2016 and just upgraded to use eglot and elixir-ls. It’s much better than alchemist and eglot will ship by default with Emacs 29.

Configuration below:

packages.el

(package! elixir-mode)
(package! eglot)
(package! exunit)
(package! inf-elixir)

config.el

;; eglot configuration
(use-package elixir-mode)
(use-package eglot)
(use-package exunit)

(add-to-list
 'eglot-server-programs
 '(elixir-mode . ("sh" "/home/YOUR_HOME_DIR/.elixir-ls/release/language_server.sh")))

(add-hook
 'elixir-mode-hook
 (lambda ()
   (subword-mode)
   (eglot-ensure)
   (company-mode)
   (flymake-mode)
   (add-hook 'before-save-hook 'eglot-format nil t)
   ))
(setq inf-elixir-project-command "iex -S mix phx.server")


dimitarvp

dimitarvp

Thousand paper cuts, really. One distro is slow, another has severely different keybinds (why oh why don’t they all use Evil mode’s keys and stop trying to be so hopelessly individualistic?!), another has almost nothing to it (“hacker-oriented” :weary:).

And there’s ALWAYS something spitting warnings or errors, and every now and then it just hangs for 5 seconds…

I really give up. I am not in the “hacker culture” at the moment and haven’t been for the last 10 or so years so I seriously give up. I’ll have to muscle through a few detailed and long VIM tutorials but at least I’ll have transferable knowledge almost everywhere and will just go with NeoVim / LunarVim.

Emacs is just not for me. It keeps crapping the bed. And believe me when I tell you, I got very obsessive at one point and managed to mostly tame it. But it still has subtle failure modes (on macOS more, apparently, and I am there) and they burned me out.

slouchpie

slouchpie

I started using emacs version 30 with doom today.

I had the same problem reported above about the “tree-sitter missing language grammar”.

To fix it I pressed Alt + X (called M-x in emacs land) and typed “treesit-ins” to find command treesit-install-language-grammar.

Then I typed elixir and press enter. It said something like “I don’t know what you’re talking about, do you want to do this together, interactively?”. So I said “yes” by typing y and pressing enter.

First of all it wants a github URL. So I google “treesitter elixir language grammar github” and the first result is https://github.com/elixir-lang/tree-sitter-elixir. I paste this in and press enter, then just accept the default answers for everything else. Then tree-sitter just pulls the repo, compiles it and installs the grammar (I guess :person_shrugging: ).

I thought I was done. I closed emacs and ran doom sync just for sanity. When I re-opened emacs, it complains now about missing heex language grammar.

The same process works again. This time the github URL is https://github.com/phoenixframework/tree-sitter-heex.

TL;DR To fix treesitter missing language grammar, M-x treesit-install-language-grammar, enter “elixir”, paste https://github.com/elixir-lang/tree-sitter-elixir and press Enter 6 or 7 times. Repeat but with “heex” and https://github.com/phoenixframework/tree-sitter-heex.

P.S. Emacs 30 is great. Faster, etc. but the main “win” for me is now strings with # symbol in heex templates do not mess up formatting of the rest of the buffer. This is a tiny thing but a huge win for my state of mind.

Where Next?

Popular in Wikis Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
nicbet
Introduction Now that the language is picking up support and maturing nicely, I’d like to start a collection of common and recurring Elix...
New
axelson
With the new year I am looking at travel for this year and I’d love to base some travel around Elixir. So in the spirit of the 2017 threa...
New
Rich_Morin
I’d like to start a discussion of data serialization formats, in the context of Elixir. The rest of this note is a combination of persona...
New
swelham
Introduction After some discussion with a few other members we thought it might be an idea to start a thread where we can post about libr...
New
blackode
This is a wiki - anyone at Trust Level 1 or higher can help keep it updated. Elixir Pocket Syntax Uncommon Logical stuff of Elixir modul...
New
gmile
:httpc HTTP client is part of Erlang standard library, and as such can be easily used in Elixir code too. One particular advantage of usi...
New
BartOtten
A wiki for Doom Emacs Doom is a configuration framework for GNU Emacs. It focuses on performance and customizability. It can be a founda...
New
jdumont
Guide Using an iPad for web development can be easily split into two main parts: Setting up the iPad as a thin client Working in a remo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement