2013-10-30 18 views
1

我有一個簡單的宏,我正在VIM中將一段文本轉換爲特定的Media-Wiki格式,並試圖使其工作。VIM - 宏中的多重搜索意向邏輯

我的樣品輸入包括文本,空換行符等的塊中的每塊從下面確切的行:

== ISSUE ==

我的目標是使得沒有壓實文本的每個塊空行在每個文本塊之間保存一行。我還想將== ISSUE ==字符串更改爲其正下方的字符串,並在其兩側各有==。最後,每封郵件的正文應該包裝在<pre></pre>標籤中。所以,下面的示例:

== ISSUE == 

Reactor leak in dilithium chamber 

Personel evacuation started. 

1 

1 

== ISSUE == 
Unathorized shuttle access. 
== ISSUE == 
No problems reported. 

應該改爲:

== Reactor leak in dilithium chamber == 
<pre> 
Personel evacuation started. 
1 
1 
</pre> 

== Unathorized shuttle access == 
<pre> 
</pre> 
== No problems reported == 
<pre> 
</pre> 

我用VIM這個簡單的宏:

qa     ' Start recording macro "a" 
/== ISSUE ==   ' Find first instance of delimiter 
dd     ' Delete the line 
j     ' Go one line down 
0i==[SPACE]   ' Prefix the line with "== " 
[ESC]$a[SPACE]==  ' Append " ==" to the end of the line 
o     ' Start new line below it 
<pre>    ' Enter the arbitrary tag while still in insert mode 
[ESC]    ' Enter normal mode 
V     ' Enter block selection mode 
/== ISSUE ==   ' Find next delimiting block 
k     ' Move cursor up one line, so the new delimiter is excluded from search 
:g/^$/d    ' Delete all empty lines between the two delimiters 
O     ' Insert a new line above the second delimiter 
</pre>    ' Insert the second arbitrary tag 
q     ' Stop macro recording 

它幾乎工作,但它似乎打破時我第二次嘗試。通過打開搜索突出顯示,似乎在宏中有多個搜索(即:搜索== ISSUE ==和刪除空行查詢)會導致衝突,儘管我在我的宏中顯式鍵入了搜索查詢。有沒有辦法更加明確地在我的VIM宏中搜索以避免這個問題?

回答

1

在這種情況下,由於缺少結束搜索參數,因此使用其他方式工作將更容易。

簡而言之

  • 首先刪除所有空行
  • 轉到文件
  • 的底部開始的宏,編輯和向後搜索
  • 停止錄製
  • 重複

命令

:g/^$/d     ' Delete all empty lines 
G       ' go to the bottom of the file 
qq      ' start recording the macro in register q 
o</pre>^[?== ISSUE ==^Mddi== ^[A ==^M<pre>^[kk 
@q      ' repeat the macro 

特殊字符

^[      ' Escape 
^M      ' Enter 

結果

== Reactor leak in dilithium chamber == 
<pre> 
Personel evacuation started. 
1 
1 
</pre> 
== Unathorized shuttle access. == 
<pre> 
</pre> 
== No problems reported. == 
<pre> 
</pre> 
+0

優秀的解釋。謝謝!有效。 – DevNull

+0

很高興它的工作,但我不禁想知道有一個更簡單的方法。我認爲@IngoKarkat ... –

+0

..和@Romainl都是沒有回答的睡眠:) –