2013-03-16 62 views
6

有沒有人在Vim中有一個插件或宏來替換匹配的{大括號}doend?最好把一個單行的語句是這樣的:在Vim(Ruby)中用do/end替換{大括號}

foo.each { |f| f.whatever } 

到:

foo.each do |f| 
    f.whatever 
end 

我可以做一個宏自己的那一個情況,但我想的東西,也可以處理轉換現有的多-line,潛在地複雜塊,如:

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

成:

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

我看過vim-surroundrails.vim,並沒有找到任何一種方法。

+0

對ruby不太瞭解。但爲什麼你的多行示例(輸出)只有外部'{,}'被替換,內部'f.bars.each {...'不是? – Kent 2013-03-16 02:46:40

+0

支持兩者都很好,但通常我不想要任何東西,只能一次擴展一個級別。我的方案是我更喜歡單行語句的內嵌塊,但經常發現自己想要添加另外一個或兩個語句,並且我想擴展該塊。是的,我知道我應該將其全部重構爲一種方法。 – 2013-03-16 03:26:54

回答

7

有一個名爲Vim Blockle的Vim插件執行此功能。

一旦你安裝了插件,你穿上{}doend光標,按<Leader>b交換塊樣式。

+0

謝謝!有點古怪,但這幾乎是我所期待的。 – 2013-03-16 04:43:29

+1

所以我研究了安裝Blockle,並偶然發現Blockle作者推薦安裝'splitjoin':https://github.com/jgdavey/vim-blockle/issues/5 – 2016-01-13 10:39:18

0

我認爲你多行例如,輸出將是:

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

也就是說,f.bars.each{...}也應該被更換。

,如果它是我們的目標,試試這個:

gg/each\s*{<enter>qqf{%send<esc><c-o>sdo<esc>[email protected] 

簡短說明:

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 

,那麼你可以例如[email protected]做和檢查結果。

+0

這並不完全是我所追求的。看到我對這個問題的評論。我只想爲當前行的範圍做這件事。不過,我會檢查一下,看看我是否可以使用它。 – 2013-03-16 03:27:59