加入N行组

时间:2016-03-14 22:09:29

标签: vim

我有一个从旧数据库生成的文本文件,每个字段包含一行,除了知道有多少字段外,记录之间没有分隔符。我想要做的是加入前N行,然后加入下一行N,依此类推。在Vim中有没有办法做到这一点?有没有办法根据任意VimL表达式(如line(".")%5==0)选择行来应用命令,而不仅仅是正则表达式?

1 个答案:

答案 0 :(得分:7)

There are multiple ways of solving this. First that comes to my mind is recording a macro, say in register w:

qw5Jjq

This essentially uses the J normal command to join 5 lines and moves one down. Then you can repeat the macro for 20 times with a simple 20@w or keep repeating afterwards with @@.

Another, maybe more "proper" way is using the :join ex command, which is the same as the J normal command, but can be abbreviated to :j and used in conjunction with the :g to operate in various lines. For example:

:g/./j5

This will match every line non-empty line and in each one of them, join the next 5 lines (inclusive). Then move to the next line and join more 5 and so on.