12

Is there a way in Vim to paste the search register on the command-line, but without the surrounding \< brackets \>? I often find myself doing a search in a buffer, and then wanting to use the matched pattern as an argument to grep (more specifically, ack.vim).

Here's what happens if you search for foo and then enter :Ack '<C-r>/':

:Ack '\<foo\>'

This will fail to find anything. What I want instead is:

:Ack 'foo'

This is of course a simplistic example. Where this would be more valuable is with more complex search results.

I am aware of <C-r><C-w> and <C-r><C-a> for pasting the word/WORD under the cursor, and these often suffice, but not always.

4

2 回答 2

12

<C-r>/在您键入时插入您的搜索模式:如果您这样做了/foo,则搜索寄存器包含foo.

请注意,<C-r>/插入搜索模式,而不是匹配:如果您搜索foo\d<C-r>/将插入foo\d,而不是foo9

正如您所发现的,如果您使用or以将搜索限制为整个单词\<,则会添加。使用and不搜索整个单词,从而避免使用.\>*#g*g#\<\>

这是一个可能有用的映射:

nnoremap <F6> :Ack '<C-r>=expand("<cword>")<CR>'
于 2013-10-14T21:46:45.953 回答
4

除非您正在执行纯文字搜索,否则搜索模式(正则表达式)和(列表)匹配项(来自缓冲区的字符串)之间存在差异。

如果你对后者感兴趣,我的PatternComplete 插件提供了所有匹配的插入模式完成,它还可以插入最后一个搜索模式的第一个匹配<C-R>&

于 2013-10-15T08:01:25.973 回答