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

时间:2019-05-16 14:50:31

标签: vim macros vim-macros

我有一段这样的文字:

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一样运行,则可以正常运行。

我无法进行的工作是用V直观地选择所有行,然后键入:normal @l,因为它将仅在所选的第一行上起作用(无论是否有{{1} }或未附加在末尾)。如何使其在所有选定的行中起作用?

1 个答案:

答案 0 :(得分:2)

一旦选择了行,就需要这样做:

:'<,'>norm @l

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

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

:10,20norm @l

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

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

:g/"name"/norm @l

在与"name"匹配的所有行上执行宏