feat(exrc): search in parent directories (#33889)

feat(exrc): search exrc in parent directories

Problem:
`.nvim.lua` is only loaded from current directory, which is not flexible
when working from a subfolder of the project.

Solution:
Also search parent directories for configuration file.
This commit is contained in:
Yochem van Rosmalen
2025-05-11 18:00:51 +02:00
committed by GitHub
parent 2c07428966
commit 23bf4c0531
7 changed files with 72 additions and 12 deletions

View File

@@ -122,6 +122,7 @@ DEFAULTS
• 'statusline' default is exposed as a statusline expression (previously it
was implemented as an internal C routine).
• Project-local configuration ('exrc') is also loaded from parent directories.
DIAGNOSTICS

View File

@@ -2412,9 +2412,9 @@ A jump table for the options with a short description can be found at |Q_op|.
'exrc' 'ex' boolean (default off)
global
Enables project-local configuration. Nvim will execute any .nvim.lua,
.nvimrc, or .exrc file found in the |current-directory|, if the file is
in the |trust| list. Use |:trust| to manage trusted files. See also
|vim.secure.read()|.
.nvimrc, or .exrc file found in the |current-directory| and all parent
directories (ordered upwards), if the files are in the |trust| list.
Use |:trust| to manage trusted files. See also |vim.secure.read()|.
Compare 'exrc' to |editorconfig|:
- 'exrc' can execute any code; editorconfig only specifies settings.

View File

@@ -209,6 +209,10 @@ nvim.swapfile:
swapfile is owned by a running Nvim process. Shows |W325| "Ignoring
swapfile…" message.
nvim.find_exrc:
- VimEnter: Extend 'exrc' to also search for project-local configuration files
in all parent directories.
==============================================================================
New Features *nvim-features*

View File

@@ -925,6 +925,29 @@ do
end
end
end
vim.api.nvim_create_autocmd('VimEnter', {
group = vim.api.nvim_create_augroup('nvim.find_exrc', {}),
desc = 'Find project-local configuration',
callback = function()
if vim.o.exrc then
local files = vim.fs.find(
{ '.nvim.lua', '.nvimrc', '.exrc' },
{ type = 'file', upward = true, limit = math.huge }
)
for _, file in ipairs(files) do
local trusted = vim.secure.read(file) --[[@as string|nil]]
if trusted then
if vim.endswith(file, '.lua') then
loadstring(trusted)()
else
vim.api.nvim_exec2(trusted, {})
end
end
end
end
end,
})
end
--- Default options

View File

@@ -2076,9 +2076,9 @@ vim.bo.expandtab = vim.o.expandtab
vim.bo.et = vim.bo.expandtab
--- Enables project-local configuration. Nvim will execute any .nvim.lua,
--- .nvimrc, or .exrc file found in the `current-directory`, if the file is
--- in the `trust` list. Use `:trust` to manage trusted files. See also
--- `vim.secure.read()`.
--- .nvimrc, or .exrc file found in the `current-directory` and all parent
--- directories (ordered upwards), if the files are in the `trust` list.
--- Use `:trust` to manage trusted files. See also `vim.secure.read()`.
---
--- Compare 'exrc' to `editorconfig`:
--- - 'exrc' can execute any code; editorconfig only specifies settings.