【发布时间】:2011-02-24 16:35:43
【问题描述】:
如何将 Vim 中的搜索范围限制为光标当前所在的函数/类/代码块,而不必弄清楚行号是什么?能够在视觉选择中进行搜索也可以,因为有选择当前代码块的方法。
(类似于this 问题,但更通用)
【问题讨论】:
如何将 Vim 中的搜索范围限制为光标当前所在的函数/类/代码块,而不必弄清楚行号是什么?能够在视觉选择中进行搜索也可以,因为有选择当前代码块的方法。
(类似于this 问题,但更通用)
【问题讨论】:
我将复制并粘贴"Searching with / and ?" (within a visual selection) from the Vim Tips Wiki 的全部内容。
在可视模式下,/ 和 ? 会更新 视觉选择就像任何 其他光标移动命令(即 是,当处于可视模式时,搜索 将扩展选择)。
为了在 视觉选择,您将需要使用
\%V原子,或使用标记 由视觉选择定义\%>'<和\%<'>原子。这是 最好离开视觉 输入前用 Esc 选择 您的搜索。您可能要考虑 自动离开的映射 视觉选择并输入 合适的原子。例如::vnoremap <M-/> <Esc>/\%V使用此映射,您可以按 Alt-/为了自动填充 在您搜索的“范围”中,就像 使用带有
:的 Ex 命令。使用 这个,移动到第一行 感兴趣并按 V 开始 逐行视觉选择。下移 (按 j 换行或 } 换行 段等)。当你有 选择您要搜索的区域, 按 Alt-/。视觉选择将 被删除,搜索命令将 开始。你会看到:/\%V添加您要查找的内容,然后按 输入。例如,您可以输入 绿色看看:
/\%Vgreen当你按下 Enter 时,每次出现 “绿色”将突出显示,但 仅在您拥有的区域内 之前选择的。
这里有另外两个例子 不使用视觉选择。首先 命令仅在第 10 行搜索到 20 包括在内。仅第二次搜索 在标记 a 和 b 之间。
/\%>9l\%<21lgreen /\%>'a\%<'bgreen
【讨论】:
为简洁起见:
" tldr;
v i { <ESC> /\%Vsearch-term
" Search for search-term within the current code block (defined by curly braces {}).
" Begin in normal mode, then enter the following:
" enter visual mode
v
" look for stuff in-between the current...
i
" curly braces enclosure
{
" (now the enclosure should be highlighted)
" exit visual mode
<ESC>
" search the last visual mode selection for search-term
/\%Vsearch-term
" note: to search within other enclosures, you can substitute curly braces for:
" - parenthesis,
" - square brackets,
" - or other enclosure pair characters
【讨论】: