我有一个简单的宏,我正在用VIM将一个文本块转换成特定的Media格式,并试图让它工作。
我的示例输入由文本块、空换行符等组成。每个块以以下确切的行开始:
== 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宏中使用更明确的搜索来避免这个问题?
发布于 2013-10-30 19:46:44
在这种情况下,由于缺少end搜索参数,因此可以更容易地以另一种方式工作。
简单地说,是
命令
: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>https://stackoverflow.com/questions/19691225
复制相似问题