Question
VSCode: delete all comments in a file
Is there an easy way to delete all Java-style & C-style comments from an open file in VSCode? Preferably both line and block comments.
Question
Is there an easy way to delete all Java-style & C-style comments from an open file in VSCode? Preferably both line and block comments.
Solution
remove comments
in the search box.A simple //.*
will match all single line comments (and more ;D). #.*
could be used to match python comments. And /\*[\s\S\n]*\*/
matches block comments. And you can combine them as well: //.*|/\*[\s\S\n]*\*/
(|
in regex means "or", .
means any character, *
means "0 or more" and indicates how many characters to match, therefore .*
means all characters until the end of the line (or until the next matching rule))
Of course with caveats, such as urls (https://...
) has double slashes and will match that first rule, and god knows where there are #
in code that will match that python-rule. So some reading/adjusting has to be done!
Once you start fiddling with your regexes it can take a lifetime to get them perfect, so be careful and go the easy route if you are short on time, but knowing some simple regex by heart will do you good, since regular expressions are usable almost everywhere.
Solution
This will be an easy way. Type CTRL + F and type //.*
and select the regex sign like the below picture.
Now you can see all the commented lines are selected. Replace those with space. Replace can enable click with > sign in the above picture.
for the second text field enter a space and press enter. It will remove commented lines one by one. By giving a long press you can delete all commented lines.