sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #150: Generating a column of increasing numbers

 tip karma   Rating 78/30, Viewed by 7530 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 31, 2001 9:03      complexity:   intermediate
author:   Charles E. Campbell, Jr.      as of Vim:   5.7

You can use the "Visual Incrementing" script from

  http://www.erols.com/astronaut/vim/index.html#VimFuncs

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.  Subsequent
lines's numbers 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).

 rate this tip  Life Changing Helpful Unfulfilling 

<< Automatically update your diff upon writing. | an ascii table >>

Additional Notes

[email protected], November 21, 2001 3:26
" another way of generating incremented numbers


"=============================================================================
" File: increment.vim
" Author: Stanislav Sitar ([email protected])
" Help:
" 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 occurence 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
endfunction
[email protected], November 28, 2001 13:04
Visincr.vim has been improved -- it now uses virtual column calculations
which avoid problems with leading tabs -- you may even mix leading
tabs and spaces, incrementing only the visually selected column.
[email protected], December 3, 2001 7:06
Other methods/scripts for incrementing scripts are available as:

    Srinath Avadhanula vimscript#156
    Stanislav Sitar    vimscript#145

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

    http://www.erols.com/astronaut/vim/index.html#VimVuncs
      -- see "Visual Block Commands"
[email protected], November 1, 2002 7:29
<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
[email protected], May 28, 2003 13:00
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 http://www.erols.com/astronaut/vim/index.html#VimFuncs.
[email protected] - NOSPAM, June 30, 2004 6:30
The website mentioned above has been changed to:
http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs
[email protected], October 30, 2006 7:41
why dont macro?
yljgPxh<c-a>
and 5@@


[email protected] - NOSPAM, December 19, 2006 6:42
1. ease of use
2. look at the May 28 note -- visincr handles more than just an incremented/decremented number lists, including dates, roman numerals, alphameric lists, daynames, etc.
3. justification -- visincr's incremented numbers may be either left or right justified
4. zfill (ie. one may have 0s, for example, instead of blanks for a right-justified number list
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo