Autocommands - nvim-neorg/neorg Wiki

The core.autocommands Module

Summary

Handles the creation and management of Neovim's autocommands.

Overview

This module exposes functionality for subscribing to autocommands and performing actions based on those autocommands.

In your module.setup(), make sure to require core.autocommands (requires = { "core.autocommands" }) Afterwards in a function of your choice that gets called after core.autocommmands gets intialized e.g. load():

module.load = function()
    module.required["core.autocommands"].enable_autocommand("VimLeavePre") -- Substitute VimLeavePre for any valid neovim autocommand
end

Afterwards, be sure to subscribe to the event:

module.events.subscribed = {

    ["core.autocommands"] = {
        vimleavepre = true
    }

}

Upon receiving an event, it will come in this format:

{
    type = "core.autocommands.events.<name of autocommand, e.g. vimleavepre>",
    broadcast = true
}

Usage

How to Apply

Configuration

No configuration provided

Developer Usage

Public API

This segment will detail all of the functions core.autocommands exposes. All of these functions reside in the public table.

Examples

Binding to an Autocommand

local mymodule = neorg.modules.create("my.module")

mymodule.setup = function()
    return {
        success = true,
        requires = {
            "core.autocommands", -- Be sure to require the module!
        },
    }
end

mymodule.load = function()
    -- Enable an autocommand (in this case InsertLeave)
    module.required["core.autocommands"].enable_autocommand("InsertLeave")
end

-- Listen for any incoming events
mymodule.on_event = function(event)
    -- If it's the event we're looking for then do something!
    if event.type == "core.autocommands.events.insertleave" then
        log.warn("We left insert mode!")
    end
end

mymodule.events.subscribed = {
    ["core.autocommands"] = {
        insertleave = true, -- Be sure to listen in for this event!
    },
}

return mymodule

Extra Info

Version

This module supports at least version 0.0.8. The current Neorg version is 0.0.9.

Imports

This module does not import any other files.

Requires

This module does not require any other modules to operate.

Required by