31

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 minibufexpl

There are no keymappings to any function key in my vimrc.

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
woodstok
  • 2,704
  • 3
  • 34
  • 50

2 Answers2

33

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.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • oops. I missed the top part last time.it is working now.Thanx.also , wat does noremap do? – woodstok Aug 19 '10 at 11:20
  • 2
    @Mlkhail Like `map`, but prevents using other user mappings. For example, if you have `imap a b` and `imap b c`, then pressing `a` will result in `c`. If you have `inoremap a b` then pressing `a` will result in `b` no matter whether `b` is remapped or not. I recommend never use `?map` unless you do know that you need remapping (for example, with `` mappings). – ZyX Aug 19 '10 at 11:24
  • @Zyx , thanx a lot :) f2 is up and running. – woodstok Aug 19 '10 at 11:26
  • one more thing.now , i hav set =(Press)(C-V). can i generalise the command for all function keys in one command (using some parameter), or do i hav to give them separately? – woodstok Aug 19 '10 at 11:31
  • @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 ``-`` is slightly different from the result of pressing ``-.... – ZyX Aug 19 '10 at 13:40
10
: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)

michael.kebe
  • 10,916
  • 3
  • 44
  • 62
  • i 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. – woodstok Aug 19 '10 at 09:03
  • You 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 . Then Try mapping something simpler to f2 eg :map "+yy (yank current line to paste buffer) Also can you map any function key? (yes you do type f and 2) – zzapper Aug 19 '10 at 09:41
  • Also type to see what and where f2 thinks it is set to :verbose map – zzapper Aug 19 '10 at 10:27
  • f2 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. :( – woodstok Aug 19 '10 at 10:45
  • @Mlkhail Have you seen my explanation of this behavior? – ZyX Aug 19 '10 at 10:51