Wednesday, May 31, 2017

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

Leave a Comment

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?

2 Answers

Answers 1

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.

Answers 2

Assuming you are in a Unix based operating system, you have a seq command. So you can do:

$ seq -f 'Item %.0f' 20 Item 1 Item 2 ... Item 20 

Inside vim you can try the reading from external command approach:

:r! seq -f 'Item \%.0f' 20 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment