How to convert this viml autocommand into .lua config for neovim?

809 Views Asked by At
au FileType python let b:coc_root_patterns = ['.git', '.env', 'venv', '.venv', 'setup.cfg', 'setup.py', 'pyrightconfig.json']

I guess not many people using this config with neovim.

3

There are 3 best solutions below

0
On

See:
:help nvim_command
Than you can do...

local vscript = "au FileType python let b:coc_root_patterns = ['.git', '.env', 'venv', '.venv', 'setup.cfg', 'setup.py', 'pyrightconfig.json']"

vim.api.nvim_command(vscript)

Check afterwards in nvim command mode: :au FileType

0
On

If you are using nvim 0.7 or later, there is now native Lua api nvim_create_autocmd for this.

local api = vim.api

api.nvim_create_autocmd({'FileType'},
  pattern = 'python',
  callback = function()
    vim.b.coc_root_patterns = {'.git', '.env', 'venv', '.venv', 'setup.cfg', 'setup.py', 'pyrightconfig.json'}
  end
)

The equivalent for vim script b:xxx variable is vim.b in Lua, see also :help vim.b for more info.

0
On

I think it should be like this.

local group = vim.api.nvim_create_augroup('coc_root', { clear = true })
local events = { 'FileType' }
vim.api.nvim_create_autocmd( events, {
  pattern = 'python',
  callback = function()
    vim.bo.coc_root_patterns = { '.git', '.env', 'venv', '.venv', 'setup.cfg', 'setup.py', 'pyrightconfig.json' }
  end,
  group = group,
})

I really did not test it, let me know if it does not work.

vim.bo, autocmd