[英]Replacing matching { braces } with do/end in Vim (Ruby)
Does anyone have a plugin or macro to replace matching {
braces }
with do
and end
in Vim? 有没有人有一个插件或宏来替换匹配的
{
braces }
与do
并在Vim中end
? Preferably turning a single-line statement like this: 最好像这样转换单行语句:
foo.each { |f| f.whatever }
into: 成:
foo.each do |f|
f.whatever
end
I could make a macro myself for that one case, but I'd like something that could also handle converting existing multi-line, potentially complicated blocks, like: 我可以为这一个案例制作一个宏,但是我想要一些可以处理转换现有的多行,可能很复杂的块的东西,比如:
foo.each { |f|
f.bars.each { |b| b.whatever }
hash = { a: 123, b: 456 }
}
into: 成:
foo.each do |f|
f.bars.each { |b| b.whatever }
hash = { a: 123, b: 456 }
end
I've looked at vim-surround and rails.vim , and haven't found a way with either. 我看过vim-surround和rails.vim ,还没找到方法。
There is a Vim plugin called Vim Blockle that performs this function. 有一个名为Vim Blockle的Vim插件可以执行此功能。
Once you install the plugin you put the cursor on the {
}
do
or end
and press <Leader>b
to swap the block styles. 安装插件后,将光标放在
{
}
do
或end
,然后按<Leader>b
交换块样式。
I assume that in your multi-line example, the output would be : 我假设在你的多行示例中,输出将是:
foo.each do |f|
f.bars.each do |b| b.whatever end
hash = { a: 123, b: 456 }
end
that is, the f.bars.each{...}
should be replaced too. 也就是说,
f.bars.each{...}
应该被替换。
if it is the goal, try this: 如果是目标,试试这个:
gg/each\s*{<enter>qqf{%send<esc><c-o>sdo<esc>nq200@q
short explanation: 简短说明:
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
then you could do for example 200@q
and check the result. 然后你可以做例如
200@q
并检查结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.