Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create the strings sequence into specified line in edited text?

Tags:

vim

Here is the initial text.

test1
test2

Only two lines in the text.

I want to insert strings sequence into from 5th line into 16th line. I have tried it with below codes.

for i in range(1,12)  
    echo ".item".i.","
endfor  

1.the initial text.
enter image description here 2.to enter into command mode and input the codes

enter image description here

Two problems to be solved.
1.echo command output the first string .item1 before endfor.

for i in range(1,12)  
    echo ".item".i.","

2.How create the strings sequence into specified line:from 5th till 16th in edited text with vimscript?

The desired result is as below.

enter image description here

Almost done!
What i get is as below with the command :pu! =map(range(1,12), 'printf(''item%1d'', v:val)').

Both of them can't work.

:5pu! =map(range(1,12), 'printf(''item%1d'', v:val)')
:5,16pu! =map(range(1,12), 'printf(''item%1d'', v:val)')

enter image description here

The last issue for my desired format is when the cursor is on the 3th line ,how to create the desired output?

like image 226
showkey Avatar asked May 22 '17 02:05

showkey


1 Answers

In order to insert the missing lines, without inserting unrequired empty lines (-> append() + repeat([''], nb) + possible negative nb)

:let lin = 5 - 1
:call append('$', repeat([''], lin-line('$')))

Then, in order to insert what you're looking for (no need for printf() if you don't want to format the numbers)

:call append(lin, map(range(1,12), '"item".v:val'))

PS: I'd rather avoid :put when I can as it's kind of difficult to use with complex expressions.

like image 198
Luc Hermitte Avatar answered Sep 18 '22 18:09

Luc Hermitte