定义一个用于构建和加载错误文件的命令。

huangapple go评论73阅读模式
英文:

Define a command for building and loading errors file

问题

我想定义一个命令,首先构建当前的Go源文件,并加载errors.err文件以便进一步使用:cnext:clist命令。

我已经在我的.vimrc中添加了以下行:

command Gobuild !go build %:t | grep -v "^\#" | tee errors.err

但是我必须在:Gobuild之后执行:cfile,因为它不会自动执行。如何自动加载errors.err文件?尝试将:cfile附加到命令中,但没有帮助。在加载后自动删除errors.err也会很有用。

我知道可以使用make来做类似的事情,但我不喜欢它。

**更新:**一个笨拙的临时解决方案(在我深入研究建议的解决方案之前):

function GoBuild()
    silent !echo -e "make:\n\t@go build ${src} | grep -v '^\#' | tee" > %:t"_makefile"
    make -f %:t"_makefile" src="%:t"
    silent !rm %:t"_makefile"
endfunction

command Gobuild call GoBuild()

这里的@防止make回显命令,grep过滤掉#command line arguments行,tee不会打扰makegrep的错误代码(如果没有要过滤的内容,grep会返回错误代码)-tee始终返回0,表示“OK”错误代码。

**更新:**一个更好的解决方案

autocmd FileType go set makeprg=go\ build\ %:t\ 2>&1\\\|grep\ -v\ '^\\#'\\\|tee
command Gorun !./%:r
英文:

I want to define a command that will first build the current Go source file and load errors.err file for further use of such commands as :cnext or :clist.

What I have:

I added the following line to my .vimrc:

command Gobuild !go build %:t | grep -v "^\#" | tee errors.err

But I have to do :cfile after :Gobuild, as it is not done automatically. How can I auto-load errors.err file? Tried to append :cfile to the command, it didn't help. Auto-removing of errors.err after it's loaded would be useful too.

I know there's a way to do something like this with make, but I don't like it.

UPD: a clumsy, termorary solution (before I dive into suggested solutions):

function GoBuild()
    silent !echo -e "make:\n\t@go build ${src} | grep -v '^\#' | tee" > %:t"_makefile"
    make -f %:t"_makefile" src="%:t"
    silent !rm %:t"_makefile"
endfunction

command Gobuild call GoBuild()

Here @ prevents make from echoing commands, grep filters out #command line arguments line, and tee is not to bother make with error code from grep (grep returns error code it there's noting to filter out) — tee always returns 0, OK error code.

UPD: a better solution

autocmd FileType go set makeprg=go\ build\ %:t\ 2>&1\\\|grep\ -v\ '^\\#'\\\|tee
command Gorun !./%:r

答案1

得分: 2

一般来说,整个构建和报告错误的业务围绕着两个选项:'makeprg''errorformat',以及:make和一堆与quickfix相关的命令,如:copen:cnext的组合。在你的~/.vimrc中添加几行代码应该就足够了。

这篇文章有一些示例代码,这里有一个完整的插件

英文:

Generally speaking, the whole build and report errors business revolves around two options: 'makeprg' and 'errorformat' and a combination of :make and a bunch of quickfix-related commands like :copen or :cnext. Adding a couple of lines in your ~/.vimrc should be all you need.

This article as some sample code and here is a full-fledged plugin.

huangapple
  • 本文由 发表于 2013年4月5日 03:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/15820008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定