which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

[Q] custom 'whichkey' for filetype ?

Open paum3 opened this issue 2 years ago • 4 comments

Hi,

I am just writing some docs in latex, and dreaming, that it could be very useful to define 'whichkey' setting for tex filetype, in my case. Is that possibile ?

thanks

paum3 avatar May 01 '22 19:05 paum3

You could attach buffer-local mappings when you open a buffer as part of your autocommands.

This is a common setup for LSP clients; they call on_attach with a buffer number from an autocommand and then you can set up keymaps on that buffer. If you place these mappings in a function it should be very easy to call it on any number of filetypes you want.

Mange avatar May 04 '22 14:05 Mange

Here's how I do it to vimwiki filetype, btw.

Setup autocommand:

https://github.com/Mange/dotfiles/blob/8a35ec89cf18dd65760405feca999041a533ae6c/config/nvim/lua/mange/autocommands/init.lua#L52-L64

(I'm using an older plugin for autocommands in Lua, not the new API in Neovim 0.7. I intend to move over soon, but the link remains a permalink to my old plugin usage. The idea is the same, however.)

Have an exported function that sets everything up:

https://github.com/Mange/dotfiles/blob/8a35ec89cf18dd65760405feca999041a533ae6c/config/nvim/lua/mange/mappings.lua#L594-L628

(I have my own wrapper method called wk_register that just makes sure my config doesn't crash if which-key isn't installed, but the signature is the same; I just check for the plugin before calling it. See source at the top of the file if you need to.)

Mange avatar May 04 '22 15:05 Mange

Hi @Mange, thank you for your ideas. Yes, I know that I can resolve this with autocommands. My idea was to use 'whichkey' plugin for that, so I'll don't need to remember all the hack I'll implement :)

paum3 avatar May 06 '22 13:05 paum3

This was briefly brought up in #165, (although with no input from the maintainer) but it would be great if a new opt was added called filetype that could perhaps support specifying an array of filetypes to apply the mappings to.

Additionally, it would likely make sense that the mapping specified by filetype would take precedence over broader, global mappings set by which-key, regardless of where they are registered.

I was doing some experimenting, and I thought perhaps I could get it to work by putting a file in after/ftplugin with some which-key bindings, but I couldn't get it to work with my intended filetype (neo-tree). I'm not sure if that's because of my own understanding of how after/ftplugin works, or if it's just not supported. Any direction here would definitely be appreciated!

macintacos avatar Dec 28 '22 02:12 macintacos