vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, {
pattern = '*.html',
group = mygroup,
command = 'set shiftwidth=4',
})
vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, {
pattern = '*.html',
group = 'vimrc', -- equivalent to group=mygroup
command = 'set expandtab',
})
<
Autocommand groups are unique for a given name, so you can reuse them, e.g.,
in a different file:
>lua
local mygroup = vim.api.nvim_create_augroup('vimrc', { clear = false })
vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, {
pattern = '*.c',
group = mygroup,
command = 'set noexpandtab',
})
<
------------------------------------------------------------------------------
Deleting autocommands *lua-guide-autocommands-delete*
You can use `vim.api.`|nvim_clear_autocmds()| to remove autocommands. This
function takes a single mandatory argument that is a table of keys describing
the autocommands that are to be removed:
>lua
-- Delete all BufEnter and InsertLeave autocommands
vim.api.nvim_clear_autocmds({event = {"BufEnter", "InsertLeave"}})
-- Delete all autocommands that uses "*.py" pattern
vim.api.nvim_clear_autocmds({pattern = "*.py"})
-- Delete all autocommands in group "scala"
vim.api.nvim_clear_autocmds({group = "scala"})
-- Delete all ColorScheme autocommands in current buffer
vim.api.nvim_clear_autocmds({event = "ColorScheme", buffer = 0 })
<
Note: Autocommands in groups will only be removed if the `group` key is
specified, even if another option matches it.
------------------------------------------------------------------------------
See also
• |nvim_get_autocmds()|: return all matching autocommands
• |nvim_exec_autocmds()|: execute all matching autocommands
==============================================================================
User commands *lua-guide-commands*
|user-commands| are custom Vim commands that call a Vimscript or Lua function.
Just like built-in commands, they can have arguments, act on ranges, or have
custom completion of arguments. As these are most useful for plugins, we will
cover only the basics of this advanced topic.
------------------------------------------------------------------------------
Creating user commands *lua-guide-commands-create*
User commands can be created via |nvim_create_user_command()|. This function
takes three mandatory arguments:
• a string that is the name of the command (which must start with an uppercase
letter to distinguish it from builtin commands);
• a string containing Vim commands or a Lua function that is executed when the
command is invoked;
• a table with |command-attributes|; in addition, it can contain the keys
`desc` (a string describing the command); `force` (set to `false` to avoid
replacing