Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nx.autocmd

nx.autocmd.create(event, opts)

nx.autocmd.create(event, opts) -> id [alias nvim_create_autocmd]: run something whenever event fires. Returns the autocmd’s numeric id (pass it to nx.autocmd.del to remove it). event is an event name ("FileType", "BufEnter", …) or a list of names to share one handler — see the autocommand events reference for the events nxvim emits and what each carries.

opts fields:

  • callback — a function run when the event fires; OR command — an ex-command string queued instead. Provide one of the two.
  • pattern — a glob (or list of globs) the event’s match string is tested against (e.g. "*.lua", { "*.c", "*.h" }). Omitted / "*" matches all.
  • group — an augroup, by numeric id or by name (see nx.augroup.create). Ties this autocmd to the group so a later clear of that group drops it.
  • buffer — make it buffer-local: it then fires only for that buffer (and pattern is ignored). 0 resolves to the current buffer at registration time.
  • once — fire once, then auto-remove. desc — a human description.

The callback receives one table describing the event: { id, event, match, buf, file, data }id this autocmd’s id, event the event name, match the matched pattern string, buf the buffer number, file its name, and data an event-specific payload (e.g. LspAttach carries { client_id = … }), nil for most events.

nx.autocmd.create("FileType", {
  pattern = "markdown",
  callback = function(ev)
    nx.bo[ev.buf].textwidth = 80
  end,
})

Defined in autocmd.lua.

nx.autocmd.del(id)

nx.autocmd.del(id) [alias nvim_del_autocmd]: remove the autocmd with this id, so it stops firing.

Defined in autocmd.lua.

nx.autocmd.exec(event, opts)

nx.autocmd.exec(event, opts) [alias nvim_exec_autocmds]: fire event (or a list of events) manually. opts.pattern (string or list) is matched as in registration; opts.buffer supplies the buffer context (defaulting to the current snapshot buffer), and the callback’s args.file is the snapshot name when firing for it.

Defined in autocmd.lua.

nx.autocmd.get(opts)

nx.autocmd.get(opts) [alias nvim_get_autocmds]: introspect the registered autocmds — a debugging affordance for confirming what clear/del left behind. Returns a list of {id, event, group, group_name, pattern, buffer, command} entries, optionally filtered by opts.event (string or list) and opts.group (id or name). Run it interactively as :lua print(vim.inspect(nx.autocmd.get({}))).

Defined in autocmd.lua.

nx.autocmd.clear(opts)

nx.autocmd.clear(opts) [alias nvim_clear_autocmds]: remove every autocmd matching the filter — the bulk analogue of nx.autocmd.del. opts.event (string/list), opts.group (id or name), opts.buffer, and opts.pattern (string/list) all narrow the set; an empty opts clears everything. Mirrors nx.autocmd.get’s matching.

Defined in autocmd.lua.