1

I want to use Vim as a simple c++ editor in windows (I have no experience with Linux). Nothing fancy, just want to get things started.

To compile a .cpp file from CMD I have to run a batch file in the CMD and then I can compile and run the program, i.e g++ -o program program.cpp.

I want to do this directly from Vim, like a custom command that does both runs the specific batch file and the run command.

I am completely beginner in Vim and currently have no idea what to do next. Thanks for helping me.

Edit :

All i want is a way to open Windows CMD then Run my batch file and then run g++ -o program program.cpp. How can i do that with single command in Vim.

2 Answers 2

1

Why do you need to run that batch file? To set PATH to include g++ and all? If so, you should consider opening a console, running that script from the console and eventually launching Vim from that same console. Or you could make sure the "environment variables" set in that script are defined to be always set on your windows system.

Then, depending on what provides you gcc, it could be very simple to compile single file programs from vim. Typing :make %< is enough with a correctly configured programming environment. On Windows, this means installing g++ through cygwin distribution. Mingw is very badly configured. With this one, you'll have to set :set makeprg=g++\ -o\ $*\ $*.cpp (typed from memory -- I stay away from mingw and thus, I never need to do this) -- or if you really want to run vim from the window explorer: :let &makeprg = 'c:/path/to/that/script.bat && g++ -o $* $*.cpp'. Or you can define a Makefile as others have explained you.

More details about what is possible with a correctly configure programming environment on the big sister/brother site: https://stackoverflow.com/a/35702919/15934

0

:!cmd runs cmd as if you were running it from the terminal!

Since you want to build code, you can think of writing a Makefile and using that. As above, you can run :!make to call make. vim has a :make command that actually calls make for you, so you might find it easier to use (as a bonus, :mak does the same and is shorter).

10
  • I want a custom command to do both run that batch file and run the program. How can i achieve that ?
    – user9120
    Sep 4, 2016 at 21:07
  • I think you can use && in Windows @A---B ? i.e. :!program1 && program2? Or perhaps a ;? :!program1; program2? Sep 4, 2016 at 21:21
  • @Carpetsmoker so you mean :!cmd my batch file && g++ -o p p.cpp right ?
    – user9120
    Sep 4, 2016 at 21:22
  • @A---B I don't think you don't need the cmd; Vim adds that for you. Just :!file will do. Sep 4, 2016 at 21:23
  • 1
    Just create a new (e.g. run) target in your makefile, that depends on the build target, and then issue :make run from Vim afterwards.
    – VanLaser
    Sep 5, 2016 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.