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.
 
 2.to enter into command mode and input the codes
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.
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)')     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      


0 comments:
Post a Comment