2013-03-15 20 views
1

我做了一個簡單的宏在JSON對象像這樣增加了一些:應用Vim的宏多行

{ 
    image: 'images/2.jpg', 
    thumb: 'images/2-thumb.jpg', 
    big: 'images/2.jpg', 
    title: '', 
    description: '', 
    link: 'images/2.jpg' 
}, 

有:

q, n, shift-v, down-till-end, p, move-to-numbers, c-a, return-to-top, q, [email protected] 

(很抱歉,如果這不是適當的語法在SE發佈vim宏)

它可以工作,但它會使增量直到第9個。我錯過了什麼?

在此先感謝。

編輯:

我想達到這樣的事:

{ 
    image: 'images/3.jpg', 
    thumb: 'images/3-thumb.jpg', 
    big: 'images/3.jpg', 
    title: '', 
    description: '', 
    link: 'images/3.jpg' 
}, 
{ 
    image: 'images/4.jpg', 
    thumb: 'images/4-thumb.jpg', 
    big: 'images/4.jpg', 
    title: '', 
    description: '', 
    link: 'images/4.jpg' 
}, 
... until *nth* value 
+1

我不明白你在做什麼?你只是想在文件的每一行增加一個數字?':%norm^A'('^ A'由ctrl-q ctrl-a創建) – 2013-03-15 20:24:17

+0

我想我誤解了你的問題。你可以提供寄存器'n'的值,你可以看到它按下' n'而插入模式 – rbernabe 2013-03-15 20:24:39

+0

我試圖「複製」一個JSON對象,但在圖像上有一個「+1」值,像這樣:foo1.jpg,foo2.jpg,foo3.jpg,foo4.jpg – ramonovski 2013-03-15 20:34:41

回答

4

假設你的光標在第一開口支架,這裏是做這件事:

qn     " start recording in register n 
V%     " select from here to the closing bracket, linewise 
y      " yank the selection 
%      " jump to the closing bracket 
p      " paste after the current line 
:'[,']norm <C-v><C-a> " executing the normal mode command <C-a>(1) on all the lines that we just pasted 
q      " stop recording 

然後做[email protected]

(1)<C-v><C-a>用於插入文字^A

+1

這與我想出的幾乎相同。但是我們會在哪裏打出一些vim高爾夫?你可以放下''['將其縮短2個字符。 – 2013-03-15 20:55:21

+0

我的答案看起來像一個誇張的剽竊版本的評論。在趕到答案表之前,我不應該跳過評論! – romainl 2013-03-15 20:59:30

+0

我很高興你做到了,因爲你的宏比我的矮:''qn8yy'] p:,''norm^Q^A^M [q'' – 2013-03-15 21:04:11

1

試試這個:

進入可視化模式,並選擇線路被列入宏執行一種類型:

:normal @n 

然後,當你按下回車鍵,宏將被應用到選定的行

0

我給它一個嘗試:

qqv%:s/\d\+/\=submatch(0)+1/^M[[yGGp 

簡短說明

qq        "recording to register q 
v%        "select things between { and } 
:s/\d\+/\=submatch(0)+1/^M  "just do +1 to all numbers (selected range) 
[[        "back to begin { 
yG        "yank till the end 
Gp        "paste at the end 

然後做[email protected]

,如果你僅僅通過Enter

如果錄製相同的宏,類型^M您將宏分配給@q^M通過<c-v><enter>

順便說一句,這不會贏得高爾夫,因爲函數名submatch(0)太長... :)

+0

謝謝你,肯特,你的解決方案也工作了,我學到了submatch()函數。 – ramonovski 2013-03-15 22:27:58

0

用我UnconditionalPaste plugin,一旦你猛拉原始塊到寄存器中,你可以粘貼[N]自動遞增塊,只需[N]gPp粘貼,所有數字遞增)。

該插件還允許對文本粘貼的方式進行其他操作。