Vim:在打开的缓冲区中搜索

发布于 2024-10-19 04:50:52 字数 238 浏览 2 评论 0原文

我喜欢 Visual Studio 的一项功能是能够仅在打开的文件中进行搜索。例如,如果我最近对某些文件进行了更改,并且我想跟踪这些更改,我可能会搜索某个单词,但仅在这些文件中搜索,以避免获得大量必要的匹配项。

Vim 可以做到这一点吗?!我感兴趣的是能够打开我已更改的文件以便使用:

gvim `git diff --name-only`

然后在这些文件中搜索我想要的内容。

One features I like with Visual Studio is the ability to search in open files only. For example, if I recently did changes to some files and I would like to trace those changes, I might search for a certain word, but only in those files to avoid getting a large list of necessary matches.

Is this possible with Vim?! What I am interested in is being able to open the files I have changed so for using:

gvim `git diff --name-only`

then search those files for what I want.

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

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

发布评论

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

评论(2

梦行七里 2024-10-26 04:50:52

一个很好的方法是使用 vim 的内部 grep 命令 (:vim):

:vim /pattern/ `git diff --name-only`
:copen

这将打开一个小窗口(称为 quickfix),其中包含搜索结果和要打开的链接相应的文件(不必打开)。

A nice way to do that is to use vim's internal grep command (:vim):

:vim /pattern/ `git diff --name-only`
:copen

This will open a small window (called quickfix) with the search results and links to open the corresponding files (they don't have to be open).

拿命拼未来 2024-10-26 04:50:52

如果您希望 vim 打开自己的缓冲区中的所有文件以查找与您的 diff 匹配的文件,您可以尝试以下操作:

gvim $(grep -l pattern $(git diff --relative --name-only))

git diff --relative --name-only 显示索引中已更改的文件但文件名相对于当前工作目录。

grep -l pattern 将报告包含 pattern 匹配的文件名。 (请注意,该模式只需存在于文件中,存在于 git diff 输出中。)

POSIX $() 而不是反引号使得可以使用嵌套命令。

If you want vim to open up all the files in their own buffers for files that match your diff, you could try this:

gvim $(grep -l pattern $(git diff --relative --name-only))

git diff --relative --name-only shows the changed files in the index but with filenames relative to the current working directory.

grep -l pattern <list of files> will report the filenames that contain a match on pattern. (Note that the pattern just has to exist in the files, not in the git diff output.)

POSIX $() instead of backticks makes using nested commands possible.

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