packer.nvim icon indicating copy to clipboard operation
packer.nvim copied to clipboard

Allow mapping definitions in `keys` specification

Open Janfel opened this issue 3 years ago • 5 comments

Describe the feature

The keys key in a packer.use() declaration should be able to accept mapping definitions like the following, similar to the :bind key of use-package.

require("packer").startup(function()
  use {
    "some/plugin",
    keys = {
      {"n", "<Key-1>", "<Plug>DoFoo"},
      {"n", "<Key-2>", "<Plug>DoBar", silent = true},
      {"v", "<Key-3>", "<Plug>DoBaz", silent = true, remap = true},
    },
  }
end)

In addition to defining those mappings as autoloads, the config block of packer_compiled.lua should then contain the corresponding vim.keymap.set() calls. This would allow the user to keep their plugin-specific bindings in one place and have them autoload the plugin with almost no extra effort, just like they would do with use-package in Emacs.

Because the translation of mapping specs into vim.keymap.set() calls would happen at compiletime, there should be no additional startup cost to this feature.

Currently this feature can be emulated by using a config value of function(_, plugin) require("config.utils").bind(plugin.keys) end with a corresponding config.utils.bind() function, because extraneous values in a keys spec are passed along and ignored.

Janfel avatar May 31 '22 14:05 Janfel

This would be so amazing!!!!

#985 should have gotten us closer!

tricktux avatar Aug 05 '22 15:08 tricktux

#985 had to be reverted because it broke compilation for specific keymaps. I think this issue is probably best deferred till compilation is refactored/removed, currently keymaps are set as strings that are written to the output compiled lua file. Adding keys to these strings that can include characters that need to be escaped is a real pain as it stands, once these keymaps are set in lua then it will be much easier to do this.

akinsho avatar Aug 05 '22 15:08 akinsho

once these keymaps are set in lua then it will be much easier to do this.

@akinsho I would like to take a stab at a PR for this. Could you please provide quick pointers/guidance on where to start or todo items?

tricktux avatar Aug 05 '22 16:08 tricktux

@tricktux this isn't possible currently, lazy loads by key are created as string for compilation, until they are created in lua which will require v2 (a rewrite almost) of packer then you will not be able to do this as lua functions rather than strings.

akinsho avatar Aug 05 '22 16:08 akinsho

I see... Okay.

tricktux avatar Aug 05 '22 17:08 tricktux