Apply a Vim macro to multiple lines

json, macros, vim

Solution

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`.

Problem

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 ```

Original source