vim写c代码,有什么插件可以自动编译

2025-05-19 21:14:14
推荐回答(1个)
回答1:

很沙马特的想法!下面是网友wenin819提供在win32下vim的自定义函数实现:

" 编译源文件
func! CompileCode()
        exec "w"
        if &filetype == "c"
            exec "!gcc -finput-charset=UTF-8 -fexec-charset=GBK -Wall -std=c99 %<.c -o E:/coder/tmp_app/%<"
        elseif &filetype == "cpp"
            exec "!g++ -finput-charset=UTF-8 -fexec-charset=GBK -Wall -std=c++98 %<.cpp -o E:/coder/tmp_app/%<"
        elseif &filetype == "java"
            exec "!javac -encoding utf-8 -d E:/coder/classes/ %"
        endif
endfunc

" 运行可执行文件
func! RunCode()
        exec "w"
        if &filetype == "c" || &filetype == "cpp"
            exec "! E:/coder/tmp_app/%<.exe"
        elseif &filetype == "java"
            exec "!java -cp E:/coder/classes/ %<"
        endif
endfunc

" 编译运行可执行文件
func! CompileAndRunCode()
exec CompileCode()
exec RunCode()
endfunc

" F5 一键保存、编译
map  :call CompileCode()
map!  :call CompileCode()

" F6 一键保存、运行
map  :call RunCode()
map!  :call RunCode()

" F9 一键保存、编译和运行
map  :call CompileAndRunCode()
map!  :call CompileAndRunCode()

不会用?把上面这一坨丢到.vimrc里去。