Vim Tips Wiki
Tip 150 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Charles E. Campbell, Jr. · version 6.0

You can use the Visual Incrementing script to convert a block of numbers selected via Ctrl-v (visual block) into a column of increasing integers. Select the column then enter :I to replace the selection. The first line's number is used as a starting value, and the number in each subsequent line is 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 enter :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;

See also[]


The vis.vim script at Visual Block Commands can apply a substitute to a visual-block.

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

An additional script, calutil.vim, adds some calendrical date to/from Julian day conversion functions. With those, visincr.vim now has new commands:

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

The optional incr (default value is 1) can be positive or negative. Both scripts are available at Vim Functions.