linux下vim配置之一键编译运行

我们知道Linux下有很多文本编辑器,但毫无疑问,vim是所有Unix及Linux系统标准的编辑器,就如同windows系统中的记事本一样。它具有很多强大的配置比如自动换行,自动缩进等等。

通常使用linux的都是开发者,编程者。所以编译和运行是两个必不可少的工作,但是在linux下每次编辑完都必须退出vim,然后编译,最后运行。一开始没觉得有什么不好的,但是随着代码量的增多,频繁的修改,这些都是无意义的操作。像我现在学习ncurses库编程一样,每次都要-lncurses,然后gcc -o,很麻烦。幸好vim配置中支持一件编译这个功能。

例如下面我自己的配置文件中,第150行开始就一键编译与运行的代码。

linux下vim配置之一键编译运行_第1张图片


func! CompileGcc()
    exec "w"
    let compilecmd="!gcc "
    let compileflag="-o %< "
    if search("mpi\.h") != 0
        let compilecmd = "!mpicc "
    endif
    if search("glut\.h") != 0
        let compileflag .= " -lglut -lGLU -lGL "
    endif
    if search("cv\.h") != 0
        let compileflag .= " -lcv -lhighgui -lcvaux "
    endif
    if search("omp\.h") != 0
        let compileflag .= " -fopenmp "
    endif
    if search("math\.h") != 0
        let compileflag .= " -lm "
    endif
    exec compilecmd." % ".compileflag
endfunc
func! CompileGpp()
    exec "w"
    let compilecmd="!g++ "
    let compileflag="-o %< "
    if search("mpi\.h") != 0
        let compilecmd = "!mpic++ "
    endif
    if search("glut\.h") != 0
        let compileflag .= " -lglut -lGLU -lGL "
    endif
    if search("cv\.h") != 0
        let compileflag .= " -lcv -lhighgui -lcvaux "
    endif
    if search("omp\.h") != 0
        let compileflag .= " -fopenmp "
    endif
    if search("math\.h") != 0
        let compileflag .= " -lm "
    endif
    exec compilecmd." % ".compileflag
endfunc

func! RunPython()
        exec "!python %"
endfunc
func! CompileJava()
    exec "!javac %"
endfunc


func! CompileCode()
        exec "w"
        if &filetype == "cpp"
                exec "call CompileGpp()"
        elseif &filetype == "c"
                exec "call CompileGcc()"
        elseif &filetype == "python"
                exec "call RunPython()"
        elseif &filetype == "java"
                exec "call CompileJava()"
        endif
endfunc

func! RunResult()
        exec "w"
        if search("mpi\.h") != 0
            exec "!mpirun -np 4 ./%<"
        elseif &filetype == "cpp"
            exec "! ./%<"
        elseif &filetype == "c"
            exec "! ./%<"
        elseif &filetype == "python"
            exec "call RunPython"
        elseif &filetype == "java"
            exec "!java %<"
        endif
endfunc

map  :call CompileCode()
imap  :call CompileCode()
vmap  :call CompileCode()

map  :call RunResult()
支持一键编辑c/c++,java,python的代码。

第一部分是gcc编译器中的代码,可编译c语言。在代码段中我们可以根据自己的需要来连接各个库函数,例如我们要链接ncurses库,我们就可以添加

if search("ncurses\.h") != 0
        let compileflag .= " -lncurses "
endif
这样我们就可以编译带ncurses库的代码了。


这个代码段是来自:http://blog.chinaunix.net/uid-21202106-id-2406761.html 这个博客中的,具体详细的编写过程可以看他的博客。

我自己使用的vimrc配置:http://pan.baidu.com/s/1qYJdyqS。


你可能感兴趣的:(Liunx,c)