在vim中,位置列表和快速修复列表有什么区别?

107
以下内容摘自“快速修复列表”和“位置列表”的文档,但我不确定它们实际上有何不同。下面的图片展示了从位置列表和快速修复列表中显示相同的内容。当我在vimgrep和lvimgrep中使用时,我应该使用哪个?
In Vim the quickfix commands are used more generally to find a list of positions 
in files.For example, |:vimgrep| finds pattern matches.  You can use the positions 
in a script with the |getqflist()| function. Thus you can do a lot more than the
edit/compile/fix cycle!
...
...

                         *location-list* *E776* 
A location list is similar to a quickfix list and contains a list of positions 
in files.  A location list is associated with a window and each window can have 
a separate location list.  A location list can be associated with only one window.  
The location list is independent of the quickfix list.

...

输入图像描述

更新

我从这里找到了以下内容。

These commands all fill a list with the results of their search. "grep" and 
"vimgrep" fill the "quickfix list", which can be opened with :cw or :copen, 
and is a list shared between ALL windows. "lgrep" and "lvimgrep" fill the 
"location list," which is local to the current window, and can be opened 
with :lw or :lopen. Both of these lists can be used to instantly jump to 
the matching line in whatever file it occurs in.

quickfix list 适用于所有窗口,local window 只适用于当前窗口的 location list。但是,我可以从任何其他窗口打开 location list。那么,它们有什么区别呢?

1个回答

129

位置列表是当前窗口本地的,因此您可以拥有与窗口一样多的位置列表:30个窗口?没问题,这里是您的30个并发位置列表。

快速修复列表是全局的,因此一次只能有一个可用。有些命令允许您用先前的修复列表替换当前的修复列表,但您不能有两个并发的快速修复列表。

不要混淆位置/快速修复“列表”(数据结构)与位置/快速修复“窗口”(显示这些数据结构内容的窗口)。 “窗口”具有类似的行为,但“列表”没有。这种区别很重要,因为这些窗口不幸不是与这些列表交互的唯一方式:有许多命令允许我们在不打开关联窗口的情况下移动到这些列表,并且了解这些列表之间的区别是使用这些命令的关键。

实际演示示例:

$ vim -O foo.txt bar.txt

  1. foo.txt中执行:lvim foo %以为包含foo.txt的窗口创建一个位置列表。

  2. 执行:lne几次以跳转到foo.txt中的一些foo

  3. 将焦点集中在bar.txt上并执行:lne。会发生什么?

  4. 现在,在bar.txt中执行:lvim bar %以为包含bar.txt的窗口创建一个位置列表。

  5. 执行:lne几次。您跳转到哪些匹配项?在哪个缓冲区?在哪个窗口?

  6. 切换到另一个窗口并执行:lne几次。会发生什么?

  • 切换到 bar.txt:lne 命令是做什么用的?

  • 现在,在 bar.txt 中执行 :vim bar % 命令,创建一个快速修正列表。

  • 执行几次 :cn 命令,在 bar.txt 中跳转到几个 bar

  • 现在,聚焦于 foo.txt:cn 命令是做什么的?

  • 结论:使用 :lne 命令跳转到的位置取决于你所在的窗口,这使得位置列表成为一个局部于窗口的位置列表,但使用 :cn 命令跳转到的错误并不是,这使得快速修正列表变成了全局的

    在我看来,这两个列表都有相对清晰的角色:快速修正列表(因此快速修正窗口)通常而且很合理地专注于错误,而位置列表则似乎更适合搜索。


    16
    一般而言:当你的搜索或编译涉及多个文件时,最好使用 quickfix 列表;当只涉及单个文件时,最好使用 location 列表。 - Trebor Rude
    8
    特别是,如果你使用-q errors.txt参数运行vim,在将编译错误放入errors.txt后(例如:gcc -Wall *.c >errors.txt 2>&1),vim会从编译错误列表中填充快速修复列表,这非常方便。 - Kevin
    @TreborRude,我不这么认为。例如,假设您有两个垂直分割的窗口(如上面的答案所示),并且想要在左窗口中搜索多个文件(foo1.txtfoo2.txt,...)以查找 foo,并在右窗口中搜索多个文件(bar1.txtbar2.txt,...)以查找 bar。您可以在左窗口中运行 :lv foo foo*,然后在右窗口中运行 :lv bar bar*。然后,在左窗口中,:lne 将显示下一个匹配项 foo,而不是 bar。这是因为 :lv 使用您运行它的窗口的位置列表,因此每个搜索都有自己的位置列表。 - ma11hew28

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接