Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a Vim macro to multiple lines

Tags:

json

vim

macros

I made a simple macro to increment a number in a json object like this:

{
    image: 'images/2.jpg',
    thumb: 'images/2-thumb.jpg',
    big: 'images/2.jpg',
    title: '',
    description: '',
    link: 'images/2.jpg'
},

with:

q, n, shift-v, down-till-end, p, move-to-numbers, c-a, return-to-top, q, 150@n

(Sorry if that's not the appropriate syntax to post vim macros here in SE)

And it works, but it makes the increment just until the 9th. What am I missing?

Thanks in advance.

EDIT:

I'm trying to reach something like this:

{
    image: 'images/3.jpg',
    thumb: 'images/3-thumb.jpg',
    big: 'images/3.jpg',
    title: '',
    description: '',
    link: 'images/3.jpg'
},
{
    image: 'images/4.jpg',
    thumb: 'images/4-thumb.jpg',
    big: 'images/4.jpg',
    title: '',
    description: '',
    link: 'images/4.jpg'
},
... until *nth* value
like image 746
ramonovski Avatar asked Dec 02 '25 12:12

ramonovski


1 Answers

Assuming your cursor is on the first opening bracket, here is one way to do it:

qn                    " start recording in register n
V%                    " select from here to the closing bracket, linewise
y                     " yank the selection
%                     " jump to the closing bracket
p                     " paste after the current line
:'[,']norm <C-v><C-a> " executing the normal mode command <C-a>(1) on all the lines that we just pasted
q                     " stop recording

then do 150@n.

(1) <C-v><C-a> is used to insert a literal ^A.

like image 166
romainl Avatar answered Dec 04 '25 06:12

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!