1

\/\*.*\(\n\s\*.*\)*\n\s\*\/ is a pattern which I cooked up to match some multi-line C comments, i.e., in the format of:

/* Comment Text
 * Comment Text
 * Comment Text
 */

Searching for the pattern in question with hlsearch set highlights the entire comment, but when :%g/\/\*.*\(\n\s\*.*\)*\n\s\*\//delete is executed, it acts only on the first line of the comment. If you replace the 'delete' in the command with any other ex command, it acts similarly, influencing only the first line of the highlighted match (while typing the command, it highlights the entire comment). Why does this peculiar behavior happen? (I know this sounds like an XY problem, but my curiosity is not about my problem, but about why this solution returns this peculiar behavior).

1

I believe this is because the :g command doesn't operate on your search pattern directly, but rather on the line that the search pattern starts on. According the help (:help :g), the way the :g command works is by marking each line containing the match; for multi-line patterns, it only marks the first line. Then it goes through each of the marked lines and runs the specified command (delete in your case).

For your particular problem, I'd probably suggest using the :substitute command to replace the pattern with nothing. To avoid it leaving an empty line you could add an optional \n at the end of the match.

:%s/\/\*.*\(\n\s\*.*\)*\n\s\*\/\n\?//g

On an unrelated note: :g operates on the entire file by default, so you could have omitted the % from the beginning (it is needed for the :s command, though).

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.