5

If I have a regex in a recorded macro and then run it over a line that the regex fails to match the macro stops. For example, I have a list of files.

family-reunion-2015.webm
road-festival-20150607.webm
ireland.webm
bike-festival-201407.webm
bike-festival-201409.webm

Which I want to convert to:

<a href="family-reunion-2015.webm">
family reunion 2015</a>
<a href="road-festival-20150607.webm">
road festival 20150607</a>
<a href="ireland.webm">
ireland</a>
<a href="bike-festival-201407.webm">
bike festival 201407</a>
<a href="bike-festival-201409.webm">
bike festival 201409</a>

I record the following macro inside the q register:

qq

yyp:s/\-/ /g<ENTER>A<Esc>xxxxxi</a><Esc>kI<a href="<Esc>A"><Esc>JJ0

q

I start from the first character of the first line and finish at the first character of the next line, i.e. at the end of the macro i move to the beginning of the next line (JJ0).

Then, I repeat the macro by typing 4@q. But i end with the following:

<a href="family-reunion-2015.webm">
family reunion 2015</a>
<a href="road-festival-20150607.webm">
road festival 20150607</a>
ireland.webm
ireland.webm
bike-festival-201407.webm
bike-festival-201409.webm

And an error message:

E486: Pattern not found: \-

Which is correct, but it stops the macro from advancing.

How can i tell vim to ignore this error and continue the macro processing?


I'm getting around this by doing :%s/webm$/we-bm/ and adding an extra x to my macro. But there should be a more elegant way.

9

Use the /e flag. From :help s_flag

[e]     When the search pattern fails, do not issue an error message and, in
    particular, continue in maps as if no error occurred.  This is most
    useful to prevent the "No match" error from breaking a mapping.  Vim
    does not suppress the following error messages, however:
        Regular expressions can't be delimited by letters
        \ should be followed by /, ? or &
        No previous substitute regular expression
        Trailing characters
        Interrupted
    {not in Vi}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.