【问题标题】:What does the :compiler command do in Vim?Vim 中的 :compiler 命令有什么作用?
【发布时间】:2011-03-03 03:22:34
【问题描述】:

我最近发现 Vim 中有一个命令叫 compiler。你可以用任何常用的编译器调用它(例如:compiler gcc、:compiler php等),但它似乎没有任何立竿见影的效果。

我在手册页上进行了搜索,但没有发现任何有用的信息,关于它的实际作用,Vim Wiki 也没有。有谁知道该命令实际上是做什么的?

【问题讨论】:

    标签: compilation command vim


    【解决方案1】:

    它为该编译器设置选项,例如用于:make 的程序和错误消息的格式,以便 vim 可以将您跳转到错误位置。在 $VIMRUNTIME/compiler/ 中查找可以获取的不同 .vim 文件。

    :help write-compiler-plugin

    编译器插件设置用于特定编译器的选项。用户可以 使用:compiler 命令加载它。主要用途是设置 'errorformat' 和 'makeprg' 选项。

    另请参阅 :help errorformat:help makeprg

    这是我机器上的 GCC 编译器文件,例如:

    /usr/share/vim/vim72/compiler/gcc.vim

    " Vim compiler file
    " Compiler:         GNU C Compiler
    " Maintainer:       Nikolai Weibull <now@bitwi.se>
    " Latest Revision:  2006-12-20
    
    if exists("current_compiler")
      finish
    endif
    let current_compiler = "gcc"
    
    let s:cpo_save = &cpo
    set cpo-=C
    
    CompilerSet errorformat=
          \%*[^\"]\"%f\"%*\\D%l:\ %m,
          \\"%f\"%*\\D%l:\ %m,
          \%-G%f:%l:\ %trror:\ (Each\ undeclared\ identifier\ is\ reported\ only\ once,
          \%-G%f:%l:\ %trror:\ for\ each\ function\ it\ appears\ in.),
          \%f:%l:\ %m,
          \\"%f\"\\,\ line\ %l%*\\D%c%*[^\ ]\ %m,
          \%D%*\\a[%*\\d]:\ Entering\ directory\ `%f',
          \%X%*\\a[%*\\d]:\ Leaving\ directory\ `%f',
          \%D%*\\a:\ Entering\ directory\ `%f',
          \%X%*\\a:\ Leaving\ directory\ `%f',
          \%DMaking\ %*\\a\ in\ %f
    
    if exists('g:compiler_gcc_ignore_unmatched_lines')
      CompilerSet errorformat+=%-G%.%#
    endif
    
    let &cpo = s:cpo_save
    unlet s:cpo_save
    

    【讨论】:

    • 我写了一个简单的c程序运行:compiler gcc:make %,我得到make: Nothing to be done for `dummy.c'.
    • 这似乎没有设置makeprg。不明白这个文件有什么用?
    • 对于:make 的默认操作模式,您需要一个现有的 Makefile - 但您可以设置 vim 以在没有 Makefile 的情况下使用 - 请参阅下面的答案。
    【解决方案2】:

    如前所述,Vim 通常安装了许多常见的编译器配置,因此它会自动选择合适的编译器配置。要实际使用编译器配置,您需要使用 Vim :make 函数,如前所述,但默认操作模式需要存在 Makefile

    如果您只是想对当前文件/缓冲区进行快速编译(没有现有的 Makefile),那么如果您安装了 GNU make,您可以像这样编译它(如 here 解释的那样):

    :make %:r
    

    它会编译文件并向 vim 提供错误/警告,以便您可以使用 quickfix (:help quickfix) 列表导航每个错误 - :cn 下一个错误,:cp 上一个错误,:cw 新窗口列表错误。

    如果你没有安装 GNU make,你可以设置这个变量 - 或者像这样在当前会话中立即设置:

    :se makeprg=gcc\ -o\ %<\ %
    

    或者把它放在你的 ~/.vimrc 文件中:

    set makeprg=gcc\ -o\ %<\ %
    

    然后你可以输入:

    :make
    

    它会编译文件并为你提供quickfix Vim 中的错误/警告列表。

    编辑:如果你还想在 vim 中运行编译后的可执行文件,你可以这样做('!' 执行,'%:r' 是没有后缀的文件名):

     :!./%:r
    

    【讨论】:

      【解决方案3】:
      :help compiler
      

      它为当前缓冲区设置所选编译器的选项。

      【讨论】:

        【解决方案4】:

        如果你编辑一个 java 文件,你可以执行 :compiler javac 然后 :make % 来编译当前文件...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-11
          • 2015-12-10
          • 2017-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多