109

I'm cleaning up some code files (C#) and want to remove the regions. And I would like to delete all the lines that have the string '#region'. That's just an example, and I can think of several more uses, but is that even possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rismo
  • 6,487
  • 11
  • 37
  • 33
  • 2
    I am tempted to vote this question to be off topic ... It's mainly a question on notepad++ and specific working pattern, but not necessarily programming related :/ –  Dec 09 '14 at 14:44
  • @AndreasNiedermair agree – Stefan Falk Dec 09 '14 at 14:54

8 Answers8

307

Notepad++ v6.5

  1. Search menu -> Find... -> Mark tab -> Find what: your search text, check Bookmark Line, then Mark All. This will bookmark all the lines with the search term, you'll see the blue circles in the margin.

  2. Then Search menu -> Bookmark -> Remove Bookmarked Lines. This will delete all the bookmarked lines.

You can also use a regex to search. This method won't result in a blank line like John's and will actually delete the line.

Older Versions

  1. Search menu -> Find... -> Find what: your search text, check Bookmark Line and click Find All.
  2. Then Search -> Bookmark -> Remove Bookmarked Lines
Ray
  • 45,695
  • 27
  • 126
  • 169
  • 9
    Thank you! Wow, I wasn't aware that NP++ did this.. a great time saver. Find & bookmark lines + delete lines. Awesome. Thank you!! – Matthew M. Dec 06 '10 at 16:26
  • what a hack, why cant i just regex replace \r\n with "" :( – felickz Oct 07 '11 at 12:51
  • Tremendous. This has many flexible applications, most obviously for me in being used to remove .svn directories from grep output. Helped me narrow down a very broad search from over 25k lines to just under 400 relevant results. Seriously... THANK YOU. – Slobaum Oct 19 '11 at 16:52
  • 15
    This is one of those little gems I find where i wish I could upvote more than 1. I can't tell you how much time I've wasted over the years never realizing this was available! Only problem is now I guess I need to be more productive... – Milner May 07 '12 at 15:42
  • 2
    If you're searching for this. 'Mark' has its own tab now on the Search popup menu. – bunnybare Oct 15 '13 at 21:24
  • 1
    @bunnybare, thanks. I updated the answer for the latest version. – Ray Oct 16 '13 at 23:15
  • Awesome! Wish I knew about that a long time ago. The Npp Edit / Search menu is full of wonders. – Andreas Nov 17 '15 at 09:06
  • One of those occasions you realize how lame it is to take a look only at the "accepted" answer. Shame on you, OP! – OmarOthman Jan 02 '16 at 08:38
  • @Ray Awesome identify/delete sequence. I am leading a small number of people that are not actually programmers. This sequence will allow them to quickly make sure that the bookmarked lines are the ones they want to remove from the file (SCORM 2004 manifests that have nearly 750K of xml lines). – Energetic Pixels Oct 16 '16 at 22:11
40

You can use menu Search -> Replace... (Ctrl + H).

It has a regular expression feature for replacing. You can use a regex that matches #region as well as whatever else is on the line, and replace it with empty space.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John T
  • 23,735
  • 11
  • 56
  • 82
  • Thanks, I did that and it works. But now I'm trying to replace \n\n with just one \n but doesn't seem to work, any ideas? – Rismo May 27 '09 at 21:59
  • 1
    use extended search for replacing escape sequences (radio button just above regex) – John T May 27 '09 at 22:05
  • Thanks John. I did that and works for one line break \n, but not for two. I'm searching for \n\n and replacing it with \n, but it doesn't work. Does it work for you ? – Rismo May 27 '09 at 22:07
  • 1
    If you typed the document on windows, line endings will be \r\n. UNIX style line endings are \n. – John T May 27 '09 at 22:12
  • 5
    Also just noticed that TextFx -> TextFx Edit -> Delete Surplus Blank Lines does the same :) – Rismo May 27 '09 at 22:25
  • Thank god for plugins! As you can tell I'm a stubborn programmer who likes doing things the old fashioned and most programmatic way possible. – John T May 27 '09 at 22:49
  • @Rismo/John T I tend to convert all linebreaks to \n (Edit -> EOL Conversion -> Unix Format) before doing stuff like this. – Stewart Mar 16 '11 at 17:15
8

This is the most common feature of Notepad++ that I use to update my code.

All you need to do is:

  • Select common string that is present in all lines.
  • Press Ctrl + F
  • In the Mark tab, paste the recurring string and check the Bookmark line checkbox.
  • Click on Mark All
  • Now go to menu SearchBookmarkRemove Bookmarked Lines

You can refer to this link for pictorial explanation.

http://www.downloadorinstall.com/best-notepad-tips-and-tricks-for-faster-work-and-development/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rishabh Pant
  • 81
  • 1
  • 1
5

Here is a way that removes the lines containing "YOURTEXT" completely:

  • Open the Replace dialog
  • Enter the following search string: .*YOURTEXT.*[\r]?[\n] (replace YOURTEXT with your text)
  • Enable "Regular expression"
  • Disable ". matches newline"

The given regular expression matches both Windows and Unix end of lines.

If your text contains characters that have a special meaning for regular expression, like the backslash, you will need to escape them.

ocroquette
  • 3,049
  • 1
  • 23
  • 26
2

Jacob's reply to John T works perfectly to delete the whole line, and you can Find in Files with that. Make sure to check "Regular expression" at bottom.

Solution: ^.*#region.*$

Nick
  • 21
  • 2
1

Investigate what is your EOL, \n or \r\n. Then replace .*#region.*\r\n with nothing in regexpr mode.

Val
  • 1
  • 8
  • 40
  • 64
0

Using regex and find&replace, you can delete all the lines containing #region without leaving empty lines. Because for some reason Ray's method didn't work on my machine I searched for (.*#region.*\n)|(\n.*#region.*) and left the replace box empty.

That regex ensures that the if #region is found on the first line, the ending newline is deleted, and if it is found on the last line the preceding newline is deleted.

Still, Ray's solution is the better one if it works for you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
George
  • 1,027
  • 3
  • 12
  • 20
0

You can try doing a replace of #region with \n, turning extended search mode on.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
northpole
  • 10,244
  • 7
  • 35
  • 58