25

Notepad++ obviously recognizes all comments as such. Is there a way to simply delete all?

Edit: Stat-R's bookmark method has helped greatly, not only for removing comments but for conditionally removing lines in general.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Cos
  • 1,649
  • 1
  • 27
  • 50

9 Answers9

39

For a general file, first of all you need to know the comment operator of the language you are writing the file in. For example, in java script the comment operator is //. For the following code...

enter image description here

In NP++, you need to

Mark the lines that contains '//'. Make sure the bookmark option is enabled.

enter image description here

Then, choose from NP++ menu Search>Bookmark>Remove Bookmarked lines

enter image description here


EDIT: Another solution after @Chris Mirno 's suggestion is as follows: Use regular expression. See the image below. It is self explanatory enter image description here

To understand it better, refer to these

Community
  • 1
  • 1
Stat-R
  • 5,040
  • 8
  • 42
  • 68
25

enter image description here

In the Find & Replace Dialog, put the following regex and adjust the search options as depicted.

/\*.*?\*/

Replace with: (empty)

Select Mode: Regular Expression AND .(dot) matches newline

This should remove all your C style comments spanned across lines.

SenG
  • 713
  • 1
  • 7
  • 15
  • This doesn't appear to work if the comment start (/*) is on a different line than its end (*/). – userx Jan 20 '15 at 22:49
  • 2
    My bad! I forgot to mention .(dot) matches newline option should also be selected. Edited the answer to correct this. Kindly note multi-line support for regex was recently introduced in Notepad++ so ensure that you have latest version installed – SenG Jan 24 '15 at 11:43
  • FYI *? matches the previous token between zero and unlimited times, as few times – Joniale Apr 29 '22 at 07:57
11

Star-R and Chris Mirno Answer are also Correct and Good.

But For Line Comment:

//.*?(?=\r?$)

Explanation:

// will be the Starting Position

.*? Will be any character

(?=\r?$) will search to the end of the line (as it is required in line comment)

Note: But Still check each of the line because for example if your code contains soap format like

//www.w3.org/2001/XMLSchema-instance\x2......");

it will capture this line because the starting is // and it goes to end of the line so watch out for this :)

Jamil
  • 5,457
  • 4
  • 26
  • 29
  • 2
    This was the best answer for me at least, however, it still had a flaw catching things like `url: http://some-link/`. To fix this simply use: `^//.*?(?=\r?$)` regular expression. This will make sure that only true comment lines get marked. – dev101 Mar 08 '16 at 06:38
9

Warning to all using Stat-R's solution:
This method will remove lines of code if formatted like this:

echo "hello"; //This comment will be detected

Following his method, the entire line will be removed. Therefore make sure to go through and make these comments, their own line before doing this method.

Chris Mirno
  • 337
  • 5
  • 13
  • You could also use regex to search, so turn on the regex functionality and add ^ in front of the comment code in order to search for the beginning of the line. Your resulting expression will be something like ^\% (\ escapes the % character). – pypmannetjies Jan 24 '14 at 11:33
  • For Line Comment Check my Answer – Jamil Oct 02 '15 at 09:31
  • so, for the better standard, should not place comment behind the statement :) – Weijing Jay Lin Mar 12 '16 at 11:11
2

I have had some luck running a macro for the above. Basically:

  • search for // (F3)
  • select to end of line (shift+end)
  • delete (delete)

Put // into the search dialog by just searching for it once. Then record the three steps in a macro, then play it back until EOF.

The first time I did it I had a problem, but then it worked, not sure what I did differently.

Florent
  • 12,310
  • 10
  • 49
  • 58
2

Anton Largiader's answer was the most reliable one, including complex inline comments.

However, it will leave many empty lines, including ones with empty characters (space, tabs...) so I would just add another step to make it almost perfect:

After running the macro, just do:

Edit > Line Operations > Remove Empty Lines

OR

Edit > Line Operations > Remove Empty Lines (Containing Blank Characters)

1st option is good if you wish to remove only really empty lines 2nd options will remove every empty line even containing space etc. so there will be no more actual spacing left between code blocks. 1st option might be the safest with some manual cleanup afterwards.

dev101
  • 1,359
  • 2
  • 18
  • 32
0

As someone suggested in another post, the simplest and most reliable is maybe to export the all text in .RTF format using Menu Plugin-->NppExport-->Export to RTF and then:

-Open the newly created file in Word

-Select any part of any comment

-On the top-right side of Word clic Select--> Select all texts with similar formatting

-Remove the selected comments all at once (del or cut if doesn't work)

  • Maybe this works, but you should definitely avoid using Microsoft Word or any other rich text editor for code editing. – dev101 Dec 17 '17 at 16:26
0

To remove Powershell comments if someone find it handy:

Removing Comment in a Powershell using Notepad ++ To find just lines beginning with # (and not with # elsewhere in the line).

  1. Notepad++ SEARCH Menu > Find
  2. ‘Mark‘ Tab – fill in as below.
  3. Select ‘Mark All’ (clear all marks if used previously).

Regex ^[#}

enter image description here

  1. SEARCH Menu > bookmark > Remove (or do anything on the list with them)
  2. Clear all marks to reset

You can select no comments just code by doing the following:

Regex ^[^#}

enter image description here

Pete
  • 91
  • 5
-4

Enter ctrl+shift+K to remove comment

punit
  • 5
  • 1
  • 5
    I believe this removes the comment, `//`, but does not delete the actual line (and all comments) as the OP asked. – ahren Nov 09 '13 at 11:52