Detect existing key binding before creating oneFigure out which plugin is responsible for a key bindingHow can I detect whether an unlisted buffer contains a new file or an existing file?Key binding for enter key in insert modeHow do you undefine existing functions and key maps?Vim default key bindingKey binding to select the current paragraphExiting and entering insert mode again with key-binding
Looking for circuit board material that can be dissolved
Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?
How to protect bash function from being overridden?
Chain on singlespeed tight, pedals won't turn backwards very freely - is this an issue?
Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?
Why did they use ultrafast diodes in a 50 or 60 Hz bridge?
What makes a character irredeemable?
What does a textbook look like while you are writing it?
What action is recommended if your accommodation refuses to let you leave without paying additional fees?
What powers or limits devil promotion?
Young adult short story book with one story where a woman finds a walrus suit and becomes a walrus
Present participles of the verb esse
Parent asking for money after I moved out
Is elastic wiring feasable?
What's the correct way to determine turn order in this situation?
As an interviewer, how to conduct interviews with candidates you already know they will be rejected?
The answer is a girl's name (my future granddaughter) - can anyone help?
Why do popular TCP-using services have UDP as well as TCP entries in /etc/services?
Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?
Why is music is taught by reading sheet music?
Why Vegetable Stock is bitter, but Chicken Stock not?
Could Boris Johnson face criminal charges for illegally proroguing Parliament?
Subject prefixes in Bantu languages
Generating numbers with cubes
Detect existing key binding before creating one
Figure out which plugin is responsible for a key bindingHow can I detect whether an unlisted buffer contains a new file or an existing file?Key binding for enter key in insert modeHow do you undefine existing functions and key maps?Vim default key bindingKey binding to select the current paragraphExiting and entering insert mode again with key-binding
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
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
add a comment
|
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
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43
add a comment
|
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
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
key-bindings vimscript leader
edited Apr 16 at 6:33
Caleb
asked Apr 16 at 6:24
CalebCaleb
9871 gold badge11 silver badges26 bronze badges
9871 gold badge11 silver badges26 bronze badges
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43
add a comment
|
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43
add a comment
|
2 Answers
2
active
oldest
votes
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.
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
Apr 16 at 7:17
2
@Calebis
is the "strict comparison", similar to===
in JavaScript and PHP.0 == ''
is1
, which is often not what you want, but0 is ''
is0
. The#
is to explicitly match case (otherwiseignorecase
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♦
Apr 16 at 7:23
add a comment
|
silent! map <unique> lhs rhs
Works too. The <unique>
modifier issues an error if there is already a mapping for the left-hand side, which silent swallows.
add a comment
|
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/4.0/"u003ecc by-sa 4.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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
Apr 16 at 7:17
2
@Calebis
is the "strict comparison", similar to===
in JavaScript and PHP.0 == ''
is1
, which is often not what you want, but0 is ''
is0
. The#
is to explicitly match case (otherwiseignorecase
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♦
Apr 16 at 7:23
add a comment
|
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.
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
Apr 16 at 7:17
2
@Calebis
is the "strict comparison", similar to===
in JavaScript and PHP.0 == ''
is1
, which is often not what you want, but0 is ''
is0
. The#
is to explicitly match case (otherwiseignorecase
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♦
Apr 16 at 7:23
add a comment
|
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.
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.
answered Apr 16 at 7:14
Martin Tournoij♦Martin Tournoij
38.1k15 gold badges122 silver badges202 bronze badges
38.1k15 gold badges122 silver badges202 bronze badges
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
Apr 16 at 7:17
2
@Calebis
is the "strict comparison", similar to===
in JavaScript and PHP.0 == ''
is1
, which is often not what you want, but0 is ''
is0
. The#
is to explicitly match case (otherwiseignorecase
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♦
Apr 16 at 7:23
add a comment
|
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
Apr 16 at 7:17
2
@Calebis
is the "strict comparison", similar to===
in JavaScript and PHP.0 == ''
is1
, which is often not what you want, but0 is ''
is0
. The#
is to explicitly match case (otherwiseignorecase
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♦
Apr 16 at 7:23
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
Apr 16 at 7:17
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
Apr 16 at 7:17
2
2
@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♦
Apr 16 at 7:23
@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♦
Apr 16 at 7:23
add a comment
|
silent! map <unique> lhs rhs
Works too. The <unique>
modifier issues an error if there is already a mapping for the left-hand side, which silent swallows.
add a comment
|
silent! map <unique> lhs rhs
Works too. The <unique>
modifier issues an error if there is already a mapping for the left-hand side, which silent swallows.
add a comment
|
silent! map <unique> lhs rhs
Works too. The <unique>
modifier issues an error if there is already a mapping for the left-hand side, which silent swallows.
silent! map <unique> lhs rhs
Works too. The <unique>
modifier issues an error if there is already a mapping for the left-hand side, which silent swallows.
answered Apr 16 at 12:35
D. Ben KnobleD. Ben Knoble
4,5961 gold badge7 silver badges25 bronze badges
4,5961 gold badge7 silver badges25 bronze badges
add a comment
|
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Related question (from the opposite end of the stick): How can I prevent plugins from overwriting my key mappings?
– Caleb
Apr 16 at 6:43