Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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.

share|improve this answer

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

:!python %

See :he cmdline-special (or here)

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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

Eg. :!python something.py

share|improve this answer

Regarding the particular case of compiling source code.

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

share|improve this answer
!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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.