VSCode Vim搜索和替换捕获组

时间:2020-01-24 20:02:32

标签: vim visual-studio-code

编辑:我刚刚在VS2019的VSVim扩展中尝试了此操作,并且按预期工作。 我开始认为VSCode的VSCodeVim扩展不能正确处理捕获?

我正在尝试在我的打字稿文件中搜索尚未分配初始值的变量列表,并将其设置为= null

  private __requestor: Req;
  private __feedback: FeedbackObject;
  private __dueDate: Calendar = null;
  private __priority: number = NaN;
  private __bigTest = TestObject;

我正在使用以下命令: :%s/(: [a-zA-Z]+);/\1 = null;/g

我希望输出将= null粘贴在第1、2和5行上,但是它将粘贴\1

预期:

  private __requestor: Req = null;
  private __feedback: FeedbackObject = null;
  private __dueDate: Calendar = null;
  private __priority: number = NaN;
  private __bigTest: TestObject = null;

实际:

  private __requestor\1 = null;
  private __feedback\1 = null;
  private __dueDate: Calendar = null;
  private __priority: number = NaN;
  private __bigTest\1 = null;

我的regex / search&replace命令出问题了吗?它看起来与在我所看到的示例中使用捕获组的其他S&R命令相似,并且我还没有看到“启用”捕获组的任何设置。

2 个答案:

答案 0 :(得分:1)

使用更多正则表达式组:

:%s/\v(__\w+)(:| \=) ([A-Z][a-zA-Z]+);/\1: \3 = null; 

 % ....................... whole file
 \v ...................... very magic (avoid some scapes)
 (__\w+)  ................ first group (matches __word)
 (:| \=)  ................ second group, followed by : or space plus =
 ([A-Z][a-zA-Z]+) ........ third group (matches CamelCaseWords) 
 ; ....................... followed by literal ;

OBS:难以获得正确结果的原因是第5行的模式不同。

答案 1 :(得分:1)

显然,在VSCodeVim中,捕获组是用$1$2等识别的,而不是\1\2等...

因此,使用此方法可行:

:%s/(: [a-zA-Z]+);/$1 = null;/g

来源: https://github.com/VSCodeVim/Vim/issues/4502