Vim - learnclang/1-helloworld GitHub Wiki

There are many fancy plugins to convert Vim into a complete C/C++ IDE, this guide aims to keep things simple and configure the minimums necessary to compile and execute the helloWorld project.

Vim's workflow

To compile and run a C program within Vim, we are going to create a Makefile to setup the project build script and rely on make to do the heavy lifting for us.

For more information on make, see here.

Makefile

Create a new file named Makefile and copy the following text into it, replace gcc by the compiler of your choice.

all: run
run: hello.c
    ./hello
hello.c: clean
    gcc ./hello.c -Wall -o ./hello
clean:
    rm -f ./hello

Now you can compile and execute the program by calling make from the terminal or using the :make command within Vim.

  • make or make all: cleans, compiles and runs the program
  • make run: execute the compiled program
  • make hello.c: cleans and compiles the hello.c source file
  • make clean: cleans previous executables

Automate all the things

In order to facilitate things, you can map the make call + open a quickfix window with the compiler output to a key (e.g. <Leader>r) adding this to your .vimrc file.

map <Leader>r :make<CR>:copen<CR><CR>
⚠️ **GitHub.com Fallback** ⚠️