I am learing Vim and I want have set it up as IDE by mapping F5 key to compilation command which is decided by the filetype.
My ~/.vim/ftplugin/c.vim:
map <F5> :w<CR>:!clang % -o %:r.out && ./%:r.out<CR>
My ~/.vim/ftplugin/cpp.vim:
map <F5> :w<CR>:!clang++ -ggdb -pthread -std=c++11 % -o %:r.out && ./%:r.out<CR>
When I open a C++ file (with .cpp extension) and hit F5, the command from c.vim is executed. Why is this happening?
When I remove the file c.vim, then Vim loads cpp.vim and works as expected.
The cpp ftplugin that comes with vim has the following line:
Which means it is going to source the
~/.vim/ftplugin/c.vim.A way to overcome this is to put your mappings in
~/.vim/after/ftplugin/cpp.vimand~/.vim/after/ftplugin/c.vimfiles.But your problems don't stop there:
:mapwhere you probably want to at least supply a mode.nmapnoremapso it would becomennoremap<f5>everytime you open acppand switch to acfile. You should make it local to the specific buffer.nnoremap <buffer>makeprgaccordingly so you can take advantage of:makeand the quickfix list. e.g.setlocal makprg clang\ %\ -o\ %:r.outFor more help see: