我最近发现Vim中有一个称为“compiler”的命令,你可以使用任何常见的编译器(例如::compiler gcc, :compiler php等),但它似乎没有任何直接的效果。
我在man页面上进行了搜索,但没有找到关于它实际做什么的有用信息,Vim Wiki也没有。是否有人知道该命令实际上是做什么的?
我最近发现Vim中有一个称为“compiler”的命令,你可以使用任何常见的编译器(例如::compiler gcc, :compiler php等),但它似乎没有任何直接的效果。
我在man页面上进行了搜索,但没有找到关于它实际做什么的有用信息,Vim Wiki也没有。是否有人知道该命令实际上是做什么的?
:make
的程序和错误消息的格式,以便vim可以跳转到错误位置。在$VIMRUNTIME/compiler/
中查找不同的.vim文件,可以被加载。:help write-compiler-plugin
编译器插件为特定编译器设置选项。用户可以使用
:compiler
命令加载它。主要用途是设置“errorformat”和“makeprg”选项。
还可以参考:help errorformat
和:help makeprg
。例如,这是我机器上的GCC编译器文件:
" 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
Vim通常安装了一些常见的编译器配置,因此它会自动选择适当的配置。要实际使用编译器配置,您需要使用Vim的 :make 命令,正如上面提到的一样,但默认情况下操作模式需要存在 Makefile
文件。
如果您只想对当前文件 / 缓冲区进行快速编译(而没有现有的Makefile),则如果您已安装GNU make,则可以像这样编译它(如此处所述:这里):
:make %:r
它将编译文件并提供vim的错误/警告,以便您可以使用快速修复(:help quickfix
)列表导航每一个错误 - :cn
下一个错误,:cp
上一个错误,:cw
新窗口列出错误。
如果您没有安装GNU make,您可以设置这个变量 - 像这样立即在当前会话中:
:se makeprg=gcc\ -o\ %<\ %
或者将其放入您的~/.vimrc文件中:
set makeprg=gcc\ -o\ %<\ %
然后您可以输入:
:make
它将编译文件并在Vim中为您提供错误/警告的快速修复列表。
编辑:如果您还想在vim中运行已编译的可执行文件,可以执行以下操作('!'表示执行,'%:r'是没有后缀名的文件名):
:!./%:r
:help compiler
它为当前缓冲区选择的编译器设置选项。
:compiler gcc
和:make %
,结果显示make: Nothing to be done for \
dummy.c'.`。 - balkimakeprg
。我不明白这个文件的用途是什么? - balki:make
操作模式需要一个现有的 Makefile 文件,但是你可以设置 vim 在没有 Makefile 的情况下工作。请参见下面的答案。 - Pierz