vimtricks.wiki Concise Vim tricks, one at a time.

How do I generate a sequence of numbers or lines and insert them into the buffer?

Answer

:put =range(1,10)

Explanation

:put pastes the result of an expression into the buffer. Combined with Vim's range() function, it generates numbered sequences, lists, and repetitive text — all without macros, external tools, or copy-paste.

How it works

  • :put =range(1,10) — inserts lines containing 1 through 10 below the cursor
  • :put =range(0,100,5) — inserts 0, 5, 10, 15, ..., 100 (step of 5)
  • :put =range(10,1,-1) — inserts 10 down to 1 (reverse)
  • :0put =range(1,5) — inserts at the top of the file

Beyond numbers

Generate formatted text with map():

:put =map(range(1,5), '"Item " . v:val . ": TODO"')

Produces:

Item 1: TODO
Item 2: TODO
Item 3: TODO
Item 4: TODO
Item 5: TODO

Generate HTML list items:

:put =map(range(1,3), '"<li>Item " . v:val . "</li>"')

Repeat a string N times:

:put =repeat(['---'], 5)

Inserts 5 lines of ---.

Tips

  • :put always inserts below the current line; :put! inserts above
  • :put = accepts any Vimscript expression — you can call functions, concatenate strings, and compute values
  • range() returns a List, and :put inserts each list element as a separate line
  • For inserting in Insert mode, use <C-r>=range(1,5) — though this inserts the list representation, not individual lines

Next

How do I run a search and replace only within a visually selected region?