3

Currently I'm using pattern in vim.api.nvim.create_autocmd():

vim.api.nvim_create_autocmd({ "BufEnter", "BufFilePost" }, {
  pattern = { "*.tf" },
  callback = SetTerraformMappings,
  group = terraformGrp,
})

But I would prefer to use filetype somehow instead of pattern. In vimscript I could

autocmd Filetype terraform lua SetTerraformMappings()

Also as a workaround I can just create ~/.config/nvim/after/ftplugin/terraform.vim with the mappings there:

nnoremap <buffer> <leader>t :Shell terraform plan -out latest.tfplan<cr>
nnoremap <buffer> <leader>r :Shell terraform apply latest.tfplan<cr>

But I wonder, is there an equivalent to autocmd Filetype xxx yyy in lua nvim_create_autocmd()?

Note that filetype and pattern are not exactly equivalent because filetypes sometime inspect the contents of the file to do the filetype detection.

1
  • as a workaround i'm using vim.cmd('autocmd ...').
    – vim.ggyG
    Nov 23, 2022 at 3:27

1 Answer 1

2

It is possible write is like this in lua, just use the FileType event.

(replace your file type, I don´t what is the correct one for terraform)

vim.api.nvim_create_autocmd( "FileType", {
  pattern = "yourfiletype",
  callback = SetTerraformMappings,
  group = terraformGrp,
})

You can see a demonstration by awesome TJDeVries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.