Mar*_*man 24 vim command compilation
我最近发现Vim中有一个叫做编译器的命令.您可以使用任何常见的编译器(例如,:编译器gcc,:编译器php等)调用它,但它似乎没有立即生效.
我搜索了联机帮助页,但没有发现它实际上有什么用处,Vim Wiki也没有.有谁知道那个命令实际上做了什么?
Joh*_*ica 17
它为该编译器设置选项,例如要使用的程序: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
Run Code Online (Sandbox Code Playgroud)
Pie*_*erz 10
Vim通常安装了许多常见的编译器配置,因此会自动选择合适的编译器配置.要实际使用编译器配置,您还需要使用Vim:make函数,但默认的操作模式需要Makefile存在.
如果你只是想这样做对当前文件/缓冲快速编译(不含现有的Makefile),那么如果你有GNU使安装你可以编译它像这样(如解释在这里):
:make %:r
Run Code Online (Sandbox Code Playgroud)
它将编译文件并向vim提供错误/警告,以便您可以使用quickfix(Makefile
)列表导航每个文件- :help quickfix
下一个错误,:cn
上一个错误,:cp
新窗口列出错误.
如果你没有安装GNU make,你可以设置这个变量 - 在当前会话中立即设置如下:
:se makeprg=gcc\ -o\ %<\ %
Run Code Online (Sandbox Code Playgroud)
或者把它放在〜/ .vimrc文件中:
set makeprg=gcc\ -o\ %<\ %
Run Code Online (Sandbox Code Playgroud)
然后你可以输入:
:make
Run Code Online (Sandbox Code Playgroud)
它将编译文件并为您提供Vim中错误/警告的quickfix列表.
编辑:如果您还想从vim中运行已编译的可执行文件,您可以执行('!'执行,'%:r'是没有后缀的文件名):
:!./%:r
Run Code Online (Sandbox Code Playgroud)