24

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

1
  • Check out github.com/tpope/vim-dispatch it allows you to set different compiler commands per file type and invoke them easily
    – elig
    Commented Feb 17, 2021 at 1:06

11 Answers 11

35

Write a Makefile, then you can simply:

:make

from inside vim. And if compilation fails:

:copen

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

15

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

:!python %

See :he cmdline-special (or here)

6

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.

4

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

4

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:

:make

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

:!./%:r
3

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.

1

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

Eg. :!python something.py

1

Assuming the editor is running in a terminal, you could just background the editor temporarily using Ctrl+Z, and then build the project from the terminal in whatever way is required. Then you would use fg from the shell to get back to the editor.

The benefit of this is that it works with vim, traditional vi, the ed line editor, the nano editor (if started with -z to allow it to be suspended), and even emacs (assuming you start with the editor running in the terminal, for example with -nw).

Switching back and forth between an editor and the terminal in this manner is not slower than using personal shortcuts, and has the benefit of being editor agnostic as well as not requiring any special configuration.

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

2
  • 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
    Commented Nov 22, 2014 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
    Commented Aug 1, 2016 at 23:37
0

Regarding the particular case of compiling source code.

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

0

I have created command to Run my c & cpp codes

:command  Runcpp !clear;g++ % -o %:r && ./%:r<CR> 
:command  Runc   !clear;gcc % -o %:r && ./%:r<CR>

Put above code in .vimrc file and run you code using command Runcpp & Runc

You must log in to answer this question.