如何高效地“制作” 与 Vim

发布于 2024-07-16 08:58:53 字数 396 浏览 9 评论 0原文

我想做的似乎是一个非常基本的事情,但我找不到任何相关内容。 我正在像往常一样构建一个项目:

project
|-- bin
|-- inc
`-- src

我想使用 Vim 中包含的 make 命令来制作我的项目。 但每次我都必须指定 :make -C ../。 我更喜欢,如果当前目录中没有 Makefile 文件,则进入父目录。 我已经在

set tags+=./tags;/

我的 .vimrc 中做到了这一点。

此外,默认情况下,该品牌是丑陋的。 是否有选项可以添加颜色,并允许直接访问错误(如在 Emacs 中)。

谢谢

What I am trying to do seems a very basic stuff, but I can't find anything about it. I am working on a project built as usual:

project
|-- bin
|-- inc
`-- src

I would like to make my project using the make command included in Vim. But each time I have to specify :make -C ../. I would prefer, if there is not Makefile file in the current directory, go in the parent directory. I already do that with

set tags+=./tags;/

in my .vimrc.

Furthermore, the make is by default ugly. Are there options to add color to make, and allow a direct access to the errors (as in Emacs).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感性不性感 2024-07-23 08:58:53

稍微修改Adam所说的

 :set makeprg=[[\ -f\ Makefile\ ]]\ &&\ make\ \\\|\\\|\ make\ -C\ ..  

未转义,这

 [[ -f Makefile ]] && make || make -C ..

就是意思是,伪代码风格

 if file-exists(Makefile) 
 then make
 else make -C ..

这只向上一个目录。 如果您想要一个更通用的解决方案
根据需要添加尽可能多的目录,您需要能够搜索祖先目录,直到找到 Makefile,并且我不确定如何简单地从命令行执行此操作。 但是编写脚本(用您喜欢的任何语言)然后从 makeprg 调用它应该不难。

Slight modification of what Adam said:

 :set makeprg=[[\ -f\ Makefile\ ]]\ &&\ make\ \\\|\\\|\ make\ -C\ ..  

Unescaped, this is

 [[ -f Makefile ]] && make || make -C ..

which means, pseudo code style

 if file-exists(Makefile) 
 then make
 else make -C ..

This only goes one directory up. If you'd like a more general solution that will go
as many directories up as necessary, you'll need to be able to search ancestor directories until a Makefile is found, and I'm not sure how to do that simply from the command line. But writing a script (in whatever language you prefer) and then calling it from your makeprg shouldn't be hard.

骄傲 2024-07-23 08:58:53

Rampion 的解是第一步,但根据 vim 负载计算。 当我加载多选项卡会话时,一个选项卡到另一个选项卡的路径可能不一致。

这是我的解决方案(+ tabnew 的额外内容)。

fun! SetMkfile()
  let filemk = "Makefile"
  let pathmk = "./"
  let depth = 1
  while depth < 4
    if filereadable(pathmk . filemk)
      return pathmk
    endif
    let depth += 1
    let pathmk = "../" . pathmk
  endwhile
  return "."
endf

command! -nargs=* Make tabnew | let $mkpath = SetMkfile() | make <args> -C $mkpath | cwindow 10

The solution of rampion is a first step, but computed on vim load. When I load a multi tab session, the path can be inconsistent from one tab to another.

Here my solution (+ extra with tabnew).

fun! SetMkfile()
  let filemk = "Makefile"
  let pathmk = "./"
  let depth = 1
  while depth < 4
    if filereadable(pathmk . filemk)
      return pathmk
    endif
    let depth += 1
    let pathmk = "../" . pathmk
  endwhile
  return "."
endf

command! -nargs=* Make tabnew | let $mkpath = SetMkfile() | make <args> -C $mkpath | cwindow 10
习惯成性 2024-07-23 08:58:53

要回答第二个问题,您可以使用 VIM 中的 quickfix 功能来浏览错误。 Quickfix 将错误存储在缓冲区中,并允许您向前/向后导航它们。

您可能必须定义错误正则表达式以允许 VIM 从 Make 的输出中识别这些错误,但我似乎记得它开箱即用的效果相当好(我必须修改它对于 Java Ant 构建的工作方式 -显然不适用于这里)

To answer your second question, you can navigate through the errors using the quickfix functionality in VIM. Quickfix stores the errors in a buffer and allows you to navigate forward/backwards through them.

You may have to define the error regexp to allow VIM to identify these from Make's output, but I seem to remember that it works out-of-the-box rather well (I've had to modify how it works for Java Ant builds - obviously doesn't apply here)

小镇女孩 2024-07-23 08:58:53

到目前为止,最简单的解决方案是在 src 目录中有一个 Makefile,这是许多很多项目的设置方式,无论编辑器/IDE 是什么。 您仍然可以拥有一个调用 make -C src 的顶级 Makefile,其中在 src 中构建的规则位于它们所属的 src 中。

By far the easiest solution is to have a Makefile in your src directory, which is the way that many, many projects are set up regardless of editor/IDE. You can still have a top-level Makefile that calls make -C src, with the rules for building in src located in src where they belong.

逆蝶 2024-07-23 08:58:53

如果项目根目录上只有一个 makefile,则可以使用另一种方法:

project
|-- bin
|-- inc
|-- src
| Makefile

将项目路径设置为 b:projectDir 等变量,可以在开始执行之前使用“自动命令”更改到该目录:make:lmake:

augroup changeMakeDir
    au!
    autocmd QuickfixCmdPre *make
                \ if exists("b:projectDir") &&
                                    \ b:projectDir != expand("%:p:h") |
                    \ exe 'cd ' . b:projectDir |
                \ endif
augroup END

Projectroot 插件 可用于设置b:projectDir; 它还提供了将当前目录更改为项目根目录的命令:

augroup changeMakeDir
    au!
    autocmd QuickfixCmdPre make ProjectRootCD
augroup END

Another approach can be used if you have a single makefile on project root directory:

project
|-- bin
|-- inc
|-- src
| Makefile

With your project path in variable like b:projectDir it is possible to use an "autocommand" to change to that directory before start executing :makeor :lmake:

augroup changeMakeDir
    au!
    autocmd QuickfixCmdPre *make
                \ if exists("b:projectDir") &&
                                    \ b:projectDir != expand("%:p:h") |
                    \ exe 'cd ' . b:projectDir |
                \ endif
augroup END

Projectroot plugin can be used to set b:projectDir; it also provides the commands to change the current directory to the project root directory:

augroup changeMakeDir
    au!
    autocmd QuickfixCmdPre make ProjectRootCD
augroup END
妳是的陽光 2024-07-23 08:58:53

在 .vimrc 中设置 makecmd="make -C .."

set makecmd="make -C .." in your .vimrc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文