Vim Insert Sequence with incremental numbers

In one line, I want to include this:

"".format(...) 

in it:

 "{0}, {1}, {2}, ..... {n}".format(...) 

where n is the number of elements I want to repeat. Without the need to manually insert each argument.

+4
source share
4 answers

I don't know if this way is right for you:

 "I(cursor here) ".format(...) 

execute the command:

 :r! seq -s, -f "{\%g}" 0 20 

then append ( J ) 3 lines. 20 - n in your case.

or "I".format(...)

to insert mode, ctrl-R , then type =system("seq -s, -f '{%g}' 0 20")

+4
source

With a cursor between empty double quotes, I would do (for n = 5):

 i<CR>=join(map(range(5), 'printf("{%d}", v:val)'), ', ')<CR> 

Some wimgolf enthusiasts can probably condense it further. But I would probably either write a mapping for it (if this happens so often), or do it manually:

 5i{0}, <Esc>^f0;<CA>;2<CA>;3<CA>;4<CA> 
+2
source

I would use a macro, for example:

 0a{0}, <esc>lqqyF{f"PB<Ca>;q 

Then just use any n-1 with @q . For instance. 4@qXX will give:

 "{0}, {1}, {2}, {3}, {4}, {5}".format(...) 
0
source

There is no "seq" command on Windows, so I prefer this:

 :call append(".", map(range(6), '"{".v:val."},"')) 

and then concatenate these lines, use '6J'.

0
source

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


All Articles