How to delete all comment lines in IDEA?
Asked Answered
W

5

16

I´m searching for a quick way to get rid of all comments in a file.

Looking for a keyboard shortcut or a quick and easy alternative.

Any suggestions?

EDIT

I would like particularly to delete only the out commented lines, so ones that start with //, not the block comments - which normally are meant for java-doc.

Wanhsien answered 26/11, 2015 at 8:27 Comment(7)
You want to delete your comments. As in remove them? As in, the lines that explain the code, you don't want near your code? If that's the case, you can use find and replace on //[.]* and replace with "". Then run formatting to tidy up any gaps. But... is this really what you want?Dorisdorisa
yes, yes, yes and yes. But particularly only the ones which start with // so not block comments. Your regex would do the trick as a workaround, but i hoped there will be a dedicated function for.Wanhsien
A rather odd feature to want if you ask me. That would remove ALL single line comments - including the ones that actually have a purpose, to document code. Comments are not second-class citizens that need to be cleaned up, they are an important part of the code base. I would find it quite odd that a dedicated feature to remove them would exist.Sherlock
@Creperum , your approach won't work if he has lines like this String s = "//my string";.Sower
It depends. If you know the class, and can ensure that all meaningful comments are written as block comments and all line comments are temporarily code snippets - it might be quite usefullWanhsien
My solution wasn't well thought through, @mathematician you're right that it will break on a number of lines. I was mainly trying to indicate that find and replace could do it, but you'll have to tweak your regex. Also, you probably don't want to hit replace all otherwise meaningful comments would be lost. Another idea is you could create an inspection (not sure how, looking into this myself).Dorisdorisa
to solve the problem presented by @mathematician one could update the regex to: \s//.*Wanhsien
B
29

You could use Find & Replace (Ctrl/Cmd+R). Search In Comments Only (option under the little cog menu), enable regex search and search for string ^//.*. Replace with the empty string.

Boarer answered 26/11, 2015 at 9:40 Comment(7)
Thanks for the respond, since its the same solution already presented in the comments and not adding a keyboard shortcut i`m not sure if i can mark it as a solution. Maybe you could add the fact, that there is a String Manipulation command "Remove empty lines" which could work with keyboard shortcut like (Ctrl+Cmd+R) - which would do the trick in 2 steps.Wanhsien
also the regex is wrong - since it would not remove all white spaces before //. \s//.* would do the trick.Wanhsien
It is slightly different:) But if it doesn't solve your problem, don't mark it as a solution. You could give it a vote (or not).Boarer
Whitespaces before the // would be removed by the Strip trailing spaces on save option, if you have that enabled. \s//.* would match nothing if you search In Comments Only.Boarer
true again. But to make live easier, since comments normally do no start at the very first letter, i would probably update the regex to: \s?//.*Wanhsien
In that case you may even want to go to \s*//.* to remove any empty lines, but watch out for // inside of string literals.Boarer
@BasLeijdekkers Nice answer. Note to others, since you're already searching in comments only, to really delete them all including the comments that don't start with //, just use the regex .*.Parada
S
3

if you are using android studio 4 above

keyboardShortcut
Control + R - Replace

Type -->

 \s//.*  

Select .* in right top of replace window . then it will select all comment like this --> //

Thank you @Bas Leijdekkers

Sievert answered 18/2, 2021 at 8:35 Comment(0)
T
0

First to open the replace tab use Ctrl/Cmd+R don't forget to enable regex search

  1. Replace all multi-line comments with empty /\*(\*)?(((?!\*/)[\s\S])+)?\*/
  2. Replace all single-line comments with empty ^(//.*)$ or use //.*$ if you don't have links in your code
Tawnyatawsha answered 24/7, 2022 at 7:36 Comment(0)
G
0

In addition to the awesome answers which work using the Find & Replace module for IDEA Jetbrains, you could also use a shell command since the terminal is readily available within the Jetbrains products for Linux and Mac systems. the good old sed command is super handy. Additionally this is useful for repeating the action for many files. The commands slightly differ for Mac and Linux. For Mac, you would need the extra '' as shown below so that backup files are not created.

For completeness, included the commands for python as well along with C and java. Linux command for C and Java,

sed -i '/^[[:space:]]*\/\//d' filename.java
sed -i '/^[[:space:]]*\/\//d' filename.C
sed -i '/^[[:space:]]*#/d' filename.py

As mentioned before, for Mac, extra '' are needed:

sed -i '' '/^[[:space:]]*\/\//d' filename.java
sed -i '' '/^[[:space:]]*\/\//d' filename.C
sed -i '' '/^[[:space:]]*#/d' filename.py
Greensand answered 27/4 at 6:16 Comment(0)
C
0

Open it in VSCode and install the extension "Remove Comments".

Search for the new command in the command palette with:

CTRL + SHIFT + P

Cluster answered 4/5 at 21:45 Comment(2)
The question is about IntelliJ - and not VS Code.Terisateriyaki
I can read @Abra, thanks. This is "a quick and easy alternative". The IDE is irrelevant in respect to the essence of the question. In this case, more options are better.Cluster

© 2022 - 2024 — McMap. All rights reserved.