替换 VIM 中从 = 到行尾的所有内容 [英] Substituting everything from = to end of the line in VIM
问题描述
假设我有几行:
$repeat_on = $_REQUEST['repeat_on'];
$opt_days = $_REQUEST['opt_day'];
$opt_days = explode(",", $opt_days);
...等等.
假设我使用视觉模式选择所有行:如何替换从 =
到行尾的所有内容,使其看起来像:
Let's say I use visual mode to select all the lines: how can I replace everything from =
to the end of the line so it looks like:
$repeat_on = NULL;
$opt_days = NULL;
$opt_days = NULL;
推荐答案
选择块后,使用这个替代:
With the block selected, use this substitute:
s/=.*$/= NULL;
替换正则表达式通过将 =
和行尾之间的任何内容(包括 =
)替换为 = NULL;
来更改每一行.
The substitution regex changes each line by replacing anything between =
and the end of the line, including the =
, with = NULL;
.
命令的第一部分是与要替换的内容匹配的正则表达式:=.*$
.
The first part of the command is the regex matching what is to be replaced: =.*$
.
=
是字面意思.- 点
.
表示任何字符. - 所以
.*
表示:0 个或多个任意字符. - 这由
$
终止,用于 行尾,但这在这里实际上不是必需的:也可以在没有$
的情况下尝试.
- The
=
is taken literally. - The dot
.
means any character. - So
.*
means: 0 or more of any character. - This is terminated by
$
for end of line, but this actually isn't necessary here: try it also without the$
.
所以正则表达式将匹配每一行中第一个=
之后的区域,并将该区域替换为替换,即=NULL;
.我们需要在替换中包含 =
以将其添加回来,因为它是要替换的匹配的一部分.
So the regex will match the region after the first =
in each line, and replace that region with the replacement, which is = NULL;
. We need to include the =
in the replacement to add it back, since it's part of the match to be replaced.
当您选择了一个块,并点击 :
输入命令时,命令行将自动以视觉选择范围作为前缀,如下所示:
When you have a block selected, and you hit :
to enter a command, the command line will be automatically prefixed with a range for the visual selection that looks like this:
:'<,'>
继续输入上面的命令,你的命令行将是:
Continue typing the command above, and your command-line will be:
:'<,'>s/=.*$/= NULL;
将替换应用到选定的视觉块.
Which will apply the replacement to the selected visual block.
如果您需要在一行中进行多个替换,则需要添加 g
标志:
If you'll need to have multiple replacements on a single line, you'll need to add the g
flag:
:'<,'>s/=.*$/= NULL;/g
这篇关于替换 VIM 中从 = 到行尾的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!