0
/* 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
  • \*//: end of comment (*/)
  • ^$: blank line before if
  • [ ]*if: spaces and an if

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

sp00m
  • 47,968
  • 31
  • 142
  • 252
Madhu
  • 61
  • 1
  • 9

4 Answers4

2

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{


}
Kent
  • 189,393
  • 32
  • 233
  • 301
1

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)
mihai
  • 37,072
  • 9
  • 60
  • 86
1
:g+*/+j

is much quicker but probably too broad.

You could do something like the following:

:g+*/\_\s*if+j
romainl
  • 186,200
  • 21
  • 280
  • 313
0

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

Alan Gómez
  • 211
  • 1
  • 6