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
- Run the compiled program asynchronously and view the result at any time
:SCCompileRunAsyncin the help file).
Let’s see more about SingleCompile.
Just like most other Vim plugins, it’s simple: Download the SingleCompile.zip
file from SingleCompile homepage and extract it to your Vim
~/.vim on UNIX/Linux or
$VIM_INSTALLATION_FOLDER\vimfiles on windows). Execute
~/.vim/doc on UNIX/Linux or
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
.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
Copy the following content into the buffer:
1 2 3 4 5 6 7
: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:
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:
If you have multiple compilers installed on your system, use
command to switch the compiler you want to use:
Let’s try to add some syntax errors into the source file. Change the buffer into the following:
1 2 3 4 5 6 7
:SCCompileRun (or press
F10 if you have set the key mapping above),
then you will see some compilation errors displayed on the screen:
Press any key to dismiss the error messages. Now let’s open the Vim
quickfix window by executing
See? The error messages has been put in the Vim quickfix window, which makes it quite easy to locate syntax errors!
For interpreting languages such as python, perl, things are similar:
interpreter auto detection, Quickfix, etc. But there is one notable difference:
: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
within Vim to take a look at the SingleCompile documentation.