Home Explore Blog CI



neovim

50th chunk of `runtime/doc/api.txt`
0d5d842f80838e2198054da6be00c1777c72492b797be88a0000000100000fd4
 autocommands.
                  • NOTE: Cannot be used with {pattern}
                • group: (string|int) The augroup name or id.
                  • NOTE: If not passed, will only delete autocmds not in any
                    group.

nvim_create_augroup({name}, {opts})                    *nvim_create_augroup()*
    Create or get an autocommand group |autocmd-groups|.

    To get an existing group id, do: >lua
        local id = vim.api.nvim_create_augroup('my.lsp.config', {
            clear = false
        })
<

    Attributes: ~
        Since: 0.7.0

    Parameters: ~
      • {name}  String: The name of the group
      • {opts}  Dict Parameters
                • clear (bool) optional: defaults to true. Clear existing
                  commands if the group already exists |autocmd-groups|.

    Return: ~
        Integer id of the created group.

    See also: ~
      • |autocmd-groups|

nvim_create_autocmd({event}, {opts})                   *nvim_create_autocmd()*
    Creates an |autocommand| event handler, defined by `callback` (Lua
    function or Vimscript function name string) or `command` (Ex command
    string).

    Example using Lua callback: >lua
        vim.api.nvim_create_autocmd({'BufEnter', 'BufWinEnter'}, {
          pattern = {'*.c', '*.h'},
          callback = function(ev)
            print(string.format('event fired: %s', vim.inspect(ev)))
          end
        })
<

    Example using an Ex command as the handler: >lua
        vim.api.nvim_create_autocmd({'BufEnter', 'BufWinEnter'}, {
          pattern = {'*.c', '*.h'},
          command = "echo 'Entering a C or C++ file'",
        })
<

    Note: `pattern` is NOT automatically expanded (unlike with |:autocmd|),
    thus names like "$HOME" and "~" must be expanded explicitly: >lua
        pattern = vim.fn.expand('~') .. '/some/path/*.py'
<

    Attributes: ~
        Since: 0.7.0

    Parameters: ~
      • {event}  (string|array) Event(s) that will trigger the handler
                 (`callback` or `command`).
      • {opts}   Options dict:
                 • group (string|integer) optional: autocommand group name or
                   id to match against.
                 • pattern (string|array) optional: pattern(s) to match
                   literally |autocmd-pattern|.
                 • buffer (integer) optional: buffer number for buffer-local
                   autocommands |autocmd-buflocal|. Cannot be used with
                   {pattern}.
                 • desc (string) optional: description (for documentation and
                   troubleshooting).
                 • callback (function|string) optional: Lua function (or
                   Vimscript function name, if string) called when the
                   event(s) is triggered. Lua callback can return a truthy
                   value (not `false` or `nil`) to delete the autocommand, and
                   receives one argument, a table with these keys:
                                                                  *event-args*
                   • id: (number) autocommand id
                   • event: (string) name of the triggered event
                     |autocmd-events|
                   • group: (number|nil) autocommand group id, if any
                   • file: (string) <afile> (not expanded to a full path)
                   • match: (string) <amatch> (expanded to a full path)
                   • buf: (number) <abuf>
                   • data: (any) arbitrary data passed from
                     |nvim_exec_autocmds()|                       *event-data*
                 • command (string) optional: Vim command to execute on event.
                   Cannot be used with {callback}
                 • once (boolean) optional: defaults to false. Run the
                   autocommand only once |autocmd-once|.
                 • nested (boolean) optional: defaults to false. Run nested
                   autocommands |autocmd-nested|.

    Return: ~
        Autocommand id (number)

Title: Neovim API: Autocommand Creation and Management (Continued)
Summary
This section details the `nvim_create_autocmd` function, which creates event handlers (autocommands) in Neovim. It explains how to define the handler using either a Lua function or a Vimscript command, and how to specify options such as the event, pattern, buffer, group, description, and whether it should run once or be nested. The section also provides examples of using Lua callbacks and Ex commands as handlers. The function returns the autocommand ID.