假设我在一个文件中有多个以下代码块(空格是无关的):
sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg dfgfdg
如何查找/突出显示所有匹配项?
理想情况下,我想要做的是直观地选择代码块,然后按search来查找所有匹配项。
发布于 2009-08-31 05:28:08
搜索的文本存储在/
寄存器中。您不能直接拖入或删除此寄存器,但可以使用‘`let’为其赋值。
试试这个:
使用可视模式突出显示要搜索寄存器类型类型的代码,将突出显示的选项拖入寄存器a
/
中,将寄存器
"ay
复制到搜索寄存器:let @/ = @a
中此时,所有与您的选择匹配的代码都将突出显示,您可以使用n/N在匹配的代码中导航,就像使用常规搜索一样。
当然,您可以使用任何临时寄存器来代替a
。为了方便使用,映射这个命令序列应该不会太难。
发布于 2009-08-31 05:18:23
也许你应该看看:Search for visually selected text
我从here那里拿来的
发布于 2009-08-31 05:20:58
尝尝这个。将此脚本包含在运行时路径中的某个位置(请参阅:help runtimepath
)。一个简单的选择是把它放到你的vimrc中。直观地选择要搜索的内容,然后按,/
(逗号键,然后按正斜键)。
" Search for other instances of the current visual range
" This works by:
" <ESC> Cancel the visual range (it's location is remembered)
" / Start the search
" <C-R>= Insert the result of an expression on
" the search line (see :help c_CTRL-R_= )
" GetVisualRange()<CR> Call the function created below
" <CR> Run the search
vmap ,/ <ESC>/<C-R>=GetVisualRange()<CR><CR>
" Create the function that extracts the contents of the visual range
function! GetVisualRange()
" Get the start and end positions of the current range
let StartPosition = getpos("'<")
let EndPosition = getpos("'>")
" Prefix the range with \V to disable "magic"
" See :help \V
let VisualRange = '\V'
" If the start and end of the range are on the same line
if StartPosition[1] == EndPosition[1]
" Just extract the relevant part of the line
let VisualRange .= getline(StartPosition[1])[StartPosition[2]-1:EndPosition[2]-1]
else
" Otherwise, get the end of the first line
let VisualRange .= getline(StartPosition[1])[StartPosition[2]-1:]
" Then the all of the intermediate lines
for LineNum in range(StartPosition[1]+1, EndPosition[1]-1)
let VisualRange .= '\n' . getline(LineNum)
endfor
" Then the start of the last line
let VisualRange .= '\n' . getline(EndPosition[1])[:EndPosition[2]-1]
endif
" Replace legitimate backslashes with double backslashes to prevent
" a literal \t being interpreted as a tab
let VisualRange = substitute(VisualRange, '\\[nV]\@!', '\\\\', "g")
" Return the result
return VisualRange
endfunction
https://stackoverflow.com/questions/1357060
复制相似问题