Question

I have a simple macro that I am working on in VIM to convert a block of text into a specific Media-Wiki format, and am attempting to get it working.

My sample input consists of blocks of text, empty newlines, etc. Each block begins with the following exact line:

== ISSUE ==

My goal is to compact each block of text so that there are no empty lines save for one between each block of text. I also want to change the == ISSUE == string to be the string directly below it, with == on each side of it. Finally, the body of each message should be wrapped in the <pre> and </pre> tags. So, the following sample:

== ISSUE ==

Reactor leak in dilithium chamber

Personel evacuation started.

1

1

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

Should become:

== Reactor leak in dilithium chamber ==
<pre>
Personel evacuation started.
1
1
</pre>

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

I used a simple macro in VIM for this:

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

It almost works, but it seems to break when I attempt it a second time. By turning on search highlighting, it seems that having more than one search within the macro (ie: the search for == ISSUE == and the delete-empty-lines query) causes conflicts, despite my explicit typing of the search queries in my macro. Is there a way to be more explicit with searches in my VIM macros to avoid this issue?

Was it helpful?

Solution

In this case, because of the missing end search parameter, it would be easier to work the other way arround.

In a nutshell

  • First delete all empty lines
  • go to the bottom of the file
  • start a macro, edit and search backwards
  • stop recording
  • repeat

Commands

: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

Special characters

^[                        ' Escape
^M                        ' Enter

Result

== Reactor leak in dilithium chamber ==
<pre>
Personel evacuation started.
1
1
</pre>
== Unathorized shuttle access. ==
<pre>
</pre>
== No problems reported. ==
<pre>
</pre>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top