Madhu
Madhu

Reputation: 61

Multiline regex pattern to delete blank lines

/* Comments for code... */

if (...) {

}

I need to delete the blank line between the comment and the if:

/* Comments for code... */
if (...) {

}

I am currently using the following regex:

/\*\/\ze\n^$\n[ ]*if

As I use \ze, cursor finally points to */. How should I do?

Upvotes: 0

Views: 322

Answers (4)

Alan Gómez
Alan Gómez

Reputation: 378

The following regex can achieve this:

:%s/\(\/*\)\(\n\+\)\(if\)/\1\r\3

The \(\/*\) match the comment line

\(\n\+\) match any empty lines below till

\(if\) that match if line

Upvotes: 0

Kent
Kent

Reputation: 195059

try this line:

%s#\*/[\s\r\n]*#*/\r#

it will make

/* Comments for code... */





if (...) {

}
/* Comments for code... */






else{


}

into:

/* Comments for code... */
if (...) {

}
/* Comments for code... */
else{


}

Upvotes: 2

romainl
romainl

Reputation: 196546

:g+*/+j

is much quicker but probably too broad.

You could do something like the following:

:g+*/\_\s*if+j

Upvotes: 1

mihai
mihai

Reputation: 38543

Why not use \zs as well.

This worked for me:

:%s/\*\/\zs\n*[ ]*\zeif/\r/g

Explaination:

%s - substitution on the entire file
\*\/ - end of comment
\zs - start of match
\n*[ ]* - eol and spaces
\ze - end of match
if - followed by if
/\n/ - replacement
g - global regex (multiline)

Upvotes: 1

Related Questions