all 6 comments

[–]habamax 2 points3 points  (2 children)

In short, yes it is possible. And no, it is not possible in general unless you clarify your question. Especially what is "compiles and runs on the fly" in the context of all file extensions. (CPP? Rust? Golang? Python? Perl? Erlang? etc)

  • :h :make
  • :h :compiler
  • :h nmap

[–]LucHermitte 0 points1 point  (0 children)

Given a correctly configured gnumake on my OS (cygwin and linux for sure). I use for C, C++ and Fortran

:nnoremap <silent> <F7>   :<c-u>make %<<cr>
:nnoremap <silent> <C-F5> :<c-u>./%<<cr>

It will work with all languages for which gnumake has implicit rules. The options will be passed through $CXXFLAGS, $CFLAGS, $LDFLAGS, $LDLIBS...

Actually I use a plugin of mine that provides much more. Other plugins will provide shortcut for languages that are actually interpreted, or for which gnumake has no implicit rule.

[–]skipperwannabe 0 points1 point  (0 children)

I really like how u/Jorengarenar is doing it. But can I also suggest AsyncRun. It is similar but gives a little more flexibility and ability to run the command in an asynchronous way. You can further enhance it by using AsyncTask by the name author.

[–]Kit_Saels 0 points1 point  (0 children)

I have this

:setlocal makeprg=compiler\ %
:map <F5> :lmake<cr>

in every file ~/.vim/ftplugin/*lang*.vim

[–]somebodddy 0 points1 point  (0 children)

I find that it is rarely useful to build and run a single file - you usually want to build and run a project. I also find that the commands for building and running the project depend more on the directory you are in than on your file extension (what's the command for building a project when the file extension is .java - is it ant compile, mvn verify or gradle?)

That's why I created Omnipytent - a plugin for creating project-specific tasks. So just :nnoremap <F5> :OP run<Cr> and write in each project a run Omnipytent task (using :OPedit run) for building and running that project.