How do I use a back reference in the cmd executed by :g?

北慕城南 提交于 2019-12-12 19:08:14

问题


When answering Remove EOL spaces of selection only if there are, I noticed that my answer is not really satisfying: the pattern \ \+$ has to be typed twice:

  • once for searching with :g
  • once for replacing with :s

First thought was to simply use g/\(\ \+$\)/s/\1//g but this gives me an error about an invalid back reference (E65).

Is there a way to re-use the pattern used in :g for the [cmd] being executed?


回答1:


You don't need \1:

:g/ \+$/s///g

:g/pat/s//PAT/g

is same as:

:%s/pat/PAT/g


来源:https://stackoverflow.com/questions/9804484/how-do-i-use-a-back-reference-in-the-cmd-executed-by-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!