Vim Tips Wiki
Advertisement
Tip 918 Printable Monobook Previous Next

created April 27, 2005 · complexity basic · author Bruno Michel · version 6.0


fun! Incr()
  let l = line(".")
  let c = virtcol("'<")
  let l1 = line("'<")
  let l2 = line("'>")
  if l1 > l2
    let a = l - l2
  else
    let a = l - l1
  endif
  if a != 0
    exe 'normal '.c.'|'
    exe 'normal '.a."\<c-a>"
  endif
  normal `<
endfunction
vnoremap <c-a> :call Incr()<CR>

You can use this script for increasing number in a column. For example, you want to initialize an array with the value i for the element i. You can type:

my_array[0] = 0;

Then you can copy and paste this line.

my_array[0] = 0; my_array[0] = 0;
my_array[0] = 0; my_array[0] = 1;
my_array[0] = 0; my_array[0] = 2;
my_array[0] = 0; --> my_array[0] = 3;
my_array[0] = 0; my_array[0] = 4;
my_array[0] = 0; my_array[0] = 5;
my_array[0] = 0; my_array[0] = 6;

--> means select the second column of 0 by ctrl-v, and type ctrl-a.

By doing the same manipulation on the first column of 0, you obtain:

my_array[0] = 0; my_array[0] = 0;
my_array[0] = 1; my_array[1] = 1;
my_array[0] = 2; my_array[2] = 2;
my_array[0] = 3; --> my_array[3] = 3;
my_array[0] = 4; my_array[4] = 4;
my_array[0] = 5; my_array[5] = 5;
my_array[0] = 6; my_array[6] = 6

This script is very simple, and works pretty well. If you search something more elaborate, there are some others tips/scripts:

Another method using macro is exposed in the help:

  1. Create the first list entry, make sure it starts with a number.
  2. qa - start recording into buffer 'a'
  3. Y - yank the entry
  4. p - put a copy of the entry below the first one
  5. CTRL-A - increment the number
  6. q - stop recording
  7. <count>@a - repeat the yank, put and increment <count> times

Comments

Advertisement