Vim Tips Wiki
(fix DrChip changed URL + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=150
 
|id=150
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
You can use the "Visual Incrementing" script from http://drchip.0sites.net/astronaut/vim/index.html#VISINCR to convert a block of numbers selected via Ctrl-v (visual block) into a column of increasing integers. Select the column, press <tt>:I<CR></tt>, and the first line's number will be used as a starting value. The number in each subsequent line will be incremented by one.
+
You can use the [http://www.drchip.org/astronaut/vim/index.html#VISINCR 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 <code>:I</code> 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.
 
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 <tt>:I</tt>
+
Example: Put cursor on topmost zero, select column with Ctrl-v, then enter <code>:I</code>
 
<pre>
 
<pre>
 
vector[0] = 1; vector[0] = 1;
 
vector[0] = 1; vector[0] = 1;
Line 25: Line 24:
 
</pre>
 
</pre>
   
  +
==See also==
This script works with both Vim 5.7 (<tt>:so visincr.vim</tt>) or Vim 6.0 (source it as for Vim 5.7 or drop it into the .vim/plugin directory).
 
  +
*[[Increasing or decreasing numbers]]
  +
*[[Making a list of numbers]]
   
 
==Comments==
 
==Comments==
  +
The vis.vim script at [http://www.drchip.org/astronaut/vim/index.html#VIS Visual Block Commands] can apply a substitute to a visual-block.
<pre>
 
" 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
 
endfunction
 
</pre>
 
   
 
----
 
----
 
visincr.vim supports:
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:
 
* Srinath Avadhanula {{script|id=156}}
 
* Stanislav Sitar {{script|id=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://drchip.0sites.net/astronaut/vim/index.html#VimFuncs -- see "Visual Block Commands".
 
 
----
 
<visincr.vim> supports:
 
 
 
<pre>
 
<pre>
:I<CR> will use the first line's number as a starting point, incrementing by 1
+
: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
+
: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> will pad on left as needed, otherwise like :I
 
:II #<CR> like :II, but will increment by given number
 
:II #<CR> like :II, but will increment by given number
 
</pre>
 
</pre>
   
 
----
 
----
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:
+
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] : makes a column of month/day/year dates
+
*<code>IMDY [incr]</code> : make a column of month/day/year dates
* IYMD [incr] : makes a column of year/month/day dates
+
*<code>IYMD [incr]</code> : make a column of year/month/day dates
* IDMY [incr] : makes a column of day/month/year dates
+
*<code>IDMY [incr]</code> : make a column of day/month/year dates
* ID [incr] : makes a column of daynames
+
*<code>ID [incr]</code> : make a column of day names
   
Of course, the optional incr (default value is 1) can be positive or negative. Both scripts are available at http://drchip.0sites.net/astronaut/vim/index.html#VimFuncs.
+
The optional incr (default value is 1) can be positive or negative. Both scripts are available at [http://www.drchip.org/astronaut/vim/index.html#VimFuncs Vim Functions].
   
 
----
 
----

Latest revision as of 05:17, 13 July 2012

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[]

Comments[]

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.