1

Is there any way for me to define a command (i.e :compile) to compile C/C++ code inside vim? I know I can use :!g++ %:p for that but is really tricky to type all these special characters.

Thanks in advance.

1

2 Answers 2

3

You can use the :make command in Vim. By default it runs make but you can set the makeprg variable to be whatever program you want.

So for your example.

:set makeprg=g++\ %

and then you can

:make
Sign up to request clarification or add additional context in comments.

7 Comments

Non need to change makeprg to g++ on machines where gnu make is correctly configured (i.e. all *nixes but mingw)
The reason you change makeprg is because you don't want to use make, i.e. you don't have a Makefile. Changing makeprg to g++\ % calls g++ directly on your source file, so it's good for quick experimenting.
We don't need to write a makefile to compile mono-file programs. The thing is that mono-file projects shall not be in directories where a Makefile already exists. Try, mkdir test && cd test && gvim hello.cpp , write an hello world program, and then, type :make.
That does not work for me. make: *** No targets specified and no makefile found. Stop.
Being able to skip make lets you use it for other kinds of projects as well, and that is useful. You can set makeprg to python or runhaskell or whatever you like. It doesn't have to be tied to make.
|
2

You can use :make (or abbreviation :mak) to invoke your Makefile. You can even specify a target (e.g. :mak build). And you can always make a mapping to make it (hehe) easier to invoke.

:help :make

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.