Gvim/vim One-click Compile, connect, run A/C + + Single file

Source: Internet
Author: User
Tags imap

One-click Compilation and run function for Gvim or Vim configuration files (Note: GCC is required on the machine)
This code is only added to C + + compile and run, if you want to add its language can refer to this code to join
At the same time, this code added to the Windows under the Gvim compiled UTF-8 encoded format source files, the solution of Chinese garbled
(that is, adding "-FEXEC-CHARSET=GBK" to the compilation option).
Add the above code to the _VIMRC file.
? 1. [Code]gvim configuration file
"------------------------------------------------------------------------------
"< determine if the operating system is Windows or Linux >
"------------------------------------------------------------------------------
if (Has ("Win32") | | have ("Win64") | | have ("Win95") | | has ("Win16"))
Let G:iswindows = 1
Else
Let G:iswindows = 0
endif

"------------------------------------------------------------------------------
"< judgment is terminal or Gvim >
"------------------------------------------------------------------------------
If has ("gui_running")
Let G:isgui = 1
Else
Let G:isgui = 0
endif

"------------------------------------------------------------------------------
"< compile, connect, run configuration >
"------------------------------------------------------------------------------
"F9 save, compile, connect and run with one click
Map <F9>: Call Run () <CR>
IMAP <F9> <esc>:call Run () <CR>

"Ctrl + F9 Save and compile with one click"
Map <c-F9>: Call Compile () <CR>
IMAP <c-F9> <esc>:call Compile () <CR>

"Ctrl + F10 One click to save and connect
Map <c-F10>: Call Link () <CR>
IMAP <c-F10> <esc>:call Link () <CR>

Let S:lastshellreturn_c = 0
Let s:lastshellreturn_l = 0
Let s:showwarning = 1
Let S:obj_extension = '. O '
Let s:exe_extension = '. exe '
Let S:sou_error = 0

Let s:windows_cflags = ' gcc\-fexec-charset=gbk\-wall\-g\-o0\-c\%\-o\%<.o '
Let s:linux_cflags = ' gcc\-wall\-g\-o0\-c\%\-o\%<.o '

Let s:windows_cppflags = ' g++\-fexec-charset=gbk\-wall\-g\-o0\-c\%\-o\%<.o '
Let s:linux_cppflags = ' g++\-wall\-g\-o0\-c\%\-o\%<.o '

func! Compile ()
EXE ": CCL"
EXE ": Update"
If expand ("%:e") = = "C" | | Expand ("%:e") = = "CPP" | | Expand ("%:e") = = "Cxx"
Let S:sou_error = 0
Let S:lastshellreturn_c = 0
Let Sou = expand ("%:p")
Let OBJ = expand ("%:p: R"). S:obj_extension
Let Obj_name = expand ("%:p: T:r"). S:obj_extension
Let v:statusmsg = "
If!filereadable (OBJ) | | (filereadable (obj) && (getftime (obj) < Getftime (Sou)))
redraw!
If expand ("%:e") = = = "C"
If G:iswindows
EXE ": setlocal makeprg=". S:windows_cflags
Else
EXE ": setlocal makeprg=". S:linux_cflags
endif
Echohl warningmsg | echo "Compiling ..."
Silent Make
ElseIf expand ("%:e") = = "CPP" | | Expand ("%:e") = = "Cxx"
If G:iswindows
EXE ": setlocal makeprg=". S:windows_cppflags
Else
EXE ": setlocal makeprg=". S:linux_cppflags
endif
Echohl warningmsg | echo "Compiling ..."
Silent Make
endif
redraw!
If V:shell_error! = 0
Let S:lastshellreturn_c = V:shell_error
endif
If G:iswindows
If S:lastshellreturn_c! = 0
EXE ": Bo Cope"
Echohl warningmsg | echo "Compilation Failed"
Else
If s:showwarning
EXE ": Bo CW"
endif
Echohl warningmsg | echo "Compilation Successful"
endif
Else
If Empty (v:statusmsg)
Echohl warningmsg | echo "Compilation Successful"
Else
EXE ": Bo Cope"
endif
endif
Else
Echohl warningmsg | echo "Obj_name" is up to date "
endif
Else
Let S:sou_error = 1
Echohl warningmsg | echo "Please choose the correct source file"
endif
EXE ": setlocal makeprg=make"
Endfunc

func! Link ()
Call Compile ()
If S:sou_error | | S:lastshellreturn_c! = 0
Return
endif
Let s:lastshellreturn_l = 0
Let Sou = expand ("%:p")
Let OBJ = expand ("%:p: R"). S:obj_extension
If G:iswindows
Let Exe = expand ("%:p: R"). S:exe_extension
Let Exe_name = expand ("%:p: T:r"). S:exe_extension
Else
Let Exe = expand ("%:p: R")
Let Exe_name = expand ("%:p: T:r")
endif
Let v:statusmsg = "
If filereadable (obj) && (getftime (obj) >= getftime (Sou))
redraw!
If!executable (Exe) | | (Executable (EXE) && getftime (EXE) < Getftime (OBJ))
If expand ("%:e") = = = "C"
setlocal makeprg=gcc\-o\%<\%<.o
Echohl warningmsg | echo "Linking ..."
Silent Make
ElseIf expand ("%:e") = = "CPP" | | Expand ("%:e") = = "Cxx"
setlocal makeprg=g++\-o\%<\%<.o
Echohl warningmsg | echo "Linking ..."
Silent Make
endif
redraw!
If V:shell_error! = 0
Let s:lastshellreturn_l = V:shell_error
Endifhttp://www.huiyi8.com/hunsha/hanshi/?
If g:iswindows Korean wedding photo
If s:lastshellreturn_l! = 0
EXE ": Bo Cope"
Echohl warningmsg | echo "Linking failed"
Else
If s:showwarning
EXE ": Bo CW"
endif
Echohl warningmsg | echo "Linking successful"
endif
Else
If Empty (v:statusmsg)
Echohl warningmsg | echo "Linking successful"
Else
EXE ": Bo Cope"
endif
endif
Else
Echohl warningmsg | echo "Exe_name" is up to date "
endif
endif
Setlocal Makeprg=make
Endfunc

func! Run ()
Let s:showwarning = 0
Call Link ()
Let s:showwarning = 1
If S:sou_error | | S:lastshellreturn_c! = 0 | | S:lastshellreturn_l! = 0
Return
endif
Let Sou = expand ("%:p")
Let OBJ = expand ("%:p: R"). S:obj_extension
If G:iswindows
Let Exe = expand ("%:p: R"). S:exe_extension
Else
Let Exe = expand ("%:p: R")
endif
If executable (EXE) && getftime (EXE) >= getftime (obj) && getftime (obj) >= getftime (Sou)
redraw!
Echohl warningmsg | echo "Running ..."
If G:iswindows
EXE ":!%<.exe"
Else
If G:isgui
EXE ":!gnome-terminal-e ./%<"
Else
EXE ":!. /%< "
endif
endif
redraw!
Echohl warningmsg | echo "Running Finish"
endif
Endfunc
?

2. [Image] Untitled. png

Gvim/vim One-click Compile, connect, run A/C + + Single file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.