Use SingleCompile to Compile and Run a Single Source File Easily in Vim

Although Vim itself has already been a very powerful text editor, its plugins make it even better. SingleCompile is a plugin aimed at making it more convenient to compile or run a single source file without leaving Vim.

Consider this situation: you’ve just written a small C file (or small python script) with Vim for some tiny test, then you need to use :!gcc %:p to compile the C source file and run the executable with :!./a.out command (Or use :!python %:p to run the python script). Although a key mapping could make this process a bit convenient, but many of Vim’s advanced features will become unavailable, such as quickfix, compiler feature. SingleCompile was born to solve this problem, making this process more convenient and powerful than simply defining a few key mappings:

  • Compile or run the source file quickly using quickfix feature and compiler feature of Vim;
  • Auto detecting compilers and interpreters;
  • Fast switch between several installed compilers or interpreters;
  • Multi-language support;
  • Custom your own compiler/interpreter template;
  • View the result of last run command at any time(requires tee command);
  • Run the compiled program asynchronously and view the result at any time (see :SCCompileRunAsync in the help file).

Let’s see more about SingleCompile.

Installation

Just like most other Vim plugins, it’s simple: Download the SingleCompile.zip file from SingleCompile homepage and extract it to your Vim runtime directory(~/.vim on UNIX/Linux or $VIM_INSTALLATION_FOLDERvimfiles on windows). Execute :helptags ~/.vim/doc on UNIX/Linux or :helptags $VIM_INSTALLATION_FOLDERvimfilesdoc on Windows if you need to check the SingleCompile documentation. If you are using pathogen to manage your Vim plugins, use the following commands to install it (on UNIX/Linux):

The following key mappings should be very helpful for you. Insert them into your .vimrc file if you think it useful:

The above two lines will make F9 as the key to trigger the compilation and F10 to compile the source file and run.

Use SingleCompile to Compile and Run a Source File

Let’s try to create a new C source file called hello.c:

Copy the following content into the buffer:

Now execute :SCCompileRun (or press F10 if you have set the key mapping above). Then SingleCompile will automatically find a C compiler available on your system (e.g. gcc on my Linux) and use this compiler to compile your source file and run the executable file. The output should be displayed on your screen then:1

What if you want to check the result again after you press any key and the output is dismissed? Simple, use :SCViewResult command to get it back:2

If you have multiple compilers installed on your system, use :SCChooseCompiler command to switch the compiler you want to use:3

Let’s try to add some syntax errors into the source file. Change the buffer into the following:

Execute :SCCompileRun (or press F10 if you have set the key mapping above), then you will see some compilation errors displayed on the screen:4

Press any key to dismiss the error messages. Now let’s open the Vim quickfix window by executing :cope:5

See? The error messages has been put in the Vim quickfix window, which makes it quite easy to locate syntax errors!

Interpreting Languages

For interpreting languages such as python, perl, things are similar: interpreter auto detection, Quickfix, etc. But there is one notable difference: :SCCompile and :SCCompileRun do the same job for such kind of languages — they both call the interpreter to run your script, since there is no compilation required for such langauges.

More

