mapping function keys in vim
Solution 1
Your problem is that vim does not know what does terminal emit when you press <F2>
. On some terminals it emits something like <Esc>[12~
, so the vim quits current mode (or just beeps if it can't) (<ESC>
), does nothing ([1
: there must be some key after [
, but not 1, so it does nothing) and changes case of two letters (2~
). So, you should open .vimrc and write there the following:
set <F2>=<C-v><F2>
where <C-v><F2> means that you must press <C-v>
and then <F2>
. This line should tell the Vim the exact sequence of codes which is emitted by terminal when you press <F2>
. After that, use noremap <F2> whatever
and it should work. If it is not the only terminal that you are using, then you may want to put if $TERM==#"<C-r>=$TERM<CR>"
before this line and endif
after.
Solution 2
:map <F2> :NERDTreeToggle<CR>
After starting Vim you can look with
:map <F2>
what F2 is mapped to. It is possible that the plugins change the mapping (not visible in .vimrc)
Related videos on Youtube
woodstok
Was a software developer at Cisco. I wrote (and fixed) code for Cisco Intrusion Prevention Sensor(IPS). My work had been predominantly on C++,C and Linux. Other than that, i love to play table tennis and football,watch sitcoms and movies, and read fiction. I listen to alt-rock and acoustic-folk mostly. I started running last year and can pant my way through 10Kms now. In 2014, I plan to finish a half marathon.
Updated on July 09, 2022Comments
-
woodstok almost 2 years
I want to map my
F2
for nerdtree with the following entry:map <F2> :NERDTreeToggle<CR>
But even before that, and after saving the vimrc , whenever i press
F2
, it just switches the case of the letters on which the cursor is present. Later found out that any function key does it.F5
switches case of 5 characters and so on. Is this because of some other plugin? I presently use c.vim , snippetsEmu , surround , nerdtree , and minibufexplThere are no keymappings to any function key in my vimrc.
-
woodstok over 13 yearsi tried that.. it says no mapping found.btb, for specifying F2 , u hav to press "F" and "2" right? or do we hav to use ctrl+v?i tried both..still not working.Also I am using SSH Secure Shell Client to login to a FreeBSD server.
-
zzapper over 13 yearsYou could try loading vim without any set up gvim -u NONE -U NONE -N then in insert mode see what happens when you press the function keys should insert <f2>. Then Try mapping something simpler to f2 eg :map <f2> "+yy (yank current line to paste buffer) Also can you map any function key? (yes you do type f and 2)
-
zzapper over 13 yearsAlso type to see what and where f2 thinks it is set to :verbose map <f2>
-
woodstok over 13 yearsf2 verbose showed not mappings. and i tried running vim without any setup.f2 is still behaving wierd.mapping something similar is not working either.i do feel it is some problem with my linux machine , since in my home laptop , function key mappings were working fine. i jus dont know how to figure out wat is wrong. :(
-
ZyX over 13 years@Mlkhail Have you seen my explanation of this behavior?
-
woodstok over 13 yearsoops. I missed the top part last time.it is working now.Thanx.also , wat does noremap do?
-
ZyX over 13 years@Mlkhail Like
map
, but prevents using other user mappings. For example, if you haveimap a b
andimap b c
, then pressinga
will result inc
. If you haveinoremap a b
then pressinga
will result inb
no matter whetherb
is remapped or not. I recommend never use?map
unless you do know that you need remapping (for example, with<Plug>
mappings). -
woodstok over 13 years@Zyx , thanx a lot :) f2 is up and running.
-
woodstok over 13 yearsone more thing.now , i hav set <F2>=(Press)(C-V)<F2>. can i generalise the command for all function keys in one command (using some parameter), or do i hav to give them separately?
-
ZyX over 13 years@Mlkhail Only separately. You can automate that using
:while
and:execute
, but you have to be sure that this is correct. In most of my terminal emulators the result of pressing<F1>
-<F4>
is slightly different from the result of pressing<F5>
-....