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).

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_FOLDER\vimfiles on windows). Execute :helptags ~/.vim/doc on UNIX/Linux or :helptags$VIM_INSTALLATION_FOLDER\vimfiles\doc 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):

cd ~/.vim/bundle
git clone git://github.com/xuhdev/SingleCompile.git

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

nmap <F9> :SCCompile<cr>
nmap <F10> :SCCompileRun<cr>

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:

vim hello.c

Copy the following content into the buffer:

#include <stdio.h>

int main(void)
{
printf("Hello, SingleCompile!n");
return 0;
}

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:

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 :SCChooseCompiler 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:

#include <stdio.h>

int main(void)
{
printf("Hello, SingleCompile!n");
retur 0; /* a typo is here: the "return" loses its "n" */
}

Execute :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 :cope:

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.

 
 
 This entry was posted in programming tools, Vim on March 7, 2012 by Hong. 
 Post navigation Add a “Show Image” Switch for the Safari Browser on Your iPhone/iPad/iPod Touch → 18 thoughts on “Use SingleCompile to Compile and Run a Single Source File Easily in Vim” grapeot December 6, 2014 at 11:45 pm 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) Reply ↓ Skullper June 21, 2014 at 11:05 am 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? Reply ↓ Hong Xu June 21, 2014 at 8:38 pm 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. Reply ↓ Skullper June 22, 2014 at 10:32 am Thank you. “:set filetype=c” works! Reply ↓ geneg November 7, 2013 at 6:35 pm 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! Reply ↓ Hong Xu November 8, 2013 at 5:53 am Hi @geneg! The two wiki pages will be helpful for your case: https://github.com/xuhdev/SingleCompile/wiki/Adding-Compilation-Flags-for-Certain-Filetypes https://github.com/xuhdev/SingleCompile/wiki/Specify-Compilation-Flags-for-Each-Individual-Source-File Reply ↓ BaRud November 5, 2013 at 10:02 am 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? Reply ↓ Hong Xu November 6, 2013 at 6:32 am See “:h SingleCompile-showquickfixiferror” and “:h SingleCompile-showquickfixifwarning”. You may use these two options to achieve your goal. Reply ↓ kulakilam June 29, 2013 at 9:54 am 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 Reply ↓ Hong Xu June 29, 2013 at 5:25 pm Where does your gtk directory locate? Reply ↓ eric July 21, 2012 at 1:22 pm 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 Reply ↓ Hong Xu January 22, 2013 at 7:42 am 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? Reply ↓ Franz April 20, 2012 at 6:52 pm 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 Reply ↓ Hong Xu April 21, 2012 at 4:37 am  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. Reply ↓ Franz April 21, 2012 at 12:22 pm 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! Reply ↓ Hong Xu April 23, 2012 at 8:52 am 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. Reply ↓ Franz April 23, 2012 at 10:54 am 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. Hong Xu April 24, 2012 at 4:52 am 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! Leave a Reply Your email address will not be published.Comment Name Email Website Subscribe to comment * Notify me of followup comments via e-mail. You can also subscribe without commenting.