Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #264 - Map function keys to compile and run your code

Created: June 22, 2002 9:18 Complexity: basic Author: Tadghe Danu Version: 6.0 Karma: 16/16 Imported from: Tip#264

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.

: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.


matt--AT--mh.dropbear.id.au , June 23, 2002 23:33


You can also use the :make command. ou 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.

mathew_spamcan--AT--yahoo.com.au , January 28, 2003 1:48


hi, 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


libberspam--AT--atlaswebmail.com , February 8, 2003 11:12


You might want to compile your file when you're in insert mode. lmap makes your mapping available in every situation in vi.

For detailed information, please refer to :help map.txt

olbyun--AT--hotmail.com , March 20, 2003 3:27


Some reason lmap doesn't work as expected in my vim/xf86/cygwin/w2k map! should work.

olbyun--AT--hotmail.com , March 20, 2003 3:32


Advertisement