1

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

我的示例输入由文本块、空换行符等组成。每个块都以以下确切行开头:

== ISSUE ==

我的目标是压缩每个文本块,以便在每个文本块之间没有空行。我还想将== ISSUE ==字符串更改为它正下方的字符串,在它==的每一侧。最后,每条消息的正文都应该包含在<pre>and</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 宏中进行更明确的搜索以避免这个问题?

4

1 回答 1

1

在这种情况下,由于缺少 end search 参数,反过来工作会更容易。

简而言之

  • 首先删除所有空行
  • 转到文件底部
  • 启动宏,编辑和向后搜索
  • 停止录制
  • 重复

命令

: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>
于 2013-10-30T19:46:44.560 回答