在一个折叠内使用行范围

huangapple go评论53阅读模式
英文:

Use line range within a fold

问题

可以将行范围限制在当前折叠内吗?比如说我想要在当前折叠内进行替换,从第二行到折叠末尾,即:

2,$s/str/replace/

但我希望它只适用于当前折叠。

英文:

Is it possible to restrict line range to within current fold? Say I want to do a substitution from second line through end of fold i.e.

2,$s/str/replace/ 

but I want it to apply to current fold only.

答案1

得分: 0

有用于跳转到当前折叠的开头(:help [z)或结束(:help ]z)的动作,但它们没有等效的标记。如果有的话,可以使用虚构 '* 标记来执行以下操作:

:2,'*s/str/replace/

但是该标记并不存在,因此无法正常工作。

实现你想要的一种方法是可视选择该区域:

]zv[zj

然后在可视选择上进行替换:

:'<,'>s/str/replace/

范围 '<,'> 会自动为你插入。

或者你可以按“grok vi”的方式手动设置标记:

]zme[zmb

然后在替换中使用它们:

:'b+,'es/str/replace/

但这似乎不如“可视方式”优化。

另一种解决方法是关闭折叠:

zc

然后在关闭的折叠上进行替换,使用确认选项以跳过发生在第一行的替换:

:s/str/replace/c

因此,这里有两种方法:一种是谨慎定义精确的范围,另一种是谨慎避免替换错误的内容。

英文:

There are motions for jumping to the beginning of the current fold (:help [z) or to its end (:help ]z) but they don't have equivalent marks. If that were the case, one could do this with the imaginary '* mark:

:2,'*s/str/replace/

but that mark doesn't exist so it won't work.

One way to achieve what you want would be to visually select the area:

]zv[zj

and then do your substitution on the visual selection:

:'<,'>s/str/replace/

with the range '<,'> automatically inserted for you.

Or you could go the "grok vi" way and place manual marks:

]zme[zmb

before using them in your substitution:

:'b+,'es/str/replace/

but it seems less optimal than the "visual way".

Another solution would be to close the fold:

zc

and do the substitution on the closed fold, with confirmation so that you can skip the substitutions that occur on the first line:

:s/str/replace/c

So there are two approaches, here: one where care is put into defining the exact range and one where care is put into not substituting the wrong thing.

huangapple
  • 本文由 发表于 2023年1月6日 10:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定