在Vim(Ruby)中用do/end替换匹配的{brates}

在Vim(Ruby)中用do/end替换匹配的{brates},ruby,vim,Ruby,Vim,是否有人在Vim中使用插件或宏将匹配的{大括号}替换为do和end?最好像这样旋转单行语句: foo.each { |f| f.whatever } 进入: 我可以为这种情况自己制作一个宏,但我希望能够处理转换现有多行、可能复杂的块的问题,例如: foo.each { |f| f.bars.each { |b| b.whatever } hash = { a: 123, b: 456 } } 进入: 我已经查看了和,但没有找到任何方法。我假设在您的多行示例中,输出将是: foo.ea

是否有人在Vim中使用插件或宏将匹配的
{
大括号
}
替换为
do
end
?最好像这样旋转单行语句:

foo.each { |f| f.whatever }
进入:

我可以为这种情况自己制作一个宏,但我希望能够处理转换现有多行、可能复杂的块的问题,例如:

foo.each { |f|
  f.bars.each { |b| b.whatever }
  hash = { a: 123, b: 456 }
}
进入:


我已经查看了和,但没有找到任何方法。

我假设在您的多行示例中,输出将是:

foo.each do |f|
  f.bars.each do |b| b.whatever end
  hash = { a: 123, b: 456 }
end
也就是说,
f.bar。每个{…}
也应该被替换

如果这是目标,请尝试以下方法:

gg/each\s*{<enter>qqf{%send<esc><c-o>sdo<esc>nq200@q
gg/each\s*{qqf{%sendsdonq200@q
简要说明:

gg               " move cursor to top
/each\s*{<enter> " search pattern we want
qq               " start recording macro to register q
f{               " move to {
%send<esc>       " move to closing {, and change it to "end", back to normal
<c-o>sdo         " back to beginning { and change it into "do"
<esc>nq          " back to normal, and go to next match, stop recording
gg“将光标移到顶部
/每个\s*{我们想要的搜索模式
qq“开始录制宏以注册q
f{“移至{
%发送“移动到关闭{”,并将其更改为“结束”,恢复正常
sdo“返回开始{并将其更改为“do”
nq“恢复正常,进入下一场比赛,停止录音

然后您可以执行例如
200@q
并检查结果。

有一个名为Vim的插件执行此功能


安装插件后,将光标放在
{
}
do
end
上,然后按
b
交换块样式。

对ruby知之甚少。但为什么您的多行示例(输出)只替换了外部
{,}
,而内部的
f.bar{…
不支持?最好同时支持这两个语句,但通常我只希望一次扩展一个级别。我的情况是,我更喜欢单语句的内联块,但我经常发现自己想要添加另一个或两个语句,我想为此扩展块。是的,我知道我应该将其全部重构为一个方法。这就是不完全是我想要的。请看我对这个问题的评论。我只想在当前行的范围内做。我会检查一下,看看我是否可以使用它。谢谢!有点奇怪,但这正是我想要的。因此我研究了安装Blockle,偶然发现Blockle作者推荐安装它所有
splitjoin
改为:
gg/each\s*{<enter>qqf{%send<esc><c-o>sdo<esc>nq200@q
gg               " move cursor to top
/each\s*{<enter> " search pattern we want
qq               " start recording macro to register q
f{               " move to {
%send<esc>       " move to closing {, and change it to "end", back to normal
<c-o>sdo         " back to beginning { and change it into "do"
<esc>nq          " back to normal, and go to next match, stop recording