mayasky
mayasky

Reputation: 1035

Using if statement inside _vimrc file?

I tried to create a function in my vim configuration file, so that I can "compile and run" according to file type ( I mainly work with Fortran, C and Python ). It did work except that each time after the compiled executable was ran, there would be a notification at bottom of my vim saying:

Error detected while processing function CompileRun: line 17: E171: Missing :endif

I have no idea what "line 17" and "E171" means because they can't be related with either source file or _vimrc file, also it seems the if statement is closed. I googled a lot without finding an answer. My function is as follows:

map <F4> : call CompileRun()<CR>

func! CompileRun()

    if &filetype == 'fortran'
    exec "w"
    exec "!gfortran % -o %<"
    exec "!%<.exe"
    exec "i" 

    elseif &filetype == 'c'
    exec "w"
    exec "!gcc % -o %<"
    exec "!%<.exe"
    exec "i"

    elseif &filetype == 'python'
    exec "w"
    exec "!python %<.py"
    exec "i"
    endif
endfunc

Upvotes: 3

Views: 3947

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32946

It's likely because one of your commands throws something.

BTW, off-topic:

  • have a look at :make and &makeprg
  • gnumake does not required any makefile for mono-file projects ; hence just: :make %< will be enough, and no need to specify any &makeprg either. (It work for sure with C, C++, and probably fortran)
  • :exec is completely useless in your calls.
  • Are you sure you want to execute :insert at the end of your functions ? Try without this call.
  • You can restrict your mapping to normal mode. It won't work in other modes like that =>

    nnoremap <F4> :call Compile()<cr>

Upvotes: 2

Related Questions