0

I've been trying to write a regex that turns the following structure

# Ignore this line

Foo
Foo

# Ignore this line too

into:

# Ignore this line

IF BAR THEN
    Foo
    Foo

# Ignore this line too

So far I have the following regex:

%s#\(\n\)\?\(\(Foo\n\)\+\)\(\n\)\?#\= submatch(1) . 'IF BAR THEN\r' . 
substitute(submatch(2), 'Foo', '    &', 'g') . submatch(4)#gc

Which gives me:

# Ignore this line

IF BAR THEN\r    FOO
    Foo

# Ignore this line

I have tried using \n, <CR>, ^M but none seem to have the desired output. Please advise!

1 Answer 1

1

The problem is that you're using a literal string (delimited by single quotes) where you want to use an escape character \r.

Replace them with double quotes to fix:

... "IF BAR THEN\r" ...

(See :help string.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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