Reference: http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html
and VIM Help documentation
This code can be used to achieve F5 compilation
"C,c++ F5 compile run
map <F5>: Call COMPILERUNGCC () <CR>
func! COMPILERUNGCC ()
exec "W"
if &filetype = = ' C '
exec '!g++%-o%< '
exec '! ./%< '
ElseIf & filetype = = ' cpp '
exec '!g++%-o%< "
exec"! ./%< "ElseIf &filetype
= ' java '
exec '!javac%" C11/>exec "!java%<"
elseif &filetype = ' sh '
:!. /%
endif
endfunc
But the actual use of a problem, that is, when compiling Java, will tell you can not find the class.
Use commands in vim: PWD View the current working directory and find that the current directory is in the user directory rather than the working directory. In other words, you must first enter the current working directory to use this script.
Vim can use "%" to represent the current file path and use some parameters to extract what is needed
The following content comes from the Vim help document
Examples, when the ' file name is ' src/version.c ', current dir "/home/mool/vim": >:p/home/mool/vim/src/ver SION.C:p:.
SRC/VERSION.C:p: ~ ~/vim/src/version.c:h src:p: H/home/mool/vim/src :p: H:h/home/mool/vim:t version.c:p: t version.c:r Src/version:p: R/home/mool/vim/src/version:t:r version:e C:s?version?main? Src/main.c:s?version?main:p/home/mool/vim/src/main.c:p: gs?/?\\? \HOME\MOOL\VIM\SRC\VERSION.C examples, when the file name is "src/version.c.gz": >:p/home/mool/vim/src/
Version.c.gz:e gz:e:e c.gz:e:e:e c.gz : E:e:r c:r src/version.c:r:e c:r:r Src/version:r:r:r src/version
So, you can use
: cd%:p: H
Enter the current working directory. Note that VIM uses the ":!" You can use command-line commands, but if the CD is in front of you, you will find that the path is reset when you compile it. Don't add! You can change the working directory.
Add the following code to the. VIMRC can be implemented F5 compiled
Map <F5>: Call COMPILERUNGCC () <CR>
IMAP <F5> <esc>:call COMPILERUNGCC () <CR>
func! COMPILERUNGCC ()
exec "W"
exec "CD%:p: H"
if &filetype = = ' C '
exec '!g++%-o%< '
exec '!/%< ;"
ElseIf &filetype = = ' cpp '
exec '!g++%-o%< "
exec"! ./%< "ElseIf &filetype
= = ' java '
E Xec "!javac%"
exec "!java%<"
elseif &filetype = ' sh '
:!. /%
endif
endfunc
The map is simply a mapping, and if you press F5 in insert mode, it becomes the insertion of F5 in the document without executing the function. So use the following line of IMAP to represent the mappings in insert mode