Customizing EditorConfig


We saw earlier that Neovim supports EditorConfig. Neovim's EditorConfig implementation supports adding properties allowing project-local configuration to be customized using simple Lua functions. These callbacks open up a wide range of possibilities for project-local configuration, both of Neovim itself as well as anything that can be defined in Lua, including keymaps, plugins, and user-commands.

To define new properties, create a new file called (for example) extend-editorconfig.lua in your plugins directory, which will contain the custom properties and callback functions that define their behavior.

The default properties are defined in the properties table located at:

require("editorconfig").properties

New properties can be defined by adding them to this table, as follows:

-- add a new property called my_prop to the properties table
require("editorconfig").properties.my_prop = function(bufnr, val, opts)
    -- function arguments:
    -- bufnr: the number of the current buffer
    -- val: the value that was set in the .editorconfig file
    -- opts: a table containing all properties and values set in the .editorconfig file

    -- any logic can be implemented in this function

    -- for example
    -- optional logic can determine whether this value should be set
    if opts.charset and opts.charset ~= "utf-8" then
        -- return without setting the value
        return
    end

    -- perform conditional logic based on val
    if val == "abc" then
        -- set Neovim configuration for the current buffer
        vim.b[bufnr].foo = true
    elseif val == "xyz" then
        -- we can even conditionally create a keymap
        vim.keymap.set("n", "lhs", function()
            -- implement keymap behavior
        end)
    else
        vim.b[bufnr].foo = false
    end
end

which is then called from within an .editorconfig file like this:

[*]

my_prop = abc

The ability to define custom properties and behaviors makes Neovim's .editorconfig a powerful tool for implementing project-local configuration.