Increment numbers in Vim
How to increment numbers in VIM with a quick trick?
101 This is an item.
102 This is an item.
103 This is an item.
104 This is an item.
and so on
There are several ways to make a list of increasing numbers. One simple method is to use Ctrl-A in a macro. As an example, suppose you type the line:
In normal mode, enter the following to record a macro into the
Now type
You will see:
Later, you might insert a new item after #102.
Now you need to renumber the following items (the new item will be 103, so the old 103 has to be incremented, as does 104, and so on.
That can be done using
101 This is an item.
qa101 This is an item.
Y
p
Ctrl-A
q
102 This is an item.
103 This is an item.
104 This is an item.
and so on
There are several ways to make a list of increasing numbers. One simple method is to use Ctrl-A in a macro. As an example, suppose you type the line:
In normal mode, enter the following to record a macro into the
a
register (type the characters shown, without pressing Enter). This
macro yanks the current line, then pastes it below, then increments the
number.
Now type
15@a
to perform the macro 15 times.You will see:
Later, you might insert a new item after #102.
Now you need to renumber the following items (the new item will be 103, so the old 103 has to be incremented, as does 104, and so on.
That can be done using
:.,$g/^\d/exe "normal! \<C-a>"
=====
Superb - Another method - Source - http://vim.wikia.com/wiki/Making_a_list_of_numbers
In Vim 8, the first number in a selection can be incremented by
pressing Ctrl-A. If the selection covers several lines, the first number
in the selection on each line is incremented. Alternatively, numbers in
a selection covering several lines can be converted to a sequence by
typing g Ctrl-A. For example, start with the following line:
my_array[0] = 0;Then copy it using
Y6p
(copy the line and paste it six times). The result is:
my_array[0] = 0; my_array[0] = 0; my_array[0] = 0; my_array[0] = 0; my_array[0] = 0; my_array[0] = 0; my_array[0] = 0;
With the cursor on the first
0
in the first line, start a
blockwise select by pressing Ctrl-V (or Ctrl-Q if you use Ctrl-V for
pasting). Move the cursor down to select the first column of zeros, then
press g Ctrl-A. Using Vim 8, that will give:
my_array[1] = 0; my_array[2] = 0; my_array[3] = 0; my_array[4] = 0; my_array[5] = 0; my_array[6] = 0; my_array[7] = 0;Vim 7 and earlier need a script or a macro to create a sequence.
Comments
Post a Comment