elm-format
formats Elm source code
according to a standard set of rules based on the official Elm Style Guide.
It is inspired by the popular gofmt.
The benefits of elm-format
:
- It makes code easier to write, because you never have to worry about minor formatting concerns while powering out new code.
- It makes code easier to read, because there are no longer distracting minor stylistic differences between different code bases. As such, your brain can map more efficiently from source to mental model.
- It makes code easier to maintain, because you can no longer have diffs related only to formatting; every diff necessarily involves a material change.
- It saves your team time debating how to format things, because there is a standard tool that formats everything the same way.
- It saves you time because you don't have to nitpick over formatting details of your code.
elm-format . # Format all *.elm files in the current directory
elm-format Main.elm # Format a single file
elm-format Main.elm --yes # Overwrite the file without prompting
elm-format src/ Main.elm # Format the listed files and directories
elm-format --help # See other command line options
To install elm-format
:
npm install -g elm-format
or download the version appropriate for your OS from the release page,
unzip it,
and place elm-format
or elm-format.exe
(windows) on your PATH
.
You must run elm-format
from the directory that contains your elm.json
(for Elm 0.19) or elm-package.json
(for Elm 0.18),
or else you must pass the appropriate --elm-version=0.19
/--elm-version=0.18
command line argument.
Find your editor in the table below. The recommended plugin for each editor is indicated with 🏆 (trophy emoji).
Editor | Plugin | Installation | Formatting | Format on save | Configuration | Error handling |
---|---|---|---|---|---|---|
Atom | 🏆 atom-elm-format | ✅ 2 steps | ✅ | ✅ | ✅ | |
atom-beautify | ✅ | ✅ | ✅ | |||
Light Table | 🏆 elm-light | ✅ | ✅ | |||
Vim | 🏆 elm-vim | ❌ 6 steps | ✅ | ✅ | ❌ no error message | |
Emacs | 🏆 elm-mode | ❌ 4 steps | ✅ | ✅ | ||
Visual Studio Code | 🏆 Elm Tooling | ✅ | ✅ | ❌ uninformative error message | ||
Sublime Text | 🏆 Elm Language Support | ✅ 2 steps | ❔ TBD | ✅ | ❔ TBD | ❔ TBD |
JetBrains (WebStorm, etc) | 🏆 intellij-elm | ✅ | ❔ TBD | |||
TextMate | 🏆 Elm.tmbundle | ✅ 2 steps | ❔ TBD | ❔ TBD | ❔ TBD | ❔ TBD |
These tools also integrate with elm-format:
If you can simplify or improve the installation instructions or add instructions for another editor, please make a pull request.
The default behavior of elm-format
-approved plugins is to format Elm files on save.
-
Install elm-format
-
Install atom-elm-format
apm install elm-format
or use the Atom package manager in Atom's settings
-
Install elm-format
-
Install atom-beautify
apm install atom-beautify
or use the Atom package manager in Atom's settings
- Use
^⌥B
(CTRL-ALT-B
) to format a file
- Install elm-format
- Install the elm-light plugin using the Light Table plugin manager
- To format on save, edit your user keymap by performing the following:
- Click File -> Settings -> User Keymap to open the user keymap.
- Copy the following line and paste it into your keymap. Anywhere is fine as long as it is whithin the outer brackets. Ensure to save the file.
[:editor.elm "ctrl-s" :save :elm-format :elm.lint]
- Search for "App: Reload keymaps" in the Commands Window to apply the changes (or restart LightTable).
-
Install elm-format
-
If your Emacs has
package.el
(which is automatically the case for Emacs >= 24), you can installelm-mode
from the package in MELPA:-
Ensure that you have added the MELPA source in your
~/.emacs.d/init.el
:(require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
-
Install elm-mode (official instructions): Use
M-x list-packages
and chooseelm-mode
.
-
-
Add the following to your
~/.emacs.d/init.el
:(add-hook 'elm-mode-hook 'elm-format-on-save-mode)
If you are an advanced vim user and already have a preferred vim plugin installation method, you may prefer to refer to the official elm-vim installation instructions. The instructions below are for those who need a step-by-step walkthrough of how to get the plugin set up.
-
Install elm-format
-
Install vim-plug (official instructions) NOTE: if you are using neovim, you will need to refer to the official instructions.
-
Download vim-plug:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
-
Make sure
~/.vimrc
exists and has a section like this:call plug#begin('~/.vim/plugged') " ... any active plugins call plug#end()
-
-
Install elm-vim (official instructions)
- Add
Plug 'elmcast/elm-vim'
to theplug#begin
plugin section in your~/.vimrc
- Start
vim
and run:PlugInstall
- Add
-
Add the following to your
~/.vimrc
:
let g:elm_format_autosave = 1
-
Install elm-format
-
Install the extension Elm Plugin for Visual Studio Code.
-
Configure the extension to format on save:
- Find your
settings.json
file (instructions). - Add the following key-value pair to your
settings.json
:
"[elm]": { "editor.formatOnSave": true },
- Find your
- Install elm-format
- Install the Elm Language Support package.
This is for WebStorm and other JetBrains IDEs like IntelliJ and PyCharm.
-
Install elm-format
-
Install the intellij-elm plugin
-
In IntelliJ, open Settings -> Languages & Frameworks -> Elm
- Specify the path to elm-format (try the "Auto Discover" button first)
- Check the "Run when file saved?" checkbox
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
- Install Haskell ghcup following the instructions for your operating system: https://www.haskell.org/ghcup/
# check out the repo
git clone https://github.com/avh4/elm-format.git
cd elm-format
# initial setup
ghcup install ghc 8.10.4
ghcup set ghc 8.10.4
# build
./build.sh -- build
# run the built elm-format
./_build/elm-format
./build.sh