Commentstring for terraform files not set

Hi,

I’m new to Neovim and i have been using LazyVim for the past week. I’m having an issue where when i select a text block in neovim for terraform files and press gcc to comment out the whole selected text block, i get an error like this:

(mini.comment) Option ‘commentstring’ is empty.

I was under the impression that treesitter or maybe the terraform-ls server would set this option for terraform files but that does not seem to be the case.

What is the recommended way to solve this for terraform files?

Thanks

Two ways to set filetype-specific options:

  1. (Recommended) Create a “ftplugin” file:

~/.config/nvim/after/ftplugin/terraform.lua

vim.bo.commentstring = '#%s'
  1. Use a FileType autocommand
autocmd FileType terraform setlocal commentstring=#%s

(or the Lua equivalent. The Vimscript version is much simpler though).

Neither Vim nor Nvim ship with a filetype plugin (“ftplugin”) for Terraform, so this option is not set by default. There is a plugin, which does set the 'commentstring' option. Maybe they would consider upstreaming it to Vim if you asked them to.

The vimscript version is much simpler but incase anyone is interested you can do this in pure lua like:

--fix terraform and hcl comment string
vim.api.nvim_create_autocmd("FileType", {
  group = vim.api.nvim_create_augroup("FixTerraformCommentString", { clear = true }),
  callback = function(ev)
    vim.bo[ev.buf].commentstring = "# %s"
  end,
  pattern = { "terraform", "hcl" },
})

1 Like