首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VIM -宏中的多个搜索破坏了预期的逻辑

VIM -宏中的多个搜索破坏了预期的逻辑
EN

Stack Overflow用户
提问于 2013-10-30 18:40:17
回答 1查看 95关注 0票数 1

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

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

== ISSUE ==

我的目标是压缩每个文本块,这样除了每个文本块之间就没有空行。我还想将== ISSUE ==字符串更改为它下面的字符串,其中的每一侧都有==。最后,每条消息的正文应该包装在<pre></pre>标记中。因此,下面的示例如下:

代码语言:javascript
复制
== ISSUE ==

Reactor leak in dilithium chamber

Personel evacuation started.

1

1

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

应成为:

代码语言:javascript
复制
== Reactor leak in dilithium chamber ==
<pre>
Personel evacuation started.
1
1
</pre>

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

我在VIM中使用了一个简单的宏:

代码语言:javascript
复制
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宏中使用更明确的搜索来避免这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-30 19:46:44

在这种情况下,由于缺少end搜索参数,因此可以更容易地以另一种方式工作。

简单地说,

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

命令

代码语言:javascript
复制
: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

特殊字符

代码语言:javascript
复制
^[                        ' Escape
^M                        ' Enter

结果

代码语言:javascript
复制
== Reactor leak in dilithium chamber ==
<pre>
Personel evacuation started.
1
1
</pre>
== Unathorized shuttle access. ==
<pre>
</pre>
== No problems reported. ==
<pre>
</pre>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19691225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档