1

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
  • `./` will not run your file; `./a.out` should. You should also use `nnoremap ` so that your other buffers don't inherit it. – Amadan Jan 31 '17 at 01:04

1 Answers1

0

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.

  • i was test it but it's not work . it just build a output file but it's not run the output file –  Jan 31 '17 at 01:16
  • Why not add `:Copen` option for easy debugging in case of error – dlmeetei Jan 31 '17 at 08:45
  • It is `:copen` with lowercase and what wants the OP actually is to run the exe file. – Meninx - メネンックス Jan 31 '17 at 08:47
  • 2
    Don't override `&makeprg`. Unless you're using mingw distribution, this is counter productive. `:make %<` is enough -- see http://stackoverflow.com/a/35702919/15934. Then the execution of the result should be conditioned to the success of the compilation. Unfortunately `v:shell_error` isn't updated with `:make`result. We have to rely on tricks that don't fit in one-line (see latex-suite, build-tool-wrapper, and a question somewhere on SO or vi.SE on the subject). Last thing: the mapping should be `` local – Luc Hermitte Jan 31 '17 at 09:24