我在vim的文件中搜索一个特定的字符串,我想要显示所有匹配字符串的行,也许是在另一个vim窗口中。
目前我这样做:
搜索'字符串'
/string
并转到下一个匹配的字符串
n or N
Bur,我希望所有在一个地方都有匹配字符串的行。
例如:
1这是一个字符串
2这里没什么
3这是相同的字符串
我希望第1行和第3行显示如下,突出显示string
1这是字符串
3这是字符串
答案 0 :(得分:6)
<td>
列出与:g/pattern/#<CR>
匹配的所有行。然后,您可以pattern
跳转到第23行。
:23<CR>
是一种替代方案,可过滤注释并在包含内容中发挥作用。
以下命令:
:ilist pattern<CR>
将使用Vim内置的类似grep的功能在当前文件(:vimgrep pattern %|cwindow<CR>
)中搜索pattern
,并在quickfix窗口中显示结果。
%
执行相同但使用外部程序。请注意,:grep pattern %|cwindow<CR>
和:grep
可以使用文件,而不是缓冲区。
参考:
:vimgrep
FWIW,我的插件vim-qlist结合了:help :g
:help include-search
:help :vimgrep
:help :grep
:help :cwindow
和快速修复窗口的功能。
答案 1 :(得分:3)
从评论中我相信你的文件看起来像这样,即行号不是文本的一部分:
Here is a string
Nothing here
Here is the same string
您可以copy all lines matching a pattern进入指定的注册表(&#34;&#34;在下面的示例中),然后将其粘贴到新文件中:
:g/string/y A
:e newfile
:"ap
哪个可以帮到你:
Here is a string
Here is the same string
或者,您可以使用grep
命令添加-n
以包含行号:
:grep -n string %
1:~/tmp.txt [text] line: 3 of 3, col: 23 (All)
:!grep -nH -n string /home/christofer/tmp.txt 2>&1| tee /tmp/vHg7GcV/3
[No write since last change]
/home/christofer/tmp.txt:1:Here is a string
/home/christofer/tmp.txt:3:Here is the same string
(1 of 2): Here is a string
Press ENTER or type command to continue
默认情况下,您将在&#34;命令缓冲区&#34;中获取输出。在底部(不知道其正确的名称),但you can store it in several different places,例如使用:copen
。