vim: 在所有选定行中播放宏失败

vim: playing a macro in all selected lines fail

我有这样一段文字:

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"},
]

我想在列表的所有对象成员中添加一个"lastName"项,它们应该变成这样:

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"},
]

我的宏是这个

:reg l
--- Registers ---
"l   0f:2lyiw2f,a "lastName": "Last Name 0",^[2hmi2F,hyiw`iviw"0p

并在一行中工作正常,或者如果我附加一个 j 以在末尾向下移动并像 14@l.

那样运行,则工作正常

我无法工作的是在视觉上 select 所有带有 V 的行,然后输入 :normal @l,因为它只会在第一行 select 上工作ed(不管末尾是否有 j 或未附加)。如何让它在所有 selected 行中工作?

你需要做的,一旦你选择了你的行:

:'<,'>norm @l

在所有选中的行中执行。 :normal @l 只在当前行执行。

此外,您可以直接给出一系列行(而不是进入可视模式):

:10,20norm @l

其中 1020 是您要覆盖的范围的第一行和最后一行。

另外,您还可以 运行 在所有匹配模式的行上使用宏:

:g/"name"/norm @l

在匹配 "name"

的所有行上执行宏