Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increment a number in vim

Tags:

vim

I found a really neat way of incrementing a number in text in vim. It works perfectly fine but only if I yank one line. If there is a whole paragraph in which I want to increment one number it does not work. Here is how it works on single line:

qa
Y
p
Ctrl-A
q

and then type 15@a to perform the macro 15 times.

Does anyone know how to apply the same logic to something like this:

text 1
bla bla 1

So what I want is:

text 2
bla bla 2

text 3
bla bla 3

... and so on
like image 934
daalgoo Avatar asked Aug 31 '25 16:08

daalgoo


1 Answers

One way is to record the following macro:

qayipjo<Esc>pvip<C-a>q

Breakdown:

qa      start recording
yip     yank inner paragraph
jo<Esc> insert line after paragraph
p       paste
vip     select inner paragraph
<C-a>   increment all numbers
q       stop recording

This macro may be repeated via 15@a.

like image 151
Mateen Ulhaq Avatar answered Sep 02 '25 20:09

Mateen Ulhaq