Search code examples
vimviml

Exiting exe mode in a macro


I had a large file I was trying to reformat which involved removing the 2nd to nth repeating sets on 2 to 100 lines per duplicate.

The data looked like

element1.element2.element...field.comment

I wanted to remove the repetition in elements after the first instance so of course I went complicated :) and did a macro something like

In a macro Yanked first element on current line to register p and then processed lines yanking the first element into register o and then doing, still in the macro

:if (@p=!@o)|:.s/paste register p//g|else|:norm! j|endif

Now this worked OK except when it got to a line where @p<>@o the :norm! j part stayed in : mode until I manually escaped once or twice then executed the :norm! j command.

I solved the problem an easier way but would like to know why it was only on the else portion that it wouldn't leave :ex mode.


Solution

  • From :help norm

    :norm[al][!] {commands}                 *:norm* *:normal*
            ...
    
            This command cannot be followed by another command,
            since any '|' is considered part of the command.
    
            ...
    
            An alternative is to use |:execute|, which uses an
            expression as argument.  This allows the use of
            printable characters to represent special characters.
    
            Example: >
                :exe "normal \<c-w>\<c-w>"
    

    So this would do the trick:

    :if (@p=!@o)|:.s/paste register p//g|else|:exe "norm j"|endif