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.


Just like most other Vim plugins, it’s simple: Download the 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.


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.