I have a file in which first 10 lines are the columns of a table and the rest 10 lines are the values of each column.
How can I use norm
in VIM to append the values after each column names like this:
column1
...
column10
value1
...value10
-->
column1: value1
...
column10: value10
It is a little similar with this(Vim - Copy Nth word of each line, from line 10-100, to end of line), but I don't know how to go to line 1:10 and append the copied lines.
Any idea will be appreciated!
Fairly naive and crude way to do this, but:
:1,10norm! 10j0d$10kA: ^[p
Explanation:
1,10norm!
: for lines 1 to 10, do the following (the !
means any custom mapping you have will be ignored, thanks to D. Ben Knoble for reminding of this):10j
: move down 10 lines0d$
: delete the whole line (not including newline)10k
: move back up 10 linesA:
: append (at the end of the line) ': ' (note the trailing space)^[
: input escape character, going back to normal mode. This (^[
) is a single character and is inputted by typing Ctrl-v then escape, not by typing ^[
.p
: paste the line deleted in step 3Another (copy-pastable) way, (ab)using the s
ubstitute command:
:1,10s/\v(.*)\ze(.*\n){10}(.*)/\1: \3/ | 11,20d
which does:
1,10s/
: for lines 1 to 10, execute the following substitution:\v
: use very-magic
regex mode (see :help \v
)(.*)
: capture the entire current line (eg column1
)\ze
: signal the end of the match. This way everything read (and captured) afterwards will not be affected (but can still be read)(.*\n){10}
: skip 10 (including current) lines, ie skip selector to 10 lines below(.*)
: capture the line (eg value1
)/
: end the 'select' part of the substitute command\1: \3
: replace with captured groups (eg column1: value1
)|
: command separator11,20d
: delete lines 11 to 20