0

I use Neovim with LazyVim configuration setup.

I want to set the mappings in which-key. So I put the following code inside ~/.config/nvim/lua/config/which-key.lua:

local wk = require("which-key")

This gives the following error:

/Users/mertnuhoglu/.config/nvim/lua/config/which-key.lua:1174: module 'which-key' not found:
    no field package.preload['which-key']
cache_loader: module which-key not found

LazyVim installs and setups which-key.nvim by default using the following code inside ~/.local/share/nvim/lazy/LazyVim/lua/lazyvim/plugins/editor.lua:

  {
    "folke/which-key.nvim",
    event = "VeryLazy",
        ...
  }

I thought maybe the error above is due to this plugin being loaded lazily. So, I did this:

    -- event = "VeryLazy",
        lazy = false,

Still, Neovim gives the same error as above.

LazyVim's documentation suggests to use the following style to customize keymaps as in https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua

map("n", "<leader>fn", "<cmd>enew<cr>", { desc = "New File" })
...

But this style is much more verbose and difficult to manage than lua tables. Therefore, I prefer to use lua tables as it is commonly used in which-key.

1 Answer 1

0

I put the following code into lua/config/keymaps.lua file instead of init.lua:

require("config/which-key")

Inside lua/config/which-key.lua I can require which-key and define the mappings as usual with which-key.

local wk = require("which-key")
wk.register(mappings, opts)

Now, which-key is loaded without any errors.

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.