1. Overview

Vim is a popular text editor in Linux. People use Vim in Linux servers for editing configuration files or source code. When editing source code, sometimes we want to compile the code from the Vim session. Otherwise, we have to quit the Vim session and compile the source code in a terminal session. That’s cumbersome because we might have to edit the source code again with Vim.

Luckily, there’s a way to compile the code from Vim.

In this tutorial, we’ll look at how to compile the code from the Vim session by calling external commands in Vim and using make.

2. Compiling a File by Running an External Command

We can call external commands in the Vim session. First, create a file with some code and call it main.c:

#include <stdio.h>

int main()
{
    printf("Hello Baeldung");

    return 0;
}

In the terminal session, we can compile this code by running the gcc compiler:

$ gcc main.c
$ ./a.out
Hello Baeldung

The gcc command above is what we want to run from our Vim session.

2.1. How to Run an External Command in Vim

We can run an external command in Vim by typing the semicolon (:) and bang (!) followed by the external command. First, we edit the file with Vim:

$ vim main.c

Making sure we are in the normal mode or command mode, we type our command:

:!gcc main.c

The command above compiles the source code from within Vim.

Finally, we can execute the program in Vim by typing this command:

:./a.out
Hello Baeldung

We should note that when we edit a source file, we need to save it before running the external command from Vim.

2.2. How to Refer to an Edited File or Buffer in Vim

When we’re in the command mode, we don’t have to type the filename we’re editing. We can refer to the filename by using %. Let’s edit the file with Vim again:

$ vim main.c

We can avoid typing main.c by using %:

:!gcc %

We’ll get the same application.

3. Using the Make Building Tool

So far, we’ve compiled a single file. But when there are many files to compile, this technique isn’t suitable.

Vim has the command make for this purpose. The make command in Vim is related closely to the make tool in Linux. make knows which pieces of a large program need to be recompiled. So we don’t have to compile files one by one.

3.1. The Make Building Tool

To compile files with make, we need to write a Makefile. Let’s create a file called Makefile in the same directory as main.c and add the following content to it:

main:
    gcc main.c

Now, we can compile the file by calling the make command:

$ make

Behind the scenes, it doesn’t differ that much from the original command. However, when there are more files, then make becomes valuable.

Let’s edit main.c and replace the content with the following content:

#include "baeldung.h"

int main()
{
    print_hello_baeldung();

    return 0;
}

Then we should create baeldung.h as the header file and add the following content to it:

void print_hello_baeldung();

Finally, we create baeldung.c for implementing the function defined in the header file:

#include <stdio.h>

void print_hello_baeldung()
{
    printf("Hello Baeldung");
}

So main.c depends on two files, baeldung.c and baeldung.h. To compile the file, we can use the following commands:

$ gcc main.c -c baeldung.c
$ gcc main.o baeldung.o -o baeldung

The first command compiled the code to object files. The second command linked the object files to the binary program.

Then we can execute the program:

$ ./baeldung
Hello Baeldung

However, typing two lengthy commands is no fun. We can wrap these two commands in our Makefile. Let’s edit Makefile to reflect this:

baeldung: baeldung.o main.o
    gcc main.o baeldung.o -o baeldung

baeldung.o main.o: baeldung.c baeldung.h main.c
    gcc main.c -c baeldung.c

The first two lines indicate that the baeldung binary program is dependent on baeldung.o and main.o. If the binary program is deleted, the make tool will link the object files to the binary program. The other two lines indicate that the object files are dependent on the source code files. If the source code files are changed, it will compile the source code into the object files.

The hard work is done. To compile the files, we can run the make command:

$ rm baeldung.o main.o baeldung
$ make
$ ./baeldung
Hello Baeldung

But that’s not the only value we can get from the make command. When only one of the files is changed, the make command doesn’t run two steps anymore. Suppose the baeldung binary program is deleted, the make command will not compile the C source code again. It will only run the last step, linking the object files.

3.2. Running Make in Vim

Let’s remove the object files and the binary program before editing the file again:

$ rm main.o baeldung.o baeldung
$ vim baeldung.c

Then this time, we can type a simpler command in Vim to compile the files:

:make

Behind the scenes, the make command in Vim calls the make command in Linux. However, we can change it. Suppose we’re working on a Java project named BazelApp, and we use bazel to build the application. We can set the make command in Vim to use bazel:

:set makeprg=bazel\ build\ //bazelapp:BazelApp

In the Vim command mode, we need to escape the space with the backslash character.

4. Conclusion

In this article, we used external commands in Vim by using the bang (!) to compile the code. We learned that we could refer to the filename using the % character. We learned how to write Makefile for the make command. Then, we used the make command in Vim to compile the source code.

Finally, we learned to change the program in the make command in Vim to something else.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.