Wikia

Vim Tips Wiki

Changes: Use a function key as map leader

Edit

Back to page

(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=1146
 
|previous=1146
 
|next=1148
 
|next=1148
|created=February 23, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Martin Krischik
 
|author=Martin Krischik
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
While fine tuning my plugins, I noticed that Vim does not correctly work with maps using <tt><Leader></tt> when the mapleader is a function key like <tt><F12></tt>. You have to use <tt>:execute</tt> to get a correct mapping:
+
While fine tuning my plugins, I noticed that Vim does not correctly work with maps using <code><Leader></code> when the mapleader is a function key like <code><F12></code>. You have to use <code>:execute</code> to get a correct mapping:
   
 
<pre>
 
<pre>
Line 34: Line 34:
   
 
==Comments==
 
==Comments==
When you want to use <tt><F12></tt> as a mapleader, you must use a backslash when you define it:
+
When you want to use <code><F12></code> as a mapleader, you must use a backslash when you define it:
   
 
<pre>
 
<pre>
Line 41: Line 41:
 
</pre>
 
</pre>
   
:But then you can't use the <tt>g:mapleader</tt> for setting up menus any more. I guess the problem runs deeper.
+
:But then you can't use the <code>g:mapleader</code> for setting up menus any more. I guess the problem runs deeper.
   
 
----
 
----
 
Isn't it a much more general issue that affects all special keys? (see {{help|expr-string}})
 
Isn't it a much more general issue that affects all special keys? (see {{help|expr-string}})
   
It seems to me that special keys must always be escaped for <tt>:execute</tt> purpose, and of course not escaped when you want to use their name.
+
It seems to me that special keys must always be escaped for <code>:execute</code> purpose, and of course not escaped when you want to use their name.
   
 
----
 
----

Latest revision as of 06:09, July 13, 2012

Tip 1147 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Martin Krischik · version 6.0


While fine tuning my plugins, I noticed that Vim does not correctly work with maps using <Leader> when the mapleader is a function key like <F12>. You have to use :execute to get a correct mapping:

execute "nnoremap <unique> " . escape(g:mapleader . "1" , "\") . " :call <SID>Set_Font (1) <CR>"
execute "nnoremap <unique> " . escape(g:mapleader . "2" , "\") . " :call <SID>Set_Font (2) <CR>"
execute "nnoremap <unique> " . escape(g:mapleader . "3" , "\") . " :call <SID>Set_Font (3) <CR>"
execute "inoremap <unique> " . escape(g:mapleader . "1" , "\") . " <C-O>:call <SID>Set_Font (1) <CR>"
execute "inoremap <unique> " . escape(g:mapleader . "2" , "\") . " <C-O>:call <SID>Set_Font (2) <CR>"
execute "inoremap <unique> " . escape(g:mapleader . "3" , "\") . " <C-O>:call <SID>Set_Font (3) <CR>"

Similarly, you can se then add menus using execute:

execute "48amenu Plugin.Font.Small<Tab>" . escape(g:mapleader . "1" , "\") . " :call <SID>Set_Font (1)<CR>"
execute "48amenu Plugin.Font.Medium<Tab>" . escape(g:mapleader . "2" , "\") . " :call <SID>Set_Font (2)<CR>"
execute "48amenu Plugin.Font.Large<Tab>" . escape(g:mapleader . "3" , "\") . " :call <SID>Set_Font (3)<CR>"

The examples are taken from my font plugin, see script#1337 for full details.

CommentsEdit

When you want to use <F12> as a mapleader, you must use a backslash when you define it:

:let mapleader = "\<F12>"
:map <Leader>h :echo "Hello world."<CR>
But then you can't use the g:mapleader for setting up menus any more. I guess the problem runs deeper.

Isn't it a much more general issue that affects all special keys? (see :help expr-string)

It seems to me that special keys must always be escaped for :execute purpose, and of course not escaped when you want to use their name.


Around Wikia's network

Random Wiki