当我在使用vim读取日志文件时,我想搜索几个单词,因此更简单的方法是可视化选择这些单词,然后按一个键,以便vim可以搜索我选择的单词。
但是该怎么做..?
非常感谢
这已在“在 vim 中搜索选择”中进行了解释。
v
y
/
<C-r>0
使用( Ctrlr+ 0)粘贴最后一个被拉出的文本:h i_ctrl-r
(感谢评论中的romainl)另一个例子是使用命令行:
v
y
q/
p
,然后按搜索Enter
简而言之:yq/p<Enter>
Bairui/Dahu 的SearchParty有几个漂亮的映射,它们建立在“yank and insert”方法的基础上,但可以干净地处理换行符等:
* Searches for the next occurrence of the currently selected visual text.
# Searches for the prior occurrence of the currently selected visual text.
& Starts a :substitute using the currently selected visual text.
如果你觉得一个插件太过分了,很容易从脚本中拉出相关的行并将其放入你的~/.vimrc
.
哦,伙计,实际上您可以使用由《实用 Vim》一书的作者发起的强大的Visual Search。它非常轻巧!
只需将以下内容添加到您的 vimrc 中,
xnoremap * :<C-u>call <SID>VSetSearch()<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch()<CR>?<C-R>=@/<CR><CR>
function! s:VSetSearch()
let temp = @s
norm! gv"sy
let @/ = '\V' . substitute(escape(@s, '/\'), '\n', '\\n','g')
let @s = temp
endfunction
视觉选择您要搜索的内容,然后按星键*
,瞧!
要一次搜索多个单词,vim 搜索支持或 '\|'。因此,要搜索 dog cat 和 bird,您可以:
/dog\|cat\|bird
或更好(精确的单词匹配):
/\<dog\>\|\<cat\>\|\<bird\>