3

When making a custom keybinding, it's always in the back of my mind that I might be conflicting with some other keybinding, especially one from a (not loaded) plugin. Hence the act of pressing the keybinding to see if it currently does anything won't guarantee that it's available.

Is there any way to definitively know for a given Vim/Neovim setup (config plus plugins) whether a specific keybinding would conflict with any other?

2
  • 2
    In addition to the great answers below, you might also be interested in :help map-which-keys.
    – Friedrich
    Commented May 25 at 10:58
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented May 26 at 5:14

2 Answers 2

3

You can use :help maparg() to get information on a specific mapping (or abbreviation) and :help maplist() to get a list of all mappings (or abbreviations). Both can be used to condition a mapping.

For example, with maparg():

if maparg('<S-Tab>', 'c', 0, 1)->empty()
    cnoremap <expr> <S-Tab> getcmdtype() =~ '[\/?]' ? "<C-t>" : "<S-Tab>" 
endif

where we check if there is already something mapped to <S-Tab> in command-line mode before doing our mapping.

Note that the built-in commands, like w in normal mode or <C-a> in insert mode (just a couple of examples from the top of my head, the list is huge, see :help index), ARE NOT MAPPINGS and therefore can't be "checked" before creating a custom mapping. The two functions mentioned above only work for actual mappings.

Note also that, as was pointed out in the other answer, a mapping defined in a script that hasn't been sourced is a mapping that doesn't exist. Vim can only know about mappings that exist in memory at the time you call maparg()/maplist() or do :map, so you will have to put a bit of effort into writing your own function that scans your :help 'runtimepath' in search of mappings if you want to cover all your bases. And even then, you will have to take filetype plugins into account, which makes it a little bit more complex.

And then, there is the problem of <leader> which is expanded when the map <leader>… command is executed, with the current value of :help mapleader. The mapping is registered with the expanded <leader>, not the string <leader>, and <leader> can change value between two expansions anyway, so <leader> mappings are hard to check reliably.

Now, opinions…

Your concerns may kind of vaguely make sense from a plugin authorship perspective but, even then, the right thing to do is to expose <Plug> mappings and let the user create their own mappings anyway. Forcing mappings on your users is not exactly a good practice.

From a user perspective, worrying about overriding existing mappings (or even existing built-in commands) is largely pointless. As a user, I know the mappings I use so there is zero chance that I willingly or unwillingly override any of them. If I accidentally override a mapping I don't know, it means that I never used it in the first place: the override will have zero impact on my workflow, therefore it is harmless and I don't have to check anything.

3
  • 1
    I thought about writing an answer to the effect of your last paragraph, I upvoted instead. I find it helpful to be as close as possible to the target environment. E.g. when thinking about a mapping for Python, do :edit foo.py and be reasonably sure that filetype plugins, autocommands, etc. have fired.
    – Friedrich
    Commented May 25 at 10:56
  • 1
    Typo? arglist instead of maplist in the first paragraph
    – D. Ben Knoble
    Commented May 25 at 15:10
  • Faulty chain of thoughts: "maparg" > "arglist". Fixed.
    – romainl
    Commented May 25 at 15:15
2

If it is not loaded there no ways since Vim has no information about the plugin.

But if it is you can get a list of all the mapping for a given prefix with the map command.

e.g.: The following command list all mapping that starts with m in normal mode:

:nmap m

Remark: the mapping can be buffer specific (typically some filetyoe mapping). You should run the command on a buffer of the filetyoe for which you would like to introduce the mapping.

Remark: the command will not liist the builtin mapping. Actually Vim use nearly all keys for commands (see this answer for the list of free keys) and a fair fraction of key pairs combination (see available key pairs from @romainl)

A common recommendation is to use mapping that starts with the <leader> key or the <localleader> key to limit conflict.

Remark: the default value of the <leader> key is \ if you start using mapping that starts with it you maybe interested to change that. Popular choice for the <leader> key are Space or ,.

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.