Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a C program from VIM?

Tags:

c

vim

Is there a way to compile and run a C program from VIM without typing its filename ?

like image 221
Coder Avatar asked Apr 13 '10 08:04

Coder


People also ask

Can you code C in vim?

While typing C text, Vim automatically indents for you. This helps you figure out where you have your matching brace. You can match braces, ), ] and } with the % command in command mode. Simply take the cursor to a brace and press %, which takes you to the corresponding closing or opening brace.

How do I run a .C file in Linux?

Now, you should be able to run the C code by using one of the following way: Using the shortcut Ctrl+Alt+N. Press F1 and then select or type Run Code. Right click the text editor and the click Run code from context menu.

Can you run a file in vim?

Open a new or existing file with vim filename . Type i to switch into insert mode so that you can start editing the file. Enter or modify the text with your file. Once you're done, press the escape key Esc to get out of insert mode and back to command mode.


3 Answers

:!gcc -o somename % && ./somename
When using :!, % will be substituted by the name of the currently opened file.

When your project becomes larger, you can also write a makefile and compile the current project with :make, if there are any errors, vim will jump to them automatically.

like image 71
tstenner Avatar answered Oct 06 '22 12:10

tstenner


Use the following mapping code in your .vimrc file for compiling and running a c programming file.

 map <F8> : !gcc % && ./a.out <CR>

F8 key is for run the mapping. "%" is to take the current file name.

Or, if you want to save the current file before compiling it, use

map <F8> :w <CR> :!gcc % && ./a.out <CR>

Or more ideally, if you want to use the file basename not the default 'a.out' as the executable file name, use the following:

map <F8> :w <CR> :!gcc % -o %< && ./%< <CR>

In the above command, "<" after "%" removes extension and dot (foo.c => foo), so "%<" is the file basename.

You can find this and similar infos in cmdline.txt. Command in vim:help: cmdline.txt. You can also find specific details about the use of "%" by using :help filename-modifiersin vim.

like image 63
ungalnanban Avatar answered Oct 06 '22 12:10

ungalnanban


TL;DR No Makefile is required, tweaking &makeprg is also completely useless, and yet :make %< is enough to compile from Vim.

Long answer:

I duplicate an answer I gave in a closed "duplicate question".

Considering we are using vim, and not vi, :make is the way to go.

On Linux-like (it also applies to cygwin, but not to mingw on windows -- in mingw case, see the other answers that alter &makeprg, leave it alone otherwise) systems where gnumake is installed, if you don't have a Makefile in your project, and if your project is made of only one file, just type :make %<. It will be enough (you can play with $CXXFLAGS, $CFLAGS, $LDFLAGS (for -Llib/path options) and $LDLIBS (for -llibname options) to tune the compilation options). Then to run the program, type :!./%<, or with latter versions of vim, run :terminal ./%<.

If your project is made of several files, then you'll need a Makefile to take advantage of :make.

If you manage your project with CMake, and if you compile your project in a directory (or several -> debug, release, ...) outside the sources tree, then the integration will require a plugin. AFAIK, I'm the only one to propose such a plugin: BuildToolsWrapper integrates the management of CMake (choice of the build directory, possibility to chose between the debug, or release, or whatever build directory). It has to be coupled with one of the local_vimrc plugin.

In all cases, calling directly the compiler from within (or outside) Vim with :!g++ -o %< % or whatever is what we used to do 15 years ago on vi. Vim has a wonderful feature: it can integrate (yes, like in IDE) the compiler. See :h quickfix. Navigating between errors directly from the editor is much easier than extracting one error line with our eyes, typing back the line number into the editor, going back to the shell to see what exactly was rejected, ... It may be enough in C, but In C++ when we are "trying to call an overload that doesn't exist", we can't work this way (switching back and forth between the editor and the shell).

Finally, if you want to compile on a single keystroke those mono-file projects, you can add in your .vimrc:

nnoremap <silent> <f7> :make %<<cr>

If you want to adapt automatically the compilation command depending of the kind of project mono-file pet project, or real world multi-file project, well, more wiring is needed, and this is what BTW does -- it reads various options to know what to do.

Last note: &makeprg is best left alone, at least not set to g++/gcc/clang/clang++/gfortran/... Because, every time you change your language, you'll have to change it (unless you use :setlocal). With the solution I recommend, if I want to use clang++ instead of g++, all I have to do is to set: :let $CXX='clang++' (or $CC in C), and then call :make %<. I can even define :let $CXXFLAGS='-std=c++11' to compile in C++11 -- the same variable will be used to turn warnings on, to use a sanitizer, etc.

like image 14
Luc Hermitte Avatar answered Oct 06 '22 13:10

Luc Hermitte