I have a block of text like this:
var a = [
{"id": 1, "name": "Name 1", "description": "Description for item #1"},
{"id": 2, "name": "Name 2", "description": "Description for item #2"},
{"id": 4, "name": "Name 4", "description": "Description for item #4"},
{"id": 5, "name": "Name 5", "description": "Description for item #5"},
{"id": 6, "name": "Name 6", "description": "Description for item #6"},
{"id": 7, "name": "Name 7", "description": "Description for item #7"},
{"id": 8, "name": "Name 8", "description": "Description for item #8"},
{"id": 9, "name": "Name 9", "description": "Description for item #9"},
{"id": 10, "name": "Name 10", "description": "Description for item #10"},
{"id": 11, "name": "Name 11", "description": "Description for item #11"},
{"id": 12, "name": "Name 12", "description": "Description for item #12"},
{"id": 13, "name": "Name 13", "description": "Description for item #13"},
{"id": 14, "name": "Name 14", "description": "Description for item #14"},
{"id": 15, "name": "Name 15", "description": "Description for item #15"},
]
I want to add a "lastName" item in all objects members of the list, and they should become something like this:
var a = [
{"id": 1, "name": "Name 1", "lastName": "Last Name 1", "description": "Description for item #1"},
{"id": 2, "name": "Name 2", "lastName": "Last Name 2", "description": "Description for item #2"},
{"id": 4, "name": "Name 4", "lastName": "Last Name 4", "description": "Description for item #4"},
{"id": 5, "name": "Name 5", "lastName": "Last Name 5", "description": "Description for item #5"},
{"id": 6, "name": "Name 6", "lastName": "Last Name 6", "description": "Description for item #6"},
{"id": 7, "name": "Name 7", "lastName": "Last Name 7", "description": "Description for item #7"},
{"id": 8, "name": "Name 8", "lastName": "Last Name 8", "description": "Description for item #8"},
{"id": 9, "name": "Name 9", "lastName": "Last Name 9", "description": "Description for item #9"},
{"id": 10, "name": "Name 10", "lastName": "Last Name 10", "description": "Description for item #10"},
{"id": 11, "name": "Name 11", "lastName": "Last Name 11", "description": "Description for item #11"},
{"id": 12, "name": "Name 12", "lastName": "Last Name 12", "description": "Description for item #12"},
{"id": 13, "name": "Name 13", "lastName": "Last Name 13", "description": "Description for item #13"},
{"id": 14, "name": "Name 14", "lastName": "Last Name 14", "description": "Description for item #14"},
{"id": 15, "name": "Name 15", "lastName": "Last Name 15", "description": "Description for item #15"},
]
my macro is this
:reg l
--- Registers ---
"l 0f:2lyiw2f,a "lastName": "Last Name 0",^[2hmi2F,hyiw`iviw"0p
and works fine in one line, or works fine if I append a j
to move down at the end and runs like 14@l
.
What I cannot make work is to visually select all the lines with V
, then typing :normal @l
, as it will only work on the first line selected (regardless if there is a j
or not appended at the end). How to make it work in all the selected lines?
You need to do, once your lines selected:
:'<,'>norm @l
To execute in all the selected lines. :normal @l
executes it only on the current line.
Also, you can give a range of lines directly (instead of going in visual mode):
:10,20norm @l
Where 10
and 20
are your first and last line of the range to cover.
As an extra, your can also run a macro on all the lines matching a pattern:
:g/"name"/norm @l
Which executes the macro on all the lines matching "name"