限制 Vim 中代码的搜索范围

发布于 2024-09-03 01:12:44 字数 206 浏览 8 评论 0原文

如何将 Vim 中的搜索范围限制为光标当前所在的函数/类/代码块,而不必弄清楚行号是什么?能够在视觉选择中进行搜索也可以,因为有选择当前代码块的方法。

(类似于这个问题,但更通用)

How can I limit the search scope in Vim to the function/class/code block that the cursor is currently in, without having to figure out what the line numbers are? Being able to search in the visual selection would also do, as there are methods for selecting the current code block.

(Similar to this question, but more generic)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-09-10 01:12:44

我将复制并粘贴 "使用 / 和 进行搜索 ? 的全部内容” (在视觉选择中)来自 Vim Tips Wiki

在可视模式下,/? 将更新
视觉选择就像任何选择一样
其他光标移动命令(即
是,当处于视觉模式时,搜索
将扩大选择范围)。

为了实际在
视觉选择,您将需要使用
\%V 原子,或使用标记
由视觉选择定义
\%>'<\%<'> 原子。这是
最好通过留下视觉效果来完成
输入前使用 Esc 选择
你的搜索。您可能需要考虑
自动离开的映射
视觉选择并输入
适当的原子。例如:

:vnoremap ; /\%V

使用此映射,您可以按
Alt-/以便自动填充
在您的搜索的“范围”中,就像
使用带有 : 的 Ex 命令。使用
移动到第一行
感兴趣并按V开始
逐行视觉选择。下移
(按 j 获取一行或按 } 获取一行
段落等)。当你有
选择您要搜索的区域,
Alt-/。视觉选择将
被删除,并且搜索命令将
开始。您将看到:

<前><代码>/\%V

添加您要查找的内容,然后按
输入。例如,您可以输入
绿色并查看:

<前><代码>/\%Vgreen

当您按Enter时,每次出现
“绿色”将突出显示,但是
仅在您拥有的区域
之前选择的。

这里有两个进一步的例子
不使用视觉选择。第一个
命令仅在第 10 行搜索
20 含。第二个仅搜索
在标记 a 和 b 之间。

<前><代码>/\%>9l\%<21l绿色
/\%>'a\%<'bgreen

I'm going to just copy and paste the entire content of "Searching with / and ?" (within a visual selection) from the Vim Tips Wiki.

In visual mode, / and ? will update
the visual selection just like any
other cursor-movement command (that
is, when in visual mode, searching
will extend the selection).

In order to actually search within the
visual selection, you will need to use
the \%V atom, or use the markers
defined by the visual selection with
the \%>'< and \%<'> atoms. This is
best done by leaving the visual
selection with Esc before entering
your search. You may want to consider
a mapping to automatically leave
visual selection and enter the
appropriate atoms. For example:

:vnoremap <M-/> <Esc>/\%V

Using this mapping, you can press
Alt-/ in order to automatically fill
in a "range" for your search just like
using an Ex command with :. To use
this, move to the first line of
interest and press V to start
line-wise visual selection. Move down
(press j for a line or } for a
paragraph, etc). When you have
selected the area you want to search,
press Alt-/. The visual selection will
be removed, and a search command will
start. You will see:

/\%V

Add what you want to find, then press
Enter. For example, you may enter
green and see:

/\%Vgreen

When you press Enter, each occurrence
of "green" will be highlighted, but
only in the area that you had
previously selected.

Here are two further examples that do
not use a visual selection. The first
command searches only in lines 10 to
20 inclusive. The second searches only
between marks a and b.

/\%>9l\%<21lgreen
/\%>'a\%<'bgreen
追我者格杀勿论 2024-09-10 01:12:44

为简洁起见:

" 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

For brevity:

" 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文