How to create an auto command for a specific filetype in Neovim 0.7?

I want create an auto command for python filetype. If I create it in init.lua file, it will enable when neovim starts up.

Do you mean having an autocmd that’s defined only for Python files or a command that executes automatically on Python files? For the latter, you can add your commands to filetype/python.lua. For the former, you can also use that file. In the file filetype/python.lua you can do something like:

vim.api.nvim_create_autocmd({event}, {pattern = {pattern}, buffer=true})

That will cause the autocmd to be created when on that event, and buffer=true ensures that the autocmd will only be active in that buffer.

1 Like

I think you meant ftplugin/python.lua

3 Likes

I want to create an autocmd that’s only defined for Python file. Could you tell me the location of filetype?

Yes. Could you tell me where the ftplugin folder is?

.config/nvim/after/ftplugin or .config/nvim/ftplugin

1 Like

Thanks for the correction! I really need some sleep lol

Does buffer = true work? Docs only mention this:

 • buffer (integer) optional: buffer number for
buffer local autocommands |autocmd-buflocal|.
Cannot be used with {pattern}.