I wish to map <D-1> to <- when i’m editing R files.
I initially edited ~/.vim/bundle/Vim-R-plugin/ftdetect/r.vim and added:
inoremap <D-1> <space><-<space>
However it was available when i was editing all types of files — i tested .py and my .vimrc.
Why does this occur?
To fix the problem, i added:
autocmd FileType r inoremap <D-1> <space><-<space>
to my .vimrc. Now the map is not there when i initially open mvim, however once i’ve opened an *.r file, the mapping exists for all my other files.
To test what was going on, i added:
autocmd FileType python inoremap <D-1> <space>==<space>
and found that when i had loaded neither type of file, <D-1> wasn’t mapped; that when i loaded an *.r file, that <D-1> gave me <- (as desired) in *.r files, as well as in all other files — and when i next opened an *.py file, that <D-1> yielded == in all files — including in the *.r files.
Experimenting, i found that whatever file type i had most recently opened would define the mapping. This is undesirable behaviour when editing more than one filetype.
What is the best practice solution to this problem?
I use MacVim and manage my bundles with vundle.
As sylvain.joyeux mentioned in his answer you should use ‘ftplugin’ directory for your mappings. But this won’t solve mapping issues. The reason why
<D-1>mapping is available for all buffers is that you define it for all buffers (globally). If you want to limit a mapping to some particular buffer you should use<buffer>special argument of:mapcommands. So your mapping command should look like this:See
:help :map-<buffer>for details.