# Format Go HTML templates


Because I was fed up with the lack of options (none), I write a tool that
can format Go HTML templates: you can find it [here](https://github.com/miekg/gotmplfmt). Includes
screenshots!

Formatting Go _text_ templates is pure madness, but the above code works well on HTML templates. If
have find issues or need more functionality open a PR against the repo.

Have this in neovim:

```vim
au FileType gohtmltmpl command! Fmt silent %!gotmplfmt
au FileType gohtmltmpl let &l:formatprg="gotmplfmt"
```

## Update 2025-05-31

In my new neovim setup (with conform.lua) I have this:

```lua
local options = {
        formatters_by_ft = {
            gotmpl = { "gotmplfmt" },
    },
}
```

and

```lua
vim.filetype.add({
        pattern = {
                [".*%.go%.tmpl"] = "gotmpl",
                [".*%.tmpl"] = "gotmpl",
        },
})
```

With the keybinding (from NvChad) `<Leader>fm` is can format this file, but I disabled format-on-save:

```lua
-- Go HTML templates
vim.api.nvim_create_autocmd("FileType", {
        pattern = "gotmpl",
        group = filetypes,
        -- disable format on save for this one, as gotmplfmt isn't perfect
        callback = function(_)
                require("conform").setup({
                        format_on_save = function() end,
                })
        end,
})
```

