Blackninja543
Blackninja543

Reputation: 3709

Vim Insert Sequence with incremental numbers

On a single line I am looking to turn this:

"".format(...)

into this:

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

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

Upvotes: 4

Views: 1771

Answers (4)

SaltyEgg
SaltyEgg

Reputation: 1558

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

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

and then join these lines use '6J'.

Upvotes: 0

Kent
Kent

Reputation: 195059

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

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

execute command:

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

then join (J) the 3 lines. 20 is n in your case.

or "I".format(...)

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

Upvotes: 4

Conner
Conner

Reputation: 31060

I would use a macro, like:

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

Then just use whatever n-1 is with @q. E.g. 4@qXX would give:

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

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172580

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

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

Some vimgolf enthusiasts can probably condense this further. But I would probably either write a mapping for it (if the need occurs so frequently), or do it manually like this:

5i{0}, <Esc>^f0;<C-A>;2<C-A>;3<C-A>;4<C-A>

Upvotes: 2

Related Questions