Skip to content

[idea] Auto-source per-project .nvim.lua on DirChanged #284

Description

@stanfish06

What

An autocmd that sources a .nvim.lua file from the project root whenever Neovim changes directory, enabling project-specific overrides without touching the global config.

Why it fits this setup

The config already uses lcd for per-window project roots and has vim.keymap.set("n", "<leader>e", ":edit **/*") for project-relative navigation. A .nvim.lua loader makes the project-root concept actionable: each repo can carry its own LSP tweaks, formatter choices, makeprg values, or iskeyword adjustments.

Sketch

-- add to options.lua or a new lua/config/project.lua
local function load_project_config()
    local root = vim.fn.getcwd()
    local nvim_lua = root .. "/.nvim.lua"
    if vim.fn.filereadable(nvim_lua) == 1 then
        -- sandbox: only source if the file is owned by the current user
        local stat = vim.uv.fs_stat(nvim_lua)
        if stat then
            pcall(dofile, nvim_lua)
        end
    end
end

vim.api.nvim_create_autocmd({ "DirChanged", "VimEnter" }, {
    callback = load_project_config,
})

Example .nvim.lua in a project

-- project root .nvim.lua
vim.opt_local.makeprg = "cargo build 2>&1"
vim.lsp.enable("rust_analyzer")
-- disable pyright for this non-Python repo
vim.lsp.enable("pyright", false)

Security note

Only source files owned by the current user (check via vim.uv.fs_stat uid or vim.fn.getfperm), and consider a vim.fn.confirm() prompt on first load (like vim's exrc with secure). Neovim's own exrc / 'secure' options do this for .nvim.lua natively since 0.9 — worth checking if simply enabling vim.o.exrc = true is sufficient before implementing a custom loader.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions