Want to convert

 abc_def      ...

 thank_you123 ...


 .abc_def ( abc_def ),   

 .thank_you123 ( thank_you123 ),

with single substitute command.
Need to avoid both empty lines and comment lines.
Comment line starts with //.
abc_def can be a-z, A-Z, 0-9 and _.

2 Answers 2


you can use a :g command:

:v#^\s*//#s/\w\+/.&( & ),/g

where :v will execute the following command on all lines not matching the pattern. So in this case, this will execute the :s command on all non-comment lines.

  • Hi Christian, sorry for my previous message. I was wrong about the comment type. But I believe your solution don't replace line like abc_def // my comment. Apr 20, 2022 at 12:10
  • yes that is true, but could be easily fixed by using: :v#^\s*//#s... :) Apr 20, 2022 at 13:24
  • But this one convert the instance in the comment abc_def // hij_def devient .abc_def( abd_def ), // .hij_def( hij_def ), . Apr 20, 2022 at 13:51
  • 2
    yes, but that was not asked :) Apr 20, 2022 at 14:18

I propose you:

:%s#\(//.*\)\@<![a-zA-Z0-9_]\+#.& ( & ),#g


:%s/[a-zA-Z0-9_]\+/.& ( & ),/

Replaces abc_def by .abc_def( abc_def ),

The g flag make sure the replacement is done multiple times in the line if necessary.

The \(\/\/.*\)\@<! make the replacement only if the string is not preceded by //.

To avoid the ugly \/\/ we can use # instead of the standard / in the substitute command.

An alternative using very magic is:

:%s#\v(//.*)@<![a-zA-Z0-9_]+#.& ( & ),#g

  • I'm not sure the _def in the pattern is correct (see the second example)
    – D. Ben Knoble
    Apr 20, 2022 at 14:19
  • I miss understood the demand that was formulated slightly differently :-) Apr 20, 2022 at 14:30

Your Answer

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

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