stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.84k stars 110 forks source link

feature request: don't override existing keymaps with the defaults #282

Closed nkhlmn closed 8 months ago

nkhlmn commented 8 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

0.9.5

Operating system/version

macos 14.0

Describe the bug

I have C-s as my global keymap to write a buffer. When I write to an oil buffer via my keymap I get the prompt to save changes, which does not happen when executing :w manually

Sorry, I misunderstood what was happening here. I assumed that because I was getting prompted to save, that my write keymap was getting executed when in fact the default keymap to open the split was and it was prompting me because my cursor was on the new file and it was attempting to open it in a new split, which triggers the prompt.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

~~1. Setup C-s as a keymap to execute :w<cr>

  1. Open an oil buffer and make some changes
  2. Execute keymap and get prompted to save changes~~

Expected Behavior

I should not get prompted to save changes when executing my keymap.

If I have some keymaps already defined that conflict with the defaults, oil will not overwrite them.

Directory structure

No response

Repro

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
        "stevearc/oil.nvim",
        config = function()
            require("oil").setup({
              -- add any needed settings here
            })
        end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

vim.keymap.set('n', 'C-s', ':w<cr>')

Did you check the bug with a clean config?

nkhlmn commented 8 months ago

Sorry, I misunderstood what was happening here. I assumed that because I was getting prompted to save, that my write keymap was getting executed when in fact the default keymap to open the split was and it was prompting me because my cursor was on the new file and it was attempting to open it in a new split, which triggers the prompt.

That said, it would be nice if I could keep the default oil keymaps but not have them overwrite any of my already existing ones (without having to disable all default keymaps and then explicitly map the remaining ones). Maybe oil could check for existing keymaps that conflict with the defaults and not overwrite those? I would be happy to submit a PR for that if you're open to that. Either way, thanks for the excellent plugin!

stevearc commented 8 months ago

This is because Oil sets up buffer-local keymaps, one of which includes <C-s>, and those have higher priority than global keymaps. You can override the oil keymap like so:

require("oil").setup({
  keymaps = {
    ['<C-s>'] = "<CMD>write<CR>",
  },
})