How do I delete specific lines in Notepad++?

Notepad++

Notepad++ Problem Overview


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?

Notepad++ Solutions


Solution 1 - Notepad++

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

Solution 2 - Notepad++

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.

Solution 3 - Notepad++

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/

Solution 4 - Notepad++

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.

Solution 5 - Notepad++

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.*$

Solution 6 - Notepad++

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

Solution 7 - Notepad++

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

Solution 8 - Notepad++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRismoView Question on Stackoverflow
Solution 1 - Notepad++RayView Answer on Stackoverflow
Solution 2 - Notepad++John TView Answer on Stackoverflow
Solution 3 - Notepad++Rishabh PantView Answer on Stackoverflow
Solution 4 - Notepad++ocroquetteView Answer on Stackoverflow
Solution 5 - Notepad++NickView Answer on Stackoverflow
Solution 6 - Notepad++ValView Answer on Stackoverflow
Solution 7 - Notepad++northpoleView Answer on Stackoverflow
Solution 8 - Notepad++GeorgeView Answer on Stackoverflow