Vim - how to increase each number in visual block?
Visually highlight the text in brackets:
Ctr+V2jl
Increment each number by five:
:norm 5
Ctr+V Ctr+A Explanation:
:norm
executes the whole command in normal mode.
The Ctr+V is necessary, otherwise the cursor would jump back to the beginning of the line.
Ctr+A increases a number by 1, and this is done 5 times.
The visual range is inserted automatically after you pressed the colon.
EDIT: As Stephane correctly pointed out, the previous code increments the first number found on any line. Here's a better solution:
%s/\[\zs\d\+\ze\]/\=(submatch(0)+5)
It adds five to all integers within brackets. The \zs
and \ze
are used to exclude the brackets from the match and submatch
returns the matched number.
We do not need to leave visual mode to increase numbers, just use g
5 g Ctrl-a
5 ......... 5 times
g ......... globally
Ctrl-a .... increase numbers
Actually I have learned this trick in a vingolf challenge.