Detect existing key binding before creating one Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Figure out which plugin is responsible for a key bindingKey binding for enter key in insert modeIs there a one-key command to insert before the first character of the line?How do you undefine existing functions and key maps?Getting tab and windows movement with one key combinationVim default key bindingKey binding conflict of paste and rectangular blocks selectKey binding to select the current paragraphHow to limit a key binding to a single keyExiting and entering insert mode again with key-binding

What are the possible ways to detect skin while classifying diseases?

Why does Python start at index -1 (as opposed to 0) when indexing a list from the end?

How can I fade player character when he goes inside or outside of the area?

What's the purpose of writing one's academic bio in 3rd person?

How does a Death Domain cleric's Touch of Death feature work with Touch-range spells delivered by familiars?

ListPlot join points by nearest neighbor rather than order

What are the motives behind Cersei's orders given to Bronn?

How to recreate this effect in Photoshop?

iPhone Wallpaper?

Why are there no cargo aircraft with "flying wing" design?

Determinant is linear as a function of each of the rows of the matrix.

What do you call a plan that's an alternative plan in case your initial plan fails?

How can I make names more distinctive without making them longer?

What does '1 unit of lemon juice' mean in a grandma's drink recipe?

Proof involving the spectral radius and the Jordan canonical form

Letter Boxed validator

When is phishing education going too far?

How to do this path/lattice with tikz

Is the argument below valid?

What happens to sewage if there is no river near by?

Why constant symbols in a language?

Diagram with tikz

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

What's the difference between `auto x = vector<int>()` and `vector<int> x`?



Detect existing key binding before creating one



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Figure out which plugin is responsible for a key bindingKey binding for enter key in insert modeIs there a one-key command to insert before the first character of the line?How do you undefine existing functions and key maps?Getting tab and windows movement with one key combinationVim default key bindingKey binding conflict of paste and rectangular blocks selectKey binding to select the current paragraphHow to limit a key binding to a single keyExiting and entering insert mode again with key-binding










2















A plugin I've recently started maintaining currently checks to see if any bindings exist to its functions before creating the default ones. This makes it easy to setup custom bindings and not have to do anything about the default ones.



if !hasmapto('<Plug>MyFunction', 'n')
nmap <buffer> <Leader>f <Plug>MyFunction
endif


What this doesn't do is keep these default bindings from clobbering existing mappings. How can I detect if, say, there is an existing normal mode mapping for <leader>f before clobbering it with the plugin's default mapping.










share|improve this question
























  • Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

    – Caleb
    1 hour ago















2















A plugin I've recently started maintaining currently checks to see if any bindings exist to its functions before creating the default ones. This makes it easy to setup custom bindings and not have to do anything about the default ones.



if !hasmapto('<Plug>MyFunction', 'n')
nmap <buffer> <Leader>f <Plug>MyFunction
endif


What this doesn't do is keep these default bindings from clobbering existing mappings. How can I detect if, say, there is an existing normal mode mapping for <leader>f before clobbering it with the plugin's default mapping.










share|improve this question
























  • Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

    – Caleb
    1 hour ago













2












2








2








A plugin I've recently started maintaining currently checks to see if any bindings exist to its functions before creating the default ones. This makes it easy to setup custom bindings and not have to do anything about the default ones.



if !hasmapto('<Plug>MyFunction', 'n')
nmap <buffer> <Leader>f <Plug>MyFunction
endif


What this doesn't do is keep these default bindings from clobbering existing mappings. How can I detect if, say, there is an existing normal mode mapping for <leader>f before clobbering it with the plugin's default mapping.










share|improve this question
















A plugin I've recently started maintaining currently checks to see if any bindings exist to its functions before creating the default ones. This makes it easy to setup custom bindings and not have to do anything about the default ones.



if !hasmapto('<Plug>MyFunction', 'n')
nmap <buffer> <Leader>f <Plug>MyFunction
endif


What this doesn't do is keep these default bindings from clobbering existing mappings. How can I detect if, say, there is an existing normal mode mapping for <leader>f before clobbering it with the plugin's default mapping.







key-bindings vimscript leader






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







Caleb

















asked 1 hour ago









CalebCaleb

96211125




96211125












  • Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

    – Caleb
    1 hour ago

















  • Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

    – Caleb
    1 hour ago
















Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

– Caleb
1 hour ago





Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?

– Caleb
1 hour ago










1 Answer
1






active

oldest

votes


















3














You can use the mapcheck() function to check if the user mapped a key to something:



:echo json_encode(mapcheck('<F1>', 'n'))
""

