VIM实现F5运行php/perl/python/c/java/erlang

function RunThisScript() 
    let file_name = expand("%:p")
    let file_ext = expand("%:e")
    let file_cmd = ""

    "python 直接调用
    if file_ext == "py"
        let file_cmd = '/usr/bin/python' 
        let file_args = ' ' . file_name
    "c 需要提取第一行的编译参数
    "如,当需要引入第三方库(以mysql为例)时,则在第一行添加: //-lmysqlclient -L/usr/local/mysql/include
    "文件中则可直接 #include <mysql/mysql.h>
    elseif file_ext == "c" 
        let file_first_line = getline(1)
        let file_arg = ""
        if strpart(file_first_line, 0, 2) == '//'
            let file_arg = strpart(file_first_line, 2) "提取参数
        endif
        let file_output_file = strpart(file_name, 0, strridx(file_name, '.c')) 
        let file_args = ' -o '. file_output_file .' '.  file_name . ' '. file_arg .' && '. file_output_file "将参数附加到编译命令之后
        let file_cmd = '/usr/bin/cc' 
    "php 直接调用
    elseif file_ext == "php"
        let file_cmd = "/usr/local/php/bin/php" "php执行路径
        let file_args = ' -f '. file_name
    "perl 直接调用
    elseif file_ext == "perl" || file_ext == "pl"
        let file_cmd = "/usr/bin/perl"
        let file_args = " ". file_name
    "erlang 默认调用 main 函数, 可以确保 escript 和 noshell/shell 执行时一致
    elseif file_ext == "erl"
        let file_output_file = strpart(expand("%"), 0, stridx(expand("%"), ".erl"))
        let file_cmd = "/usr/bin/erlc" 
        let file_args = file_output_file .".". file_ext ." ; /usr/bin/erl -noshell -s ". file_output_file . " main  -s init stop"
    "java 先调用 javac,再调用java
    elseif file_ext == "java"
        let file_output_file = strpart(expand("%"), 0, stridx(expand("%"), ".java"))
        let file_cmd = 'javac'
        let file_args = file_name ." && java ". file_output_file
    else
        echo "错误: 没有任何编译器匹配此文件类型, 请确认您的文件扩展名!"
    endif

    if file_cmd != ""
        if ! executable(file_cmd)
            echo file_cmd
            echo "The executable file to compile ". file_ext . " type files."
        else
            let cmd = "! ". file_cmd . ' ' . file_args
            "echo "执行命令: ". cmd
            exec cmd 
        endif
    endif
endfunction

编程技巧