Effortless Formatting for OpenTofu Files with LazyVim

miry

Michael Nikitochkin

Posted on April 26, 2024

Effortless Formatting for OpenTofu Files with LazyVim

Here's my snippet to automatically format HCL files upon saving:

-- ~/.config/nvim/lua/plugins/hcl.lua
-- Configure automatic formatting for HCL files in NeoVim

return {
  {
    "stevearc/conform.nvim",
    opts = {
      formatters_by_ft = {
        tf = { "tfmt" },
        terraform = { "tfmt" },
        hcl = { "tfmt" },
      },
      formatters = {
        tfmt = {
          -- Specify the command and its arguments for formatting
          command = "tofu",
          args = { "fmt", "-" },
          stdin = true,
        },
      },
    },
  },
  {
    "nathom/filetype.nvim",
    config = function()
      -- Setup overrides for file extensions
      require("filetype").setup({
        overrides = {
          extensions = {
            tf = "terraform",
            tfvars = "terraform",
            tfstate = "json",
          },
        },
      })
    end,
  },
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
miry
Michael Nikitochkin

Posted on April 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related