如何使用 vi 命令重新编号行?

How to renumber lines using a vi command?

我正在尝试使用 vim 命令对文件中的行 2,$ 重新编号,我知道 nl 的命令 cat -n,我可以对行进行编号,但我没有'得到预期的输出:

我试过了:2,$s/^\([^,]\)// | 2,$!cat -n

输入:

#,Name,Types,Total,HP,Attack,Weaknesses,Strength
493,Arceus,Normal,720,120,120,Fighting,strong
483,Dialga,Steel;Dragon,680,100,120,Fighting;Ground,strong
250,Ho-oh,Fire;Flying,680,106,130,Electric;Water;Rock,strong
.... moer 100 lines

预期输出:

#,Name,Types,Total,HP,Attack,Weaknesses,Strength
1,Arceus,Normal,720,120,120,Fighting,strong
2,Dialga,Steel;Dragon,680,100,120,Fighting;Ground,strong
3,Ho-oh,Fire;Flying,680,106,130,Electric;Water;Rock,strong
....

可以用\=来使用sub-replace-expression,用line('.')来获取当前行号:

" The parenthesis around `line('.')-1` are not needed, but it seems clearer to me
:2,$s/^/\=(line('.')-1).','/

编辑:刚刚意识到您实际上是在替换第一列,所以您可能真的想要

:2,$s/^\d\+/\=line('.')-1/