0

I have been refactoring my nvim setup. I manage my plugins with lazy.nvim. I have vimtex plugin, with following binds:

local wk = require"which-key"
wk.register({
    l = {
        name = "VimTeX",
        l = { "<cmd>update<cr><cmd>VimtexCompile<cr>", "Compile" }, -- important line
        s = { "<cmd>update<cr><cmd>VimtexCompileSS<cr>", "Single shot compile" },
        -- . . .
    },
}, { prefix = "<leader>" })

The wk.register gets called from config field in lazy.nvim pluginspec table. Also, in other file I have setup for markdown-preview plugin, it has same keybind <leader>ll:

local wk = require"which-key"
wk.register({
    l = {
        name = "VimMarkdown",
        l = {'<Plug>MarkdownPreviewToggle', "Toggle Markdown Preview" },
    },
}, { prefix = "<leader>" })

So the problem is that when I open fresh instance of neovim, and open some latex file and then press <leader>, whichkey says that the l group has name VimTex and if I press <leader>l I see the vimtex binds for l and s.

But if I open new markdown buffer, press <leader>, the l group has the name VimMarkdown, which is correct, but the <leader>l shows that l is the markdow one bind, and s is the vimtex bind. The problem is that vimtex s bind is useless in markdown files. Also, when I switch back to latex buffer, the <leader> says that l group has the VimMarkdown name.

All this is logicall, firstly the vimtex is lazy loaded and its config pluginspec gets called, then the vim markdown gets loaded and redefines group name. But I want it to be automatic, I want the which-key show that <leader>l has VimTeX name in latex buffers, and it should say that <leader>l has the VimMarkdown group name in markdown buffers. Also the vimtex keybinds should get dismissed in non-latex buffers.

My setup can be found here. The lua/plugins/vimtex.lua contains vimtex keybinds definitions, lua/plugins/markdown.lua contains markdown-preview keybind.

Any workaround?

1 Answer 1

2

I believe the problem is that your register your which-key buffer mappings globally.

Instead of having the register call of the buffer mapping in vimrc they should probably be in the ~/.config/nvim/ftplugin/tex.lua and specify the buffer number option.

Something like:

local wk = require"which-key"

wk.register({
    l = {
        name = "VimTeX",
        l = { "<cmd>update<cr><cmd>VimtexCompile<cr>", "Compile" }, -- important line
        s = { "<cmd>update<cr><cmd>VimtexCompileSS<cr>", "Single shot compile" },
        -- . . .
    },
}, { prefix = "<leader>", buffer = vim.api.nvim_buf_get_number(0) })
2
  • 1
    I have just one more question, are there more "lazy-nvim and lua" way of doing this? I mean without separating plugin spec and it's binds. Commented May 29 at 15:04
  • 1
    It is already lua. But If you don't want to use the ftplugin you can do it using an autocmd on the Filetype event. Commented May 29 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.