在 Vim 中用递增的值搜索和替换 [英] Search and Replace with incremented values in Vim
问题描述
假设我写了一个简单的 CSS 规则,如下所示:
Lets say I wrote a simple CSS rule like this:
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
我需要 10 个,所以我复制了 9 次.
And I need 10, so I copied it 9 times.
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
等
现在我想用递增的值更改 star_10 和 0 0 如下所示:
Now I want to change the star_10 and 0 0 with incremented values so it looks like this:
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_9 {
background: url(stars.png) no-repeat 0 -18px;
}
.star_8 {
background: url(stars.png) no-repeat 0 -36px;
}
.star_7 {
background: url(stars.png) no-repeat 0 -54px;
}
等等...
那么我如何搜索/替换每个实例,进行计算并编写它?
So how can I search/replace every instance, do a calculation and write it?
推荐答案
您可以使用宏轻松完成.假设您只有这个:
You can do it easily with a macro. Lets say you have only this:
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
将光标放在第一个点上(在 .star10
中)并在正常模式下键入以下内容:
Place your cursor over the first dot (in .star10
) and type the following in normal mode:
qa3yy3jp^Xjt;18^Xk0q
说明:
qa
将在寄存器a"中开始录制宏.3yy
将拉取(复制)以下 3 行.3j
会将光标向下放置 3 行.p
将粘贴过去的文本.^X
(ctrl+x) 会递减星级.j
会将光标向下移动一行.t;
会将光标置于当前行中的下一个;
之前.18^X
将背景的 y 坐标减少 18;k
将光标放在一行,0
将光标置于行首.q
将完成宏录制.
qa
will start a macro recording in register "a".3yy
will yank (copy) the following 3 lines.3j
will place the cursor 3 lines down.p
will paste the past yanked text.^X
(ctrl+x) will decrement the star class number.j
will place your cursor one line down.t;
will place your cursor before the next;
in the current line.18^X
will decrement the y coordinate of backround by 18;k
will put the cursor one line up,0
will put the cursor at the beggining of the line.q
will finish the macro recording.
在那之后,你可能会有这样的事情.
After that, you may have something like this.
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
.star_9 {
background: url(stars.png) no-repeat 0 -18;
}
就是这样.只需将光标放在 .star_9 上的点上,然后按 8@a
再执行 8 次寄存器 a
中记录的宏.
That's it. Just place your cursor at the dot on .star_9 and press 8@a
to execute the macro recorded in register a
eight more times.
这篇关于在 Vim 中用递增的值搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!