将通过命令获取的一组文件加载到 Vim 缓冲区中 [英] Loading a set of files obtained via command into Vim buffers

查看:23
本文介绍了将通过命令获取的一组文件加载到 Vim 缓冲区中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常做一个 find -type f |xargs grep 'something' 并尝试在 Vim 中打开这些文件.发生的事情是,我必须关闭和打开 Vim 会话,有时,如果我丢失了结果,我必须再次执行查找操作.有没有办法在 Vim 中自动执行此操作,就像我希望 Vim 将所有这些文件打开到单独的缓冲区中一样,以便我可以在它们之间无缝切换?请注意,我正在查看是否可以从 Vim 中执行这些查找命令(使用 !),然后将这些文件加载​​为缓冲区.

I normally do a find -type f | xargs grep 'something' and try to open those files in the Vim. What is happening is, I have to close and open Vim sessions and sometimes, I have to do the find operation again, if I had lost the result. Is there a way to automate this in Vim, like I would like the Vim to open all these files into separate buffers so that I can switch between them seamlessly? Note that I am looking if I can execute these find commands from within Vim (using !) and then load those files as buffers.

如果您遇到上述情况,您会使用任何其他技巧吗?

Any other tricks you would use, if you are in for above situation?

推荐答案

Vim 有几个命令集成和实现 Grep 功能(请参阅:help grep).两个主要的是 :grep:vimgrep.前者使用外部 Grep 程序(由 grepprg 选项设置),而后者使用在 Vim 本身中实现了核心 Grep 功能.

Vim has several commands integrating and implementing Grep capabilities (see :help grep). The two major ones are :grep and :vimgrep. The former uses external Grep program (set by grepprg option), while the latter uses core Grep functionality implemented in Vim itself.

内部 :vimgrep 的主要优点是 Vim 正则表达式语法和可移植性,包括 ** glob 通配符.:vimgrep 命令理解扩展 glob 模式的子集(参见 :help wildcard).这个允许在大多数情况下避免 find 命令.使用 :vimgrep,您的示例搜索如下所示.

The main advantages of the internal :vimgrep are Vim regex syntax and portability, that includes ** glob wildcard. The :vimgrep command understands a subset of extended glob patterns (see :help wildcard). This allows to avoid find command in most of the cases. Using :vimgrep, your example search would look like the following.

:vimgrep /something/ **/*

同样的搜索可以用 :grep 命令执行,如果你的 shell 或grepprg 实现了 ** 通配符.下面是一个显示 Zsh 的例子扩展的全局符号.

The same search can be performed with :grep command, if your shell or grepprg implements the ** wildcard. Below is an example showing Zsh extendned glob notation.

:grep 'something' **/*(.)

这两个命令(及其变体)都利用 Vim 界面功能称为 QuickFix 列表及其窗口本地版本、位置列表(请参阅 :help快速修复).QuickFix 窗口是一个特殊的只读缓冲区,包含任何一种与一个或多个文件相关的搜索结果.搜索结果由 Grep 命令收集的数据会立即聚合在 QuickFix 窗口中.

Both of these commands (and their variants) utilize Vim interface features called QuickFix list and its window-local version, location list (see :help quickfix). The QuickFix window is a special read-only buffer containing any kind of search results relating one or several files. Search results collected by Grep commands are immediately aggregated in the QuickFix window.

QuickFix 列表表示搜索模式的每个匹配位置一行,并允许通过按在它们之间快速切换在这些行上输入.没有几个相关的命令快速修复列表.这里我只列出其中一些作为起点(请参阅帮助以获取更多信息).

The QuickFix list represents each matching position of the search pattern by a single line, and allows to quickly switch between them by pressing Enter on these lines. There is not a few commands related to the QuickFix list. Here I list only some of them as a starting point (see help for additional information).

  • :cw:cope 打开 QuickFix 窗口(请参阅帮助以了解它们之间的区别).
  • :cc, :cn, :cp 显示当前、下一个、上一个分别在列表中匹配.
  • :cr, :cla 显示列表中的第一个和最后一个匹配项,
  • :ccl 关闭 QuickFix 窗口.
  • :cw or :cope open the QuickFix window (see help to understand the difference between them).
  • :cc, :cn, :cp display the current, the next, and the previous match in the list, respectively.
  • :cr, :cla display the first, and the last match in the list, respectively.
  • :ccl closes the QuickFix window.

位置列表是附加到某个窗口的 QuickFix 列表.每个窗口可以有一个附加的位置列表(独立于其他windows 的位置列表和 QuickFix 列表).任何 QuickFix 列表命令上面列出的那些具有与位置列表等效的工作.

A location list is a QuickFix list attached to a certain window. Each window can have a single location list attached to it (independent from other windows' location lists and the QuickFix list). Any QuickFix list command of those listed above has an equivalent working with a location list.

  • :lw:lop 打开当前关联的位置列表窗口.
  • :ll, :lne, :lp 显示当前、下一个、上一个分别在列表中匹配.
  • :lr, :lla 显示列表中的第一个和最后一个匹配项,
  • :lcl 关闭位置列表.
  • :lw or :lop open the location list associated with the current window.
  • :ll, :lne, :lp display the current, the next, and the previous match in the list, respectively.
  • :lr, :lla display the first, and the last match in the list, respectively.
  • :lcl closes the location list.

:grep:vimgrep 命令的对应位置列表是 :lgrep:lvimgrep,分别.

Location list counterparts of the :grep and :vimgrep commands are :lgrep and :lvimgrep, respectively.

这篇关于将通过命令获取的一组文件加载到 Vim 缓冲区中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