New to vim and and I want to be able to compile code from within vim without running a new terminal and calling the compiler.

How do I go about doing this? Note that this requirement is not restricted to gcc only, I sometimes need to call python also on the current script I am working on, so you get the idea...


Write a Makefile, then you can simply:


from inside vim. And if compilation fails:


will show you warning and errors. Selecting them will take you to the appropriate file and line.


In vim, the current file can be referred to as %, so

:!python %

See :he cmdline-special (or here)


If you don't like makefiles for some reason you can call any command with :!

For example you can use ":!javac *.java && java Foo" to compile and run a simple java program. You can also call other build systems (scons, cmake, ...) this way.


Try the quickrun plugin for Vim. The page includes a link to github, which is the latest version and which is what I use.

The advantage in using this plugin as opposed to doing :!, the output from the command will be collected and shown in a split window.

Also, by default that plugin will hang your vim instance when you ask it to execute a command, but it can be configured to run the command asynchronously, which is what I do. Read the documentation for more details.


I use a vim that has Python interpreter compiled in.

I source a python file that has this function:

def pyterm(filename="", interactive=1):
    cmd = "%s %s %s " % (PYTHON, "-i" if interactive else "", filename)
    if "DISPLAY" in os.environ:
        return run_config(os.environ.get("XTERM"), cmd)
        return os.system(cmd)

And map it to a keyboard shortcut:

nmap ;ru :update<CR>:python pyterm(vim.current.buffer.name, 0)<CR>
nmap ;ri :update<CR>:python pyterm(vim.current.buffer.name, 1)<CR>

I had previous set some environment variables to determine the exact terminal to run in if using gvim, or in the same terminal if not in a X.

Then I usually just type ';ri' in a Python buffer to run it (usually to test it).


Vim can be used to compile using gnu make on the current file - even if there's no Makefile for the file (for more details see here):

:make %:r

This way vim provides you with access to the quickfix error feedback from the compiler (:help quickfix) list - :cn Next error, :cp Previous error, :cw New window listing errors.

If you've not got gnu make then you can set the makeprg variable (change to g++ if you're compile C++):

:se makeprg=gcc\ -o\ %<\ %

and then use the vim make command to compile the current file and get a quickfix list of errors in vim:


EDIT: If you also want to run the compiled executable from within vim you can do ('!' executes, '%:r' is the filename without its suffix):


In case of gcc, python etc. you can call it with a colon-bang (:!)

Eg. :!python something.py

!gcc file.c && ./a.out

This will compile your C file as well as run the executable asking for the input all within you vim window.

  • 1
    A more complete answer would include the ':', and to make it more versatile you could change 'file.c' for the current file operator '%' : :!gcc % && ./a.out – Pierz Nov 22 '14 at 8:56
  • I'm doing something similar in my .vimrc, I map Command + Shift + R to: map <D-R> :exe '!gcc %:p' <bar> exe '!'.getcwd().'/a.out'<cr> – f1lt3r Aug 1 '16 at 23:37

Regarding the particular case of compiling source code.

And if you have something that can compile python, set makeprg accordingly.

protected by Kusalananda Sep 18 '17 at 18:03

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?