在 Vim 中用递增的值搜索和替换 [英] Search and Replace with incremented values in Vim

查看:50
本文介绍了在 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_100 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

说明:

  1. qa 将在寄存器a"中开始录制宏.
  2. 3yy 将拉取(复制)以下 3 行.
  3. 3j 会将光标向下放置 3 行.
  4. p 将粘贴过去的文本.
  5. ^X (ctrl+x) 会递减星级.
  6. j 会将光标向下移动一行.
  7. t; 会将光标置于当前行中的下一个 ; 之前.
  8. 18^X 将背景的 y 坐标减少 18;
  9. k 将光标放在一行,
  10. 0 将光标置于行首.
  11. q 将完成宏录制.
  1. qa will start a macro recording in register "a".
  2. 3yy will yank (copy) the following 3 lines.
  3. 3j will place the cursor 3 lines down.
  4. p will paste the past yanked text.
  5. ^X (ctrl+x) will decrement the star class number.
  6. j will place your cursor one line down.
  7. t; will place your cursor before the next ; in the current line.
  8. 18^X will decrement the y coordinate of backround by 18;
  9. k will put the cursor one line up,
  10. 0 will put the cursor at the beggining of the line.
  11. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