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.

asked Nov 26 '15 at 8:27

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?

    Nov 26 '15 at 8:33

  • 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.

    Nov 26 '15 at 8:45

  • @Creperum , your approach won't work if he has lines like this String s = "//my string";.

    Nov 26 '15 at 9:18

  • 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).

    Nov 26 '15 at 9:36

  • to solve the problem presented by @mathematician one could update the regex to: \s//.*

    Nov 26 '15 at 9:37

3 Answers 3

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.

answered Nov 26 '15 at 9:40

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.

    Nov 26 '15 at 9:42

  • also the regex is wrong - since it would not remove all white spaces before //. \s//.* would do the trick.

    Nov 26 '15 at 9:47

  • 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).

    Nov 26 '15 at 9:47

  • 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.

    Nov 26 '15 at 9:49

  • 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?//.*

    Nov 26 '15 at 10:00

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

answered Feb 18 at 8:35

Select all lines you want comment/uncomment and use through menu

Code -> comment with line comment

or

Code -> comment with block comment

answered Nov 26 '15 at 8:28

2

  • sorry, i don't want to turn a line or block into comment but delete the already out commented line. But only the ones that are out commented wit //

    Nov 26 '15 at 8:41

  • This option will comment a no commented line and will uncomment a commented line, it work in both ways. For delete the lines from file it will not work

    Nov 26 '15 at 8:42

Not the answer you're looking for? Browse other questions tagged java intellij-idea ide or ask your own question.