created June 22, 2002 · complexity basic · author Tadghe Danu · version 6.0
I love vim, it's my default editor on my Sun, Windows, Linux and *BSD boxen. That said, I hate having to flip windows to compile while doing the write->compile->debug loop.
If you're used to Visual Studio and the ability it has to just hit F5 to compile and run the current file or F8 to compile or step through the code you'll appreciate this...
This is my Windows version of this scriplet/tiplet. For other platforms, you'll want to change the IF ELSE loops. You should actually never see the "Unsuccessful" message from the compile/run loop unless the compiler completely bombs out. This is from my _vimrc...
map <F5> :call CompileRunGcc()<CR> map <F8> : call CompileGcc()<CR> func! CompileRunGcc() exec "w" "Save the file exec "!gcc % -o %< && cr 10 && IF EXIST %<.exe (%<) ELSE banner -c = Compile Unsuccessful " exec "i" "jump back where we were endfunc func! CompileGcc() exec "w" exec "!gcc % -o %< && IF EXIST %<.exe (cr 5 && banner -c # Success) ELSE banner -c # Compile Unsuccessful " exec "i" endfunc
Comments
You might like to take a look at Vim's quickfix mode. See :help quickfix.
Also, on unix systems one setup I used for a while (and still have, though I'm more often than not using gvim now) involves running vim from within screen, then you simply have to double-tap ctrl-a to get a shell window.
$ xterm -name VIM -geometry 132x60 -e screen vim
I especially like this method for editing HTML/XML/TeX etc where the post-edit operation isn't a compile, but something like moving files around, rsync, or whatever.
You can also use the :make command. You can set the variable makeprg variable if required, eg I use
set makeprg=gmake\ -j
Parallel build with gnu make.
:make will accept the target name as an option.
The limits are that it does not do a save. I would MUCH prefer is ran make in the background somehow too.
Under Unix this is what i got to compile and run my program. i would imagine make would be a much better solution for big projects but for small single file projects this works great
map <F5> :call CompileRunGcc()<CR> func! CompileRunGcc() exec "w" exec "!gcc % -o %<" exec "! ./%<" endfunc
You might want to compile your file when you're in insert mode. lmap makes your mapping available in every situation in Vim.
See :help :lmap.