cvimautocmd

Executing C code with F5 key in Vim


I want to save and execute my codes that's written in C programming language with just pressing F5 key in vim (in normal mode).

So I added this line to .vimrc file :

:autocmd FileType c nnoremap <F5> <Esc>:w!<CR>:!clear && gcc %<CR> && ./<CR>

But it doesn't work ! (It's make .Out file but it isn't run that file)

How can I do that's purpose with editing .vimrc file?


Solution

  • Add these lines to your .vimrc

    autocmd FileType c
       \ set makeprg=gcc\ -Wall\ %\ -o\ output |
       \ nnoremap  <special> <buffer> <F5> :w!<cr>:make<cr>:!./output
    

    With this form you use :make alone for compiling.

    Press F5 and then press Enter to execute the output.