在“vi”中的选择范围内查找和替换

发布于 2024-07-17 09:15:56 字数 36 浏览 3 评论 0原文

如何在 vi 中进行查找和替换?

How do I do a Find and Replace within a selection in vi?

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

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

发布评论

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

评论(5

眼藏柔 2024-07-24 09:15:56

如果您的选择是跨越一行或多行,则可以通过在可视模式下选择文本来完成(我假设这就是您正在做的事情),然后按 : 开始输入命令,您将看到类似这样的内容出现在命令行中:

:'<,'>

这意味着该命令将应用于所选内容。 然后输入 s/search/replace/ 并按 Enter 键。 (如果您想替换所有匹配项,请在第三个斜杠后添加一个 g;如果您想对每次替换进行确认,请添加一个 c

If your selection is a across one or more lines, this can be done by selecting the text in visual mode (I assume that's what you're doing), then press : to start typing a command, you'll see something like this appear in the command line:

:'<,'>

That means that the command will apply to the selection. Then type s/search/replace/ and hit enter. (Add a g after the third slash if you want to replace all matches, and a c if you want a confirmation for every replace)

云朵有点甜 2024-07-24 09:15:56

这里建议的大多数其他解决方案都适用于发生选择的整个行,这可能不是您想要的。

要仅在选择中搜索和替换,首先以可视方式选择文本,然后使用如下命令:

:%s/\%VSEARCH/REPLACE/g

这将仅在可视选择的部分中进行搜索和替换,将 SEARCH 替换为 REPLACE。 如果您选择了多条线,这也适用于多条线。

Most of the other solutions suggested here work over the ENTIRE line in which the selection occurs, which may not be what you want.

To search and replace ONLY in the selection, first visually select the text, then use a command like so:

:%s/\%VSEARCH/REPLACE/g

This will do the search and replace only in the visually selected section, replacing SEARCH with REPLACE. If you have more than one line selected, this will work over multiple lines too.

栖竹 2024-07-24 09:15:56

如果您使用可视模式进行选择,则:

:'<,'>s/regex/replacement/options

如果您进入命令行模式(按 ':') 从可视模式中。

If you used Visual Mode to select, then:

:'<,'>s/regex/replacement/options

VIM will place the range ('<,'>) automatically if you go into Command Line Mode (by pressing ':') from within Visual Mode.

神魇的王 2024-07-24 09:15:56

Ex 命令的范围是按行指定的(请参阅 *cmdline-ranges*),并且当在存在可视选择时按下 : 时,行范围会自动指定在命令行上指定为 '<,'>(请参阅 *v_:*),这使得 :s[ubstitute]命令对整行进行操作,除非在搜索模式中使用 \%V 指定视觉选择边界(请参见 */\%V*),例如 / \%Vvi\%Vm 仅在视觉选择范围内匹配“vim”,其中选择的结尾在搜索模式结束之前指定,因为每个 \%V 指定下一个字符作为视觉选择的开始或结束,因此 /\%Vvim\%V 将要求视觉选择在“m”之后继续以匹配“vim”。 请注意,在搜索模式中使用第二个 \%V 是不必要的,除非匹配项需要位于可视选择的边界处或仅部分位于可视选择的边界中。

The range of Ex commands are specified line-wise (see *cmdline-ranges*), and when : is pressed while there is a visual selection, the line range is automatically specified on the command line as '<,'> (see *v_:*), which makes the :s[ubstitute] command operate on the whole lines unless the visual selection boundaries are specified in the search pattern with \%V (see */\%V*), e.g. /\%Vvi\%Vm matches "vim" only within the visual selection, where the end of the selection is specified right before the end of the search pattern since each \%V specifies the next character as the start or end of the visual selection, and thus /\%Vvim\%V would require the visual selection to continue after 'm' to match "vim". Note that using the second \%V in a search pattern isn't necessary unless a match is required to be right at the border of or only partly in the visual selection.

夜吻♂芭芘 2024-07-24 09:15:56

如果您想对文件中的所有实例进行全局搜索和替换(使用可选的正则表达式),我将执行以下操作:

:%s/foo/bar/g

省略 g 进行本地替换。

If you want to do a global search and replace (with optional regexes) for all instances in the file, I would do the following:

:%s/foo/bar/g

Omit the g to do a local replace.

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