From 7325e3b55a16dc8d72700341fffbd27be610b88c Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Thu, 2 Jul 2026 14:55:48 -0500 Subject: [PATCH] fix(dir): define "-" (up/parent) mapping earlier #40531 Problem: The dir.lua "-" mapping cannot be easily overridden (because of autocmd ordering). Solution: - Move it to defaults.lua. - Also to be extra polite: fall back to builtin `-` motion if the user disabled the `dir.lua` plugin. --- runtime/doc/vim_diff.txt | 1 + runtime/lua/vim/_core/defaults.lua | 7 +++++++ runtime/plugin/dir.lua | 4 ---- test/functional/plugin/dir_spec.lua | 12 ++++++++++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index f92499d080b..e90098d26dd 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -149,6 +149,7 @@ you never want any default mappings, call |:mapclear| early in your config. - |i_CTRL-W-default| - |CTRL-L-default| - & |&-default| +- - |dir-mappings| - Q |v_Q-default| - @ |v_@-default| - # |v_#-default| diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index b1e705411bc..2916511d399 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -129,6 +129,13 @@ do --- See |&-default| vim.keymap.set('n', '&', ':&&', { desc = ':help &-default' }) + vim.keymap.set('n', '-', function() + if vim.fn.maparg('(nvim-dir-up)', 'n') ~= '' then + return '(nvim-dir-up)' + end + return (vim.v.count == 0 and '' or vim.v.count) .. '-' + end, { expr = true, silent = true, desc = 'Open parent directory' }) + --- Use Q in Visual mode to execute a macro on each line of the selection. #21422 --- This only make sense in linewise Visual mode. #28287 --- diff --git a/runtime/plugin/dir.lua b/runtime/plugin/dir.lua index 85f3b9e6a1e..022da9c7d3c 100644 --- a/runtime/plugin/dir.lua +++ b/runtime/plugin/dir.lua @@ -18,10 +18,6 @@ vim.keymap.set('n', '(nvim-dir-reload)', function() require('nvim.dir')._reload() end, { silent = true, desc = 'Reload directory' }) -if vim.fn.mapcheck('-', 'n') == '' and vim.fn.hasmapto('(nvim-dir-up)', 'n') == 0 then - vim.keymap.set('n', '-', '(nvim-dir-up)', { silent = true, desc = 'Open parent directory' }) -end - ---@param buf integer ---@param path string ---@return boolean diff --git a/test/functional/plugin/dir_spec.lua b/test/functional/plugin/dir_spec.lua index 9bd98fac0d5..b3c31108fa6 100644 --- a/test/functional/plugin/dir_spec.lua +++ b/test/functional/plugin/dir_spec.lua @@ -144,9 +144,9 @@ describe('nvim.dir', function() assert_directory(root) end) - it('maps - to open the parent directory of the current file', function() + it('maps - to open parent directories', function() make_fixture() - n.clear({ args_rm = { '-u' } }) + n.clear({ args_rm = { '-u', '--cmd' } }) edit(file) feed('-') @@ -154,6 +154,14 @@ describe('nvim.dir', function() assert_directory(root) line_of('alpha.txt') + + n.clear({ args_rm = { '--cmd' }, args = { '--noplugin' } }) + api.nvim_buf_set_lines(0, 0, -1, false, { ' alpha', ' beta' }) + api.nvim_win_set_cursor(0, { 2, 7 }) + feed('-') + + eq({ 1, 2 }, api.nvim_win_get_cursor(0)) + eq(false, exec_lua([[return package.loaded['nvim.dir'] ~= nil]])) end) it('uses an absolute buffer name for a relative startup directory argument', function()