【问题标题】:VIM - Multiple Searches Within Macro Breaks Intended LogicVIM - 宏内的多次搜索破坏了预期的逻辑
【发布时间】:2013-11-10 13:20:55
【问题描述】:

我有一个简单的宏,我正在 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 == 和 delete-empty-lines 查询)会导致冲突,尽管我在宏中明确键入了搜索查询。有没有办法在我的 VIM 宏中进行更明确的搜索以避免这个问题?

【问题讨论】:

    标签: regex vim vi


    【解决方案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>
    

    【讨论】:

    • 很高兴它成功了,但我不禁想知道有一个更简单的方法。我假设@IngoKarkat ...
    • .. 和 @Romainl 都因为还没有回答而睡着了 :)
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2011-01-07
    • 2012-03-14
    • 2017-02-02
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多