I have a text file generated from an old database that consists of one line per field, with no delimiter between records other than knowing how many fields there are. What I'd like to do is join the first N lines, and then the next N, and so on. Is there any way to do this within Vim? Is there were a way to select lines to apply a command to based on an arbitrary VimL expression (like line(".")%5==0
) instead of just a regex?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With