.heex files or ~H sigils.
Overview
ThePhoenix.LiveView.HTMLFormatter is a mix format plugin that automatically formats HEEx templates. It understands HTML structure, Elixir expressions, and Phoenix LiveView components.
Setup
Add it as a plugin to your.formatter.exs file:
For umbrella projects, you must also add
:phoenix_live_view to deps in the root mix.exs and add the plugin to the root .formatter.exs.Configuration Options
line_length
The maximum line length for formatting. Defaults to 98 characters (Elixir formatter default).heex_line_length
Override line length specifically for HEEx templates:migrate_eex_to_curly_interpolation
Automatically migrate single expression<%= ... %> EEx to curly braces syntax. Defaults to true.
attribute_formatters
Specify custom formatters for certain attributes:tag_formatters
Specify formatters for<style> and <script> tag contents:
Phoenix.LiveView.HTMLFormatter.TagFormatter for implementation details.
inline_matcher
List of regular expressions to determine if a component should be treated as inline:["link", "button"]. Set to [] to disable.
Formatting Behavior
Block vs Inline Elements
The formatter distinguishes between block-level and inline elements: Input:- Block elements (
<section>,<h1>) start on new lines - Inline elements (
<b>) stay on the same line if they fit
Intentional Line Breaks
The formatter preserves intentional spacing:Multi-line Attributes
Attributes are placed on separate lines when they don’t fit: Input:Multiple Newlines
Multiple consecutive blank lines are collapsed to one: Input:Elixir Expressions
Elixir expressions withdo...end are not reformatted by the HEEx formatter (but their contents are):
Input:
Inline Elements
The formatter treats these as inline elements by default:<a>,<abbr>,<acronym>,<audio><b>,<bdi>,<bdo>,<big>,<br>,<button><canvas>,<cite>,<code><data>,<datalist>,<del>,<dfn><em>,<embed><i>,<iframe>,<img>,<input>,<ins><kbd><label><map>,<mark>,<meter><noscript><object>,<output><picture>,<progress><q><ruby><s>,<samp>,<select>,<slot>,<small>,<span>,<strong>,<sub>,<sup>,<svg><template>,<textarea>,<time><u>,<tt><var>,<video><wbr>- Components matching
:inline_matcherpatterns
Inline Whitespace Handling
Inline elements are not formatted when there’s no whitespace before or after them:Skip Formatting
Use thephx-no-format attribute to prevent formatting of a specific element:
Source code:
Mix Format Integration
The formatter implements theMix.Tasks.Format behavior.
features/1
Returns the sigils and extensions this formatter handles:format/2
Formats the given HEEx source:The HEEx template source code
Formatting options including:
:line_length- Maximum line length:heex_line_length- HEEx-specific line length:sigil- The sigil being formatted (:H):attribute_formatters- Custom attribute formatters:tag_formatters- Custom tag formatters:inline_matcher- Inline component patterns
The formatted source code
Editor Support
Most editors withmix format integration will automatically format .heex files and ~H sigils:
- VS Code: Formatting HEEx templates in VS Code
- Emacs: Works with elixir-mode and mix-format
- Vim/Neovim: Works with vim-mix-format plugins