如何使用 vi 或 Vim 在文件中生成数字序列? [英] How to generate a number sequence in file using vi or Vim?

查看:79
本文介绍了如何使用 vi 或 Vim 在文件中生成数字序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 vi 或 Vim 中生成数字序列?

Is there a way to generate a number sequence in vi or Vim?

例如,对于任意范围的行 ij(其中 i <j)在 Vim 中打开的文件中,有没有办法从 i 行上的数字 1 一直到数字 (j - i + 1) 在线 j?

For example, for an arbitrary range of lines i  through j (where i < j) in a file opened in Vim, is there a way to generate a number sequence from number 1 on line i all the way through number (j − i + 1) on line j?

比如说,我在一个文件中有以下几行:

Say, I have the following lines in a file:

this is line #1
this is line #2
this is line #3
this is line #4
this is line #5
this is line #6
this is line #7
this is line #8
this is line #9
this is line #10

我想在第 4 行到第 8 行的数字序列前面加上数字 1 到 5.操作后,生成的文件应如下所示:

I want to prefix the number sequence from line 4 to line 8 with numbers 1 through 5. After the operation, the resulting file should be as follows:

this is line #1
this is line #2
this is line #3
1 this is line #4
2 this is line #5
3 this is line #6
4 this is line #7
5 this is line #8
this is line #9
this is line #10

如果可以的话,有没有办法对生成的序列使用不同的步长?例如,可以用 2 代替步长,这样得到的序列是 2, 4, 6, 8 等吗?

If this is possible, is there a way to use different step sizes for the generated sequence? For example, can 2 be used for the step size instead, so that the resulting sequence is 2, 4, 6, 8, etc.?

注意:问题如何将行号添加到范围Vim 中的行数?"提出了类似的问题,但并不相同.

Note: The question "How to add line numbers to range of lines in Vim?" brings up a similar problem, but it is not the same.

推荐答案

V(Shift-v) 选择几行,然后输入下面的命令:

Select several lines with V(Shift-v), then type command bellow:

:let i=1 | '<,'>g/^/ s//\=i . " "/ | let i+=2

输入 :help sub-replace-expression 阅读更多内容.

Type :help sub-replace-expression to read more.

这篇关于如何使用 vi 或 Vim 在文件中生成数字序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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