ayamir / nvimdots

A well configured and structured Neovim.
BSD 3-Clause "New" or "Revised" License
2.83k stars 451 forks source link

How to deal with keymap when adding a new plugin #1189

Closed ycpss91255 closed 4 months ago

ycpss91255 commented 4 months ago

Version confirmation

Following prerequisites

Neovim version

NVIM v0.9.4

Branch info

main (Default/Latest)

Minimal (user) folder structure required to reproduce the issue

lua/plugin/vim-tmux-navigator.lua
lua/configs/vim-tmux-navigator.lua
lua/keymap/vim-tmux-navigator.lua

Minimal config with steps on how to reproduce the issue

plugin/vim-tmux-navigator.lua

local custom = {}

local in_tmux = function ()
    return os.getenv("TMUX") ~= nil
end

custom["christoomey/vim-tmux-navigator"] = {
    lazy = false,
    cond = in_tmux,
    config = require("configs.vim-tmux-navigator"),
}

return custom

config/vim-tmux-navigator.lua

return function()
end

keymap/vim-tux-navigator.lua

local bind = require("keymap.bind")
local map_cr = bind.map_cr
local map_cmd = bind.map_cmd
local map_cu = bind.map_cu
-- local map_callback = bind.map_callback

return {
    ["n|<C-j>"] = map_cu("TmuxNavigateUp"):with_noremap():with_desc("tmux: Navigate Up"),
    ["n|<C-k>"] = map_cu("TmuxNavigateDown"):with_noremap():with_desc("tmux: Navigate Down"),
}

Expected behavior

I'm not sure if there is a way to modify the keymap after starting the plugin. I want to make sure I start the plugin in tmux and change the original C-j and C-k to the new function.

Additional information

No response

ayamir commented 4 months ago

You should add a new plugin by following https://github.com/ayamir/nvimdots/wiki/Usage#steps-to-add-a-new-plugin. Then add keymaps by following https://github.com/ayamir/nvimdots/wiki/Usage#modify-keymaps.

ayamir commented 4 months ago

Full example for you:

  1. lua/user/plugins/tool.lua
    
    local tool = {}
    local in_tmux = function()
    return os.getenv("TMUX") ~= nil
    end

tool["christoomey/vim-tmux-navigator"] = { lazy = true, cond = in_tmux, cmd = { "TmuxNavigateLeft", "TmuxNavigateDown", "TmuxNavigateUp", "TmuxNavigateRight", "TmuxNavigatePrevious", }, }

return tool


2. `lua/user/keymap/tool.lua`
```lua
local bind = require("keymap.bind")
local map_cu = bind.map_cu

return {
    ["n|<C-j>"] = map_cu("TmuxNavigateUp"):with_noremap():with_desc("tmux: Navigate Up"),
    ["n|<C-k>"] = map_cu("TmuxNavigateDown"):with_noremap():with_desc("tmux: Navigate Down"),
}

https://github.com/ayamir/nvimdots/assets/61657399/cba832c2-c9e2-4bea-b5df-ac140f510924

ycpss91255 commented 4 months ago

I succeeded, but this configuration also keymaps in environments other than using tmux.

ayamir commented 4 months ago

I succeeded, but this configuration also keymaps in environments other than using tmux.

just remove the cond:

local tool = {}

tool["christoomey/vim-tmux-navigator"] = {
    lazy = true,
    cmd = {
        "TmuxNavigateLeft",
        "TmuxNavigateDown",
        "TmuxNavigateUp",
        "TmuxNavigateRight",
        "TmuxNavigatePrevious",
    },
}

return tool