VIM - 宏内的多个搜索打破了预期的逻辑

时间:2013-10-30 18:40:18

标签: regex vim vi

我有一个简单的宏,我正在VIM中工作,将一个文本块转换为特定的Media-Wiki格式,并试图让它工作。

我的示例输入包含文本块,空换行符等。每个块都以以下完全行开头:

== 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 个答案:

答案 0 :(得分: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>