SingleCompile is far more powerful than this. If you want to know more about SingleCompile, you may want to visit SingleCompile homepage. If you have installed SingleCompile, you could also execute :h SingleCompile within Vim to take a look at the SingleCompile documentation.

  • Franz

    Really good article, i even learned some new vim-features 😉

    I have been using your plugin for a couple of days now, and really love it. The only one thing which bothers me, is that vim is put on halt while compiling/running code — Especially the times you have to press enter to get back to your code. As far as I see it, if I run :SCCompileRun on a .m (Obj-C-File), it is compiling, then i have to press enter, then it is running, then i have to press enter, then it goes back to vim. If there is an error, the compiler is showing me its errors, i have to press enter, it goes back to kinda vim but not the real thing, it is showing a message like “SingleCompile: Compiler exit code is 1” with the errors above and then after hitting enter i go back to vim with quickfix open.

    That means, that i see the mistakes at least 2 times before i can edit my code again, which seems a little overdone. 

    I believe this can be fixed with using your Async-Mode, therefore my question:

    HOW can i use Async mode?

    I have vim with +python +python3 compiled, but i can’t get it to work, it is always showing me, that async is not available.. so i’d like to know: how can i make async available and will (might) it fix my problem?

    Again, i am very impressed by this plugin, please keep up the work!
    Franz

    •  Thanks for using SingleCompile!

      For your case, I guess it might be caused by another plugin or some code in your vimrc that uses python3 before SingleCompile is loaded. For Vim 7.3, once python3 is loaded, python2 will not be available. While SingleCompile supports python2 well, it currently does not support python3 for async stuff. If you want SingleCompile to support python3, you could post an issue here:

      https://github.com/xuhdev/SingleCompile/issues

      However, this is only a guess; to confirm the guess, start vim as usual, then run :echo has('python'). If it gives 0, it means python2 is not available, even you have compiled vim with python support.

      • Franz

        First of, many thanks for your fast reply!

        I was checking :echo has('python') and it showed 1, but i didn’t trust that and switched back to a vim version compiled without python 3 (flags +huge+python27) and: Async-Mode worked!:) 

        Besides writing some python3-Scripts i don’t need my vim to support it, there for i guess I just keep sticking with python27 in vim and use an external python interpreter with call SingleCompile#ChooseCompiler('python', 'python3')
        So, i don’t know why the echo showed 1 on has(‘python’), but with python3 disabled everything works, so thanks!

        • For personal curiosity, what vim version are you using? Are you using an early 7.3 version? If so, I recommend you upgrade your vim, then this might be solved even if +python and +python3 are both compiled.

          • Franz

            I downloaded and installed vim via Macports, so i should have the newest version. A port installed vim reports: “vim @7.3.496_0”. Currently i have switched to +huge+python32 (needed to do some python coding yesterday), and Async does not work. If i activate version @7.3.496+huge+python27 then async works.. :/ well it doesn’t bother me really, i just wanted to make sure that i didn’t miss something obvious.

          • If everything works fine for you, it’s OK for you. But I think there should be something wrong behind the scene. I’ll try to investigate it when I am accessible to a Mac. When there is any news, I’ll reply here. Anyway, you don’t have to care about this since you have made it work. Thanks for you reply!

  • eric

    I install ifort on my computer,but this SingleCompile cannot detect any compiler on my computer. Is this a bug? Or, if it is possible to assign the ifort compiler to the program i’m editing. Thanks a lot

    • Sorry for the delayed reply. I didn’t get notified on this comment. ifort should be supported by built-in templates. If it cannot be detected, the reason is probably you didn’t set the environmental variables for the compiler appropriate. Which operating system are you working on?

  • kulakilam

    when i want to compile a gtk program coded with C++ , I just get the warning : gtk/gtk.h : no such file or directory, how to solve this problem , thanks a lot

    • Where does your gtk directory locate?

  • BaRud

    Hi,
    Thanks for the SingleCompile plugin. I am facing a problem with tex files. kindly help me.
    First, everything is fine if I keep SCCompile in its default mode. But, I don’t want to get all those log and unnecessary warnings everytime, only the errors. So I have done:

    nmap :SCCompile:cw
    nmap :SCCompileRun
    autocmd Filetype tex nmap :SCCompileAF -quiet :cw

    But in this way, I am not getting the quickfix error window even when I intentionally include an error. I only get:
    :!latexmk -pdf -quiet trial.tex >/tmp/vXBOmQI/3 2>&1
    :cw
    Press ENTER or type command to continue

    What is the wayout to have a compiler option with latexmk with quickfix?

    • See “:h SingleCompile-showquickfixiferror” and “:h SingleCompile-showquickfixifwarning”. You may use these two options to achieve your goal.

  • geneg

    Many thanks for the SingleCompile tutorial. How can I get vim to automatically compile with the newer C++11? I have a mac with OS 10.8.5 and clang5.0, and I’ve installed g++4.8 with Homebrew, but whenever I build and run a file with C++11 code I get an error that it’s not compatible with C++98 regardless of the compiler I choose. If I type:
    <> it works, but I would prefer not to have to type so much every time. Thanks again!

  • Skullper

    I’ve got this error; “Current buffer’s filetype is not specified. Use ” :help ‘filetype’ ” command to see more details if you don’t know what filetype is.”
    What I should to do?

    • This means that Vim cannot detect the type of file you are editing (e.g. C source file, Python script). You can set them manually, for example “:set filetype=c”. For more details, you should check the document by typing “:h filetype”. Once filetype is set, SingleCompile would know which compiler to choose.

      • Skullper

        Thank you. “:set filetype=c” works!

  • grapeot

    Totally lifesaver! Thank you so much sir! (So glad I googled the idea before implementing my own version, which obviously won’t be as comprehensive as this masterpiece)