Vim Tips Wiki

Generating a column of increasing numbers

Revision as of 08:33, September 28, 2008 by JohnBot (Talk | contribs)

1,619pages on
this wiki
Tip 150 Printable Monobook Previous Next

created October 31, 2001 · complexity intermediate · author Charles E. Campbell, Jr. · version 5.7

You can use the "Visual Incrementing" script from to convert a block of numbers selected via ctrl-v (visual block) into a column of increasing integers. Select the column, press :I<CR>, and the first line's number will be used as a starting value. The number in each subsequent line will be incremented by one.

If the ctrl-v block is "ragged right", which can happen when "$" is used to select the right hand side, the block will have spaces appended as needed to straighten it out. If the strlen of the count exceeds the visual-block allotment of spaces, then additional spaces will be inserted.

Example: Put cursor on topmost zero, select column with ctrl-v, then :I

vector[0] = 1;       vector[0] = 1;
vector[0] = 1;       vector[1] = 1;
vector[0] = 1;  -->  vector[2] = 1;
vector[0] = 1;       vector[3] = 1;
vector[0] = 1;       vector[4] = 1;

This script works with both Vim 5.7 (:so visincr.vim) or Vim 6.0 (source it as for Vim 5.7 or drop it into the .vim/plugin directory).


" another way of generating incremented numbers
" File: increment.vim
" Author: Stanislav Sitar
" Put increment.vim into a plugin directory.
" Use in replacement strings
" :let I=0
" :%s/my_token_word_to_be_replaced_by_the_auto_incremented_numbers/\=INC(1)/
" or
" :let I=95
" :%s/@/\=INC(5)/
" to replace each occurrence of character @ with numbers starting with 100 and
" growing by 5 (100, 105, 110, ...)
" Instalation: save this text as increment.vim in your plugins directory
let g:I=0
function INC(increment)
  let g:I = g:I + a:increment
  return g:I

Visincr.vim has been improved -- it now uses virtual column calculations which avoids problems with leading tabs -- you may even mix leading tabs and spaces, incrementing only the visually selected column.

Other methods/scripts for incrementing scripts are available as:

If you're interested in using substitute based approaches, you might wish to consider Stefan Roemer's <vis.vim>, which allows one to apply a substitute to just a visual-block. You can get a copy of his script at -- see "Visual Block Commands".

<visincr.vim> supports:

:I<CR> will use the first line's number as a starting point, incrementing by 1
:I #<CR> like :I, but will increment by given number; negative numbers work fine
:II<CR> will pad on left as needed, otherwise like :I
:II #<CR> like :II, but will increment by given number

More features for <visincr.vim>! There's now an additional script, <calutil.vim>, which adds some calendrical dates <-> Julian day conversion functions. With those, <visincr.vim> now has new commands:

  • IMDY [incr] : makes a column of month/day/year dates
  • IYMD [incr] : makes a column of year/month/day dates
  • IDMY [incr] : makes a column of day/month/year dates
  • ID [incr] : makes a column of daynames

Of course, the optional incr (default value is 1) can be positive or negative. Both scripts are available at

Around Wikia's network

Random Wiki