Atom Editor for Elixir Development
In this post, we’ll cover…
- Basics of Atom, how to add Packages
- What packages are there for Elixir?
- Short review of packages
- Recommend packages to try
- Deeper coverage of the recommended packages
This is a short guide on getting started with Elixir and setting up a development environment using the Atom Editor. I chose Atom because it is OpenSource, free, cross-platform, highly hackable and pretty darn cool to use too.
Oh, it also has packages that give good support for Elixir development.
Atom Editor Basics
Atom’s packages can be accessed here…
ctrl+,
> Install- Atom > Packages > Settings View > Install Packages/Themes
There are a number of general interest packages that are a good idea to install first thing. Some of these are in the Featured Packages.
Here are a few that I like and recommend for getting started. I include a short description of their function so you can more easily decide if it matters to you.
- minimap - SublimeText-like whole file overview in the sidebar
- atom-beautify - Multi-language code reformatting for “beautify” it. Doesn’t have Elixir support.
- Git related
- git-plus - Nice git support for committing, pushing, pulling, changing branches, etc.
Atom Editor and Elixir Integration
The choices of Elixir-related packages is growing and changing. Since we are still in the early days of Elixir adoption, it is worth re-visiting the list of packages from time to time.
Packages Reviewed (as of Nov 10, 2015)
For working with Elixir code, there are a few options. Here’s an overview of where it stands at the time of this writing.
- language-elixir - Syntax support
- script - Only “runs” highlighted code or a single file. Not helpful for real development. Instead just use an ‘iex’ terminal.
- build-elixir - Compiles a project. Shows errors but doesn’t link to the the broken point. Helpful for getting quick feedback on if it compiles and the error report for failure.
- elixir-cmd - Can “build” and run “tests” where output shows up in a tab.
- linter-elixirc - Displays in-line the error with parsed out error message text. Runs after saving the file. Works by compiling the project and displaying the first error it encounters. Works well.
- elixir-docs - Open a panel with Elixir documentation for the highlighted text.
- iex - Open an IEx terminal in Atom. Can easily run tests with a keystroke once the keybindings are setup.
- autocomplete-elixir - Gives interactive auto-complete support. Works for both framework code and code in the project.
Recommended Packages
Find and install the following:
- language-elixir - Syntax support
- iex - show me Supports IEx terminal as a pane in Atom. Allows running tests and more within Atom. The keybindings need to be customized. Has some minor annoyances but worth trying out.
- linter-elixirc - show me Displays in-line the error with parsed out error message text. Works well and is painless to try out.
- elixir-docs - show me Worth trying out. Still very early but I find it can be helpful.
- autocomplete-elixir - show me Runs a beam process to give interactive auto-complete.
iex Package
Appears to work well. Run an IEx terminal in a pane. Gives access to mix code directly. Easy way to run tests. Need to setup key bindings.
Fix Key bindings
In Atom, open the settings page using ctrl+,
or cmd+,
. Select “Keybindings” to see
all the keybindings available to you. You can override/customize your
keybindings in the keymap.cson file. There is a link to open it at the top.
My keymaps:
Personally, I like the ctrl-alt-l down
keybinding to open an IEx terminal
tab below my working files. I mostly just use it for running tests.
Disable or change the ones that don’t make sense or conflict.
What doesn’t work well
There are a few features of the IEx Atom package that don’t work well. At least not at this time. Feel free to disable the keybindings for those specific features.
- iex:help - With the cursor positioned at the front of a fully qualified type or function, using the keybinding will take whatever the text is and put an “h” in front of it in the console and execute it. So if the cursor is at the start of
IO.puts
it will puth IO.puts
in the console. It doesn’t work that well and the Elixir Docs package does a better job. - iex:gotoDefinition - Again, the cursor has to be at the front of a fully qualified module or function. Pressing the keybinding may sit there and do nothing or after a delay it may open the file that defines that in a new tab. It only works for things declared in the project, meaning no libraries or framework things. Sometimes it just does nothing with no error or explanation.
- iex:pretty-print - It writes
IO.puts(v(-1))
to the IEx terminal for writing out the last command (from history) in a prettier way. I guess I just can’t figure out how it supposed to work because it doesn’t work well for me. - iex:pipe - I haven’t actually figured out how this is supposed to work or what it should do.
IEx Tab
Before you can do any of the helpful things, we need an IEx terminal pane open.
Either use menu, ctrl+shift+p
or your assigned shortcut to open an IEx terminal panel.
Below is pictured the IEx tab having run the tests for the current file.
Reloading IEx (for code changes)
When you have an IEx terminal open and you’ve made code changes that you want to be available to play with, you need to “reload” the changes.
ctrl+shift+e
Running your tests
Using one of the IEx shortcuts for running you tests, do that. It outputs on the first line the command used to do it.
Running All Tests
ctrl+alt+a
Running a test file
ctrl+alt+x
Running a specific test
ctrl+alt+j
linter-elixirc Package
A simple automatic package that compiles the project when a file is saved, parses any compiler errors and highlights the line with the error and adds a bubble with the compiler error message.
It requires the package “linter” to report the errors through, so make sure to install that too.
Oops. Looks like a Rails hash habit crept in there, it should have been “{name, pid}”.
elixir-docs Package
By default, the keymap it uses for toggling the documentation panel conflicts with other
keymaps for me. Needs some work, like an ability to close the documentation panel without
a keyboard or ctrl+shift+p
interaction. But here’s what it does and why it may be
worth trying out.
In IEx, the h
command will output the help for the fully namespaced function or type.
Example of help on the IO
module.
Example of help on the IO.puts
function.
The elixir-docs package is doing the same thing. It is apparent that it taking the IEx help output, formatting it as HTML and putting it in a pane in Atom. Still, this can be helpful. Note it will also work for your own documentation.
Using Elixir Docs
Currently, the docs pane must be opened. This can be toggled through the Atom Command Palette or
configuring a keybinding. Once open, select a fully namespaced type or function
and use the keybinding to activate the documentation for it. (Linux is ctrl+alt+i
).
Selecting the function…
The displayed documentation…
autocomplete-elixir Package
The Autocomplete-elixir package is a great one. It runs a BEAM that compiles your project and can inspect it. It shows an auto-complete popup with context-aware options for completing your statement.
Important Settings
To get it working, there are is a currently not-well-documented setting that needs
to be set. In the autocomplete-elixir
package’s Settings, there are two inputs
for giving the path to your Elixir and Erlang directory.
As show, these are the settings for my Arch Linux system. /usr/bin
. For Mac it may
be /usr/local/bin
. To find out, on a terminal type:
Then enter the path not including the binary program elixir
and erl
. So it is
just the directory portion. Currently this isn’t documented well and it doesn’t give
a friendly UI error if you enter an invalid directory.
Instead, if in Atom you View > Developer > Toggle Developer Tools, you might see
many errors about the path not being a directory. Additionally, on my system it spawned
a bunch of beam.smp
processes that I had to kill.
What it Does
Once the settings are correct, this is how it works.
Start typing and if your Autocomplete is set to auto-suggest (the default), then you should see it suggest some options…
As you go further, it continues. Notice at the bottom it tries to show the @spec
information for the highlighted function.
When you make the final selection, it populates suggested arguments which remain selected so as you type they are automatically replaced.
This package is my favorite. It helps prevent simple typos and helps me discover modules and functions.
Conclusion
Elixir is still a young language. Atom is still a young tool. However, both are turning into mature platforms and something worth investigating further. Atom has a number of packages (aka plugins) that make working with Elixir more pleasant. While there are still things to improve in Atom, I’ve been enjoying it a lot.
Atom is OpenSource, cross-platform, and free. Also, it is the most hackable editor I’ve ever used. You can really dig in and tweak it to your liking.
Give Atom a try! Download it for your platform on Atom.io