ayamir / nvimdots

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

How to modify keymap for Vim plugin #1220

Closed Penguin-SAMA closed 2 months ago

Penguin-SAMA commented 2 months ago

Version confirmation

Following prerequisites

Neovim version

NVIM v0.9.5

Branch info

main (Default/Latest)

Minimal (user) folder structure required to reproduce the issue

.
├── configs
│   ├── visual-multi
│   │   └── visual-multi.lua
├── keymap
│   ├── tool.lua
├── plugins
│   ├── visual-multi.lua
└── settings.lua

Minimal config with steps on how to reproduce the issue

I use the vim-visual-multi plugin and originally I filled in the configuration file as instructed in #901 (shown below)

user/plugins/visual-multi.lua

local custom = {}

custom["mg979/vim-visual-multi"] = {
    lazy = true,
    event = "BufReadPost",
    config = require("configs.visual-multi.visual-multi"),
}

return custom

user/configs/visual-multi/viusal-multi.lua

return function()
    vim.g.VM_maps["Find Under"] = "<C-m>" -- replace C-n
    vim.g.VM_maps["Find Subword Under"] = "<C-m>" -- replace visual C-n
    vim.g.VM_mouse_mappings = 1
end

However, after the recent update to v3.6.0, the configuration was found to be invalid. Then I looked at #1189 and the wiki link there. I changed the configuration to the following code: user/plugins/visual-multi.lua

local custom = {}

custom["mg979/vim-visual-multi"] = {
    lazy = true,
    event = "BufReadPost",
    config = require("configs.visual-multi.visual-multi"),
    cmd = {
        "Find Under",
        "Find Subword Under",
    },
}

return custom

user/configs/visual-multi/visual-multi.lua

return function() end

user/keymap/tool.lua

local bind = require("keymap.bind")
local map_cu = bind.map_cu

return {
    ["n|<C-d>"] = map_cu("Find Under"):with_noremap(),
    ["n|<C-d>"] = map_cu("Find Subword Under"):with_noremap(),
}

But this still doesn't seem to work, I think maybe it's the configuration code of the vim plugin that is different, but I don't know how I should modify my configuration.

Expected behavior

I can use <C-d> for word selection normally

Additional information

No response

ayamir commented 2 months ago

Not user/plugins/visual-multi.lua but user/plugins/editor.lua.

Penguin-SAMA commented 2 months ago

Now my config is as follows, but it still doesn't work.

user/plugins/editor.lua

custom["mg979/vim-visual-multi"] = {
    lazy = true,
    event = "BufReadPost",
    config = require("configs.visual-multi.visual-multi"),
}

user/configs/visual-multi/visual-multi.lua

return function()
    vim.g.VM_maps["Find Under"] = "<C-d>" -- replace C-n
    vim.g.VM_maps["Find Subword Under"] = "<C-d>" -- replace visual C-n
    vim.g.VM_mouse_mappings = 1
end
CharlesChiuGit commented 2 months ago

https://github.com/ayamir/nvimdots/wiki/Usage#modify-keymaps

Penguin-SAMA commented 2 months ago

Sorry, I've looked at this wiki page and I've tried the following two modifications and it still doesn't work. I don't know much about the configuration method of vim plug-in, so I don't know how to fill in the keymap.

user/configs/visual-multi/visual-multi.lua

return function()
    vim.g.VM_maps["Find Under"] = "<C-d>" -- replace C-n
    vim.g.VM_maps["Find Subword Under"] = "<C-d>" -- replace visual C-n
    vim.g.VM_mouse_mappings = 1
end

user/keymap/tool.lua

local bind = require("keymap.bind")
local map_cu = bind.map_cu

return {
    ["n|<C-d>"] = map_cu("Find Under"):with_noremap(),
    ["n|<C-d>"] = map_cu("Find Subword Under"):with_noremap(),
}
CharlesChiuGit commented 2 months ago

user/keymap/tool.lua

local bind = require("keymap.bind")
local map_cu = bind.map_cu

return {
    ["n|<C-d>"] = map_cu("Find Under"):with_noremap(),
-   ["n|<C-d>"] = map_cu("Find Subword Under"):with_noremap(),
+   ["v|<C-d>"] = map_cu("Find Subword Under"):with_noremap(),
}
Penguin-SAMA commented 2 months ago

Still not working. After modification, <C-d> will jump to the last line of the file, and the shortcut key for visual-multi is still <C-n>.

CharlesChiuGit commented 2 months ago

r u using <C-d> under visual mode?

CharlesChiuGit commented 2 months ago

oh wait, why do u use "Find Under" directly in the keymaps directly, that should be a function.

CharlesChiuGit commented 2 months ago

u should use following command instead: https://github.com/mg979/vim-visual-multi/blob/e2ff111f123da6cf97f95b96b10eb95854f953c9/doc/vm-ex-commands.txt#L8-L21

Penguin-SAMA commented 2 months ago

I think the key code should be the following part? https://github.com/mg979/vim-visual-multi/blob/e2ff111f123da6cf97f95b96b10eb95854f953c9/doc/vm-mappings.txt#L23-L29

So should I change the code to the following style?(But it still doesn't work) user\keymap\tool.lua

local bind = require("keymap.bind")
local map_cu = bind.map_cu

return function()
    vim.g.VM_maps["Find Under"] = "<C-d>" -- replace C-n
    vim.g.VM_maps["Find Subword Under"] = "<C-d>" -- replace visual C-n
    vim.g.VM_mouse_mappings = 1
end

I just want to change visual-multi's default shortcut key to .

ayamir commented 2 months ago

Solution tested on my machine: lua/user/plugins/editor.lua

local editor = {}
editor["mg979/vim-visual-multi"] = {
    lazy = true,
    event = { "CursorHold", "CursorHoldI" },
    init = require("configs.editor.visual-multi"),
}
return editor

lua/user/configs/editor/visual-multi.lua

return function()
    vim.g.VM_default_mappings = 0
    vim.g.VM_maps = {
        ["Find Under"] = "<C-x>",
        ["Find Subword Under"] = "<C-x>",
    }
    vim.g.VM_mouse_mappings = 1
end

Not recommend register this map with <C-d> which is usually used to pagedown and it has conflicts with neoscroll. If you want to set it in nvimdots style, you can set it in this way: lua/user/keymap/editor.lua

local bind = require("keymap.bind")
local map_cmd = bind.map_cmd

return {
    ["nv|<C-x>"] = map_cmd("<Plug>(VM-Find-Under)"):with_silent():with_noremap(),
}

In fact, seems all of vimscript-written plugins should use init rather than config to configure, we should correct current config.

Penguin-SAMA commented 2 months ago

In fact, seems all of vimscript-written plugins should use init rather than config to configure.

Yes, you are right. thanks for your help.

And I thought it would be more helpful to provide some content on vim plugin writing in the wiki?

ayamir commented 2 months ago

And I thought it would be more helpful to provide some content on vim plugin writing in the wiki?

Done.