:echo mapcheck('<F11>', 'n')
:set cursorcolumn!<CR>:set cursorcolumn?<CR>


So in your mapping, you can do:



if !hasmapto('<Plug>MyFunction', 'n') && mapcheck('<Leader>f', 'n') is# ''
nmap <buffer> <Leader>f <Plug>MyFunction
endif



Personally, I'm not completely convinced this is actually a good approach though. While it does prevent overriding user mappings, it's also very "magic" and may be surprising.



An approach that I often use is to make a single setting to disable all mappings:



if get(g:, 'myplugin_map_keys', 1)
nmap <buffer> <Leader>f <Plug>MyFunction
endif


This way users have a reasonable set of mappings by default, but can disable mappings and do their own thing if they want something custom.



Another option is to map from a variable:



exe printf('nmap <buffer> %s <Plug>MyFunction',
get(g:, 'myplugin_map_foo', '<Leader>f')


This way users can set their own keys for a specific mapping, without having to remap everything.




I don't think there is "one right way". Aside from personal preference, it also depends on the kind of plugin you're working on. For some plugins a key mapping is just an "extra bonus", whereas it's critical for others. Some plugins map just one or two keys, others dozens or even hundreds.



For smaller plugins with just one or two mappings, the first method will usually be better; for larger plugins with a lot of mappings, the second is often better. Of course, you can also do both.






share|improve this answer























  • Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

    – Caleb
    32 mins ago







  • 1





    @Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

    – Martin Tournoij
    26 mins ago












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "599"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19597%2fdetect-existing-key-binding-before-creating-one%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can use the mapcheck() function to check if the user mapped a key to something:



:echo json_encode(mapcheck('<F1>', 'n'))
""

:echo mapcheck('<F11>', 'n')
:set cursorcolumn!<CR>:set cursorcolumn?<CR>


So in your mapping, you can do:



if !hasmapto('<Plug>MyFunction', 'n') && mapcheck('<Leader>f', 'n') is# ''
nmap <buffer> <Leader>f <Plug>MyFunction
endif



Personally, I'm not completely convinced this is actually a good approach though. While it does prevent overriding user mappings, it's also very "magic" and may be surprising.



An approach that I often use is to make a single setting to disable all mappings:



if get(g:, 'myplugin_map_keys', 1)
nmap <buffer> <Leader>f <Plug>MyFunction
endif


This way users have a reasonable set of mappings by default, but can disable mappings and do their own thing if they want something custom.



Another option is to map from a variable:



exe printf('nmap <buffer> %s <Plug>MyFunction',
get(g:, 'myplugin_map_foo', '<Leader>f')


This way users can set their own keys for a specific mapping, without having to remap everything.




I don't think there is "one right way". Aside from personal preference, it also depends on the kind of plugin you're working on. For some plugins a key mapping is just an "extra bonus", whereas it's critical for others. Some plugins map just one or two keys, others dozens or even hundreds.



For smaller plugins with just one or two mappings, the first method will usually be better; for larger plugins with a lot of mappings, the second is often better. Of course, you can also do both.






share|improve this answer























  • Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

    – Caleb
    32 mins ago







  • 1





    @Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

    – Martin Tournoij
    26 mins ago
















3














You can use the mapcheck() function to check if the user mapped a key to something:



:echo json_encode(mapcheck('<F1>', 'n'))
""

:echo mapcheck('<F11>', 'n')
:set cursorcolumn!<CR>:set cursorcolumn?<CR>


So in your mapping, you can do:



if !hasmapto('<Plug>MyFunction', 'n') && mapcheck('<Leader>f', 'n') is# ''
nmap <buffer> <Leader>f <Plug>MyFunction
endif



Personally, I'm not completely convinced this is actually a good approach though. While it does prevent overriding user mappings, it's also very "magic" and may be surprising.



An approach that I often use is to make a single setting to disable all mappings:



if get(g:, 'myplugin_map_keys', 1)
nmap <buffer> <Leader>f <Plug>MyFunction
endif


This way users have a reasonable set of mappings by default, but can disable mappings and do their own thing if they want something custom.



Another option is to map from a variable:



exe printf('nmap <buffer> %s <Plug>MyFunction',
get(g:, 'myplugin_map_foo', '<Leader>f')


This way users can set their own keys for a specific mapping, without having to remap everything.




I don't think there is "one right way". Aside from personal preference, it also depends on the kind of plugin you're working on. For some plugins a key mapping is just an "extra bonus", whereas it's critical for others. Some plugins map just one or two keys, others dozens or even hundreds.



For smaller plugins with just one or two mappings, the first method will usually be better; for larger plugins with a lot of mappings, the second is often better. Of course, you can also do both.






share|improve this answer























  • Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

    – Caleb
    32 mins ago







  • 1





    @Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

    – Martin Tournoij
    26 mins ago














3












3








3







You can use the mapcheck() function to check if the user mapped a key to something:



:echo json_encode(mapcheck('<F1>', 'n'))
""

:echo mapcheck('<F11>', 'n')
:set cursorcolumn!<CR>:set cursorcolumn?<CR>


So in your mapping, you can do:



if !hasmapto('<Plug>MyFunction', 'n') && mapcheck('<Leader>f', 'n') is# ''
nmap <buffer> <Leader>f <Plug>MyFunction
endif



Personally, I'm not completely convinced this is actually a good approach though. While it does prevent overriding user mappings, it's also very "magic" and may be surprising.



An approach that I often use is to make a single setting to disable all mappings:



if get(g:, 'myplugin_map_keys', 1)
nmap <buffer> <Leader>f <Plug>MyFunction
endif


This way users have a reasonable set of mappings by default, but can disable mappings and do their own thing if they want something custom.



Another option is to map from a variable:



exe printf('nmap <buffer> %s <Plug>MyFunction',
get(g:, 'myplugin_map_foo', '<Leader>f')


This way users can set their own keys for a specific mapping, without having to remap everything.




I don't think there is "one right way". Aside from personal preference, it also depends on the kind of plugin you're working on. For some plugins a key mapping is just an "extra bonus", whereas it's critical for others. Some plugins map just one or two keys, others dozens or even hundreds.



For smaller plugins with just one or two mappings, the first method will usually be better; for larger plugins with a lot of mappings, the second is often better. Of course, you can also do both.






share|improve this answer













You can use the mapcheck() function to check if the user mapped a key to something:



:echo json_encode(mapcheck('<F1>', 'n'))
""

:echo mapcheck('<F11>', 'n')
:set cursorcolumn!<CR>:set cursorcolumn?<CR>


So in your mapping, you can do:



if !hasmapto('<Plug>MyFunction', 'n') && mapcheck('<Leader>f', 'n') is# ''
nmap <buffer> <Leader>f <Plug>MyFunction
endif



Personally, I'm not completely convinced this is actually a good approach though. While it does prevent overriding user mappings, it's also very "magic" and may be surprising.



An approach that I often use is to make a single setting to disable all mappings:



if get(g:, 'myplugin_map_keys', 1)
nmap <buffer> <Leader>f <Plug>MyFunction
endif


This way users have a reasonable set of mappings by default, but can disable mappings and do their own thing if they want something custom.



Another option is to map from a variable:



exe printf('nmap <buffer> %s <Plug>MyFunction',
get(g:, 'myplugin_map_foo', '<Leader>f')


This way users can set their own keys for a specific mapping, without having to remap everything.




I don't think there is "one right way". Aside from personal preference, it also depends on the kind of plugin you're working on. For some plugins a key mapping is just an "extra bonus", whereas it's critical for others. Some plugins map just one or two keys, others dozens or even hundreds.



For smaller plugins with just one or two mappings, the first method will usually be better; for larger plugins with a lot of mappings, the second is often better. Of course, you can also do both.







share|improve this answer












share|improve this answer



share|improve this answer










answered 35 mins ago









Martin TournoijMartin Tournoij

35.9k14111185




35.9k14111185












  • Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

    – Caleb
    32 mins ago







  • 1





    @Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

    – Martin Tournoij
    26 mins ago


















  • Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

    – Caleb
    32 mins ago







  • 1





    @Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

    – Martin Tournoij
    26 mins ago

















Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

– Caleb
32 mins ago






Thanks. Using this I searched around for other implementations and am now wondering why you used ... is# '' instead of ... == ''. Can you clarify that bit?

– Caleb
32 mins ago





1




1





@Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

– Martin Tournoij
26 mins ago






@Caleb is is the "strict comparison", similar to === in JavaScript and PHP. 0 == '' is 1, which is often not what you want, but 0 is '' is 0. The # is to explicitly match case (otherwise ignorecase setting is used). Neither are strictly needed here, but it's just a habit I've picked up after I ran in to a few bugs with ==, and I think everyone should probably be using it (just like everyone is using ===). Also see :help expr4.

– Martin Tournoij
26 mins ago


















draft saved

draft discarded
















































Thanks for contributing an answer to Vi and Vim Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f19597%2fdetect-existing-key-binding-before-creating-one%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High