Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode: delete all comments in a file

Tags:

Is there an easy way to delete all comments from an open file in VSCode? Preferably both line and block comments.

Most interested in Java, but also Python and R.

like image 211
Igor Avatar asked May 29 '18 00:05

Igor


People also ask

How do I select all comments in Vscode?

Type CTRL + F and type //. * and select the regex sign like the below picture. Now you can see all the commented lines are selected.

How do I delete multiple comments in Visual Studio?

Ctrl + K , Ctrl + U . There is also a button for it on the Standard toolbar. Show activity on this post. ctrl + / can be used for adding and removing comments.

How do I delete comments in Visual Studio?

If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.


1 Answers

Easy way:

  • Open extensions (ctrl-shift-x)
  • type in remove comments in the search box.
  • Install the top pick and read instructions.

Hard way:

  • search replace(ctrl-h)
  • toggle regex on (alt-r).
  • Learn some regular expressions! https://docs.rs/regex/0.2.5/regex/#syntax

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.

like image 176
ippi Avatar answered Nov 15 '22 11:11

ippi