I am trying to setup vim for compiling and runing c and c++ programs right inside the editor, but these don't seem to work:
autocmd! filetype *.c nnoremap <leader>cc :!gcc -o %:p:r %<cr>
autocmd! filetype *.c nnoremap <leader>cr :!gcc -o %:p:r %<cr>:!%:p:r<cr>
autocmd! filetype *.cpp *.cc nnoremap <leader>cc :!g++ -o %:p:r %<cr>
autocmd! filetype *.cpp *.cc nnoremap <leader>cr :!g++ -o %:p:r %<cr>:!%:p:r<cr>
The FileType autocommand uses the filetype not the filename to match against. You can instead use c and cpp. See :h FileType. However your problems do not end there:
nnoremap <buffer> instead. See :h :map-localautocmd! without a group. autocmd! will remove all previously defined and add in the current autocommand. It would be best to create a group with augroup.%< instead of %:r.:h ftplugin and :h after-directoryAll together:
augroup CBuild
autocmd!
autocmd filetype c nnoremap <buffer> <leader>cc :!gcc -o %:p:r %<cr>
autocmd filetype c nnoremap <buffer> <leader>cr :!gcc -o %:p:r %<cr>:!%:p:r<cr>
autocmd filetype cpp nnoremap <buffer> <leader>cc :!g++ -o %:p:r %<cr>
autocmd filetype cpp nnoremap <buffer> <leader>cr :!g++ -o %:p:r %<cr>:!%:p:r<cr>
augroup END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With