How to create a sequence of lines in the specified line in the edited text?

Here is the source code.

test1 test2 

There are only two lines in the text.

I want to insert a sequence of rows from the 5th row into the 16th row. I tried this using below code.

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

1. The source text.
enter image description here 2. Enter command mode and enter codes

enter image description here

Two problems to solve.
The 1.echo command prints the first line of .item1 to the end.

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

2. How to create a sequence of lines in the specified line: from 5 to 16 in the edited text with vimscript?

The desired result will be as shown below.

enter image description here

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

Both of them cannot 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 problem for my desired format is when the cursor is on the 3rd line, how to create the desired result?

+5
source share
2 answers

To insert missing lines without inserting unoccupied empty lines (-> append() + repeat([''], nb) + possible negative nb )

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

Then, to insert what you are looking for (there is no need for printf() if you do not want to format the numbers)

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

PS: I would rather avoid :put when I can, since it is difficult to use with complex expressions.

+4
source

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

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

Inside vim you can try reading from an external command :

 :r! seq -f 'Item \%.0f' 20 
+1
source

Source: https://habr.com/ru/post/1268076/


All Articles