Notepad++ Tips: Find and Replace, and Text Manipulation Examples

notepad++ iconNotepad++ is an excellent light-weight text editor with many useful features. With Notepad++, you can find and replace text in the current file or in multiple files in a folder recursively. You can also find and replace text using regex.

This post has many Notepad++ find & replace examples and other useful Notepad++ tips for different scenarios.

Remove Path from the File name in a text file

If you have full paths for files in a text file and want to remove the path (i.e., only want the file name), use the following Find & Replace technique:

notepad++ remove path from file name

  1. Bring up the Replace dialog (Ctrl + H) and use the following replace method:
  2. In the Find box, type ^.*\\
  3. Set the Search mode to Regular expression
  4. Leave the Replace box blank.
  5. Uncheck matches newline
  6. Click Replace All

::Before::

C:\Users\ramesh\Pictures\Screenshots\Screenshot 90.png
C:\Users\ramesh\Pictures\Screenshots\Screenshot 97.png
C:\Users\ramesh\Pictures\Screenshots\Screenshot 10.png
C:\Users\ramesh\Pictures\Screenshots\Screenshot 15.png

::After::

Screenshot 90.png
Screenshot 97.png
Screenshot 10.png
Screenshot 15.png

Remove File name from Full Path in a text file

To remove the file name from a full path, use this search operator:

  • Find what: \\[^\\]+$
  • Replace with: Leave empty
  • Set the Search mode to Regular expression
  • Uncheck matches newline
  • Click Replace All

::Before::

D:\Tools\Sysinternals\accesschk.exe
D:\Tools\Sysinternals\AccessEnum.exe
D:\Tools\NirSoft\AddrView.exe
D:\Tools\Others\activehotkeys.exe

::After::

D:\Tools\Sysinternals
D:\Tools\Sysinternals
D:\Tools\NirSoft
D:\Tools\Others

Tip: If you need the trailing slash after the folder path, you can use the following regex search instead.

  • Find what: (.*\\).*
  • Replace with: \1

Remove a fixed number of characters from the beginning of each line

To remove a fixed number of characters at the beginning of each line in a text file, use this regex search & replace query:

  1. Find what: ^.{11}(.*)$
  2. Replace with: $1
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

This deletes the first 11 characters from the starting of each line.

::Before::

File Path: D:\Tools\Sysinternals\accesschk.exe
File Path: D:\Tools\Sysinternals\AccessEnum.exe
File Path: D:\Tools\NirSoft\AddrView.exe
File Path: D:\Tools\Others\activehotkeys.exe

::After::

D:\Tools\Sysinternals\accesschk.exe
D:\Tools\Sysinternals\AccessEnum.exe
D:\Tools\NirSoft\AddrView.exe
D:\Tools\Others\activehotkeys.exe

Delete characters that exceed the number of characters

To delete characters that exceed the number of characters in a text file, use this:

  1. Find what: ^.{19}\K.*$
  2. Replace with: Leave blank
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

This deletes the characters that exceed 19 characters in each line.

::Before::

The Quick Brown Fox is lazy
The Quick Brown Fox is very cute
The Quick Brown Fox jumps over the lazy dog

::After::

The Quick Brown Fox
The Quick Brown Fox
The Quick Brown Fox

Remove text after a specific character from each line in a text file

To remove text after a specific character — e.g., a hyphen, from each line in a text file, use:

  1. Find what: (.+)\s*-\s*(.+)
  2. Replace with: $1
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

To remove the text before a character (e.g., hyphen), use $2 in the replace field:

  1. Find what: (.+)\s*-\s*(.+)
  2. Replace with: $2

Alternatively, to remove text after a specific character or word, you can use the following, which looks easier:

  1. Find what: -.*
  2. Replace with:  leave it empty
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

::Before::

accesschk.exe - from Sysinternals
AccessEnum.exe - from Sysinternals
AddrView.exe - from NirSoft
activehotkeys.exe - from another vendor

::After::

accesschk.exe 
AccessEnum.exe 
AddrView.exe 
activehotkeys.exe

You can also use it to remove text after a specific word (e.g., “from”).

  1. Find what: from.*
  2. Replace with:  leave it empty
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

::Before::

accesschk.exe from Sysinternals
AccessEnum.exe from Sysinternals
AddrView.exe from NirSoft
activehotkeys.exe from another vendor

::After::

accesschk.exe 
AccessEnum.exe 
AddrView.exe 
activehotkeys.exe

Remove leading or trailing space from each line in a text file

To remove the trailing and/or leading whitespace from each line in a text file, use the Blank Operations menu.

From the Edit menu in Notepad++, click Blank Operations
notepad++ tips - remove blank whitespace

Choose one of the three options:

  • Trim Trailing Space
  • Trim Leading Space
  • Trim Leading and Trailing Space

Delete blank lines in a text file

To delete empty/blank lines in a text file, from the Edit menu in Notepad++, select Line Operations, and click Remove Empty Lines

notepad++ tips - remove empty lines

To also remove lines containing blank characters or white spaces, click Remove Empty Lines (Containing Blank characters) option instead.

::Before:

The Quick Brown Fox is lazy


The Quick Brown Fox is very cute


The Quick Brown Fox jumps over the lazy dog

::After::

The Quick Brown Fox is lazy
The Quick Brown Fox is very cute
The Quick Brown Fox jumps over the lazy dog

Delete empty lines only in the selected rows

Note that the above command removes empty lines in the entire text file. To remove empty lines only within the text selection, use this search operator:

  1. Select the rows where you want to remove empty lines.
  2. Bring up the Replace dialog (Ctrl + H)
  3. In the Find what: box, type \n\r
  4. Leave the Replace with: box empty
  5. Enable the In selection checkbox
  6. Select Search Mode to Extended
  7. Click Replace All

That’s it! It deletes the empty rows within the selected rows only rather than the entire file.


Remove text after ‘n’th occurrence of comma or symbol

Suppose you have text in each line delimited by a comma or any other symbol. Example below:

::Before::

------------------------------
name,address,pin,landmark
------------------------------
ramesh,10 san jose avenue,11011,near museum
pete,1 sf marg,45089,near childrens park
john,7 rcr,11909,near metro station

To remove text after the 3rd occurrence of the comma, use this find and replace search operator:



  1. Find what: ^([^,]*,[^,]*,[^,]*),.*$
  2. Replace with: $1
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

::After::

--------------------------
name,address,pin
--------------------------
ramesh,10 san jose avenue,11011
pete,1 sf marg,45089
john,7 rcr,11909

Prefix each line with a word or phrase in a text file

To add a word or phrase (prefix) at the beginning of each line in a text file, use the following search & replace operator:

  1. Find what: ^
  2. Replace with: Some word or phrase
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

The above can be especially useful when creating a HOSTS file to block a list of certain Ad servers. Use 0.0.0.0 with a trailing space in the Replace with: text box, and click Replace All. This adds the prefix string for each line (Ad server) in the text file.

::Before::

ssp.adriver.ru
r.adrolays.de
adrotate.se
www.adrotate.net
adrunnr.com

::After::

0.0.0.0 ssp.adriver.ru
0.0.0.0 r.adrolays.de
0.0.0.0 adrotate.se
0.0.0.0 www.adrotate.net
0.0.0.0 adrunnr.com

Suffix each line with a word or phrase in a text file

To add a word or phrase (suffix) at the end of each line in a text file, use the following search & replace operator:

  1. Find what: $
  2. Replace with: Some word or phrase
  3. Set the Search mode to Regular expression
  4. Uncheck matches newline
  5. Click Replace All

::Before::

D:\Tools\Sysinternals\accesschk.exe
D:\Tools\Sysinternals\AccessEnum.exe
D:\Tools\Sysinternals\Procexp.exe

::After::

D:\Tools\Sysinternals\accesschk.exe (your word or phrase here)
D:\Tools\Sysinternals\AccessEnum.exe (your word or phrase here)
D:\Tools\Sysinternals\Procexp.exe (your word or phrase here)

Remove duplicate rows in a text file using Notepad++ without sorting the lines

To remove duplicate rows in a text file using Notepad++ without sorting the rows, use this search and replace operator:

  1. Find what: ^(.*?)$\s+?^(?=.*^\1$)
  2. Replace with: Leave blank
  3. Set the Search mode to Regular expression
  4. *Enable* matches newline
  5. Click Replace All

This removes all the duplicate lines leaving out the original. As a bonus, it also removes blank lines automatically.

notepad++ tips - remove duplicate rows without sorting

Important: You must enable matches newline for this to work. Credits to stema

The above is a brilliant method that doesn’t need sorting of the lines. The duplicate rows can be located anywhere in the text file and they’re not reordered.

::Before::

12345
23456
34567
45678

12345
23456
34567
45678

12345
23456
34567
45678

::After::

12345
23456
34567
45678

Remove consecutive duplicate lines

If the duplicate rows are located immediately after each other, to remove the consecutive duplicate rows, from the Edit menu in Notepad++, click Line Operations, and select Remove Consecutive Duplicate Lines

::Before::

12345
12345
12345
23456
23456
34567
34567
45678
45678

::After::

12345
23456
34567
45678

Insert new line (carriage return) at a specific character or string

To insert a new line (carriage return) after at a specific character or string — e.g., after a Comma, use this search and replace operator:

  1. Find what: ,
  2. Replace with: \r\n
  3. Set the Search mode to Extended
  4. Click Replace All

The above search & replace operation adds a new line wherever the comma appears.

::Before::

Cecilia Chapman,711-2880 Nulla St.,Mankato Mississippi 96522,(257) 563-7401,Iris Watson,P.O. Box 283 8562 Fusce Rd.

::After::

Cecilia Chapman
711-2880 Nulla St.
Mankato Mississippi 96522
(257) 563-7401
Iris Watson
P.O. Box 283 8562 Fusce Rd.

If you want to retain the trailing comma after every line, use ,\r\n in the Replace with: text box.

Example 2:

To insert a new line at a specific string ( named GUID:), use this example:

  1. Find what:  GUID:
  2. Replace with: \r\n
  3. Set the Search mode to Extended
  4. Click Replace All

::Before::

Documents GUID:{D3162B92-9365-467A-956B-92703ACA08AF}

Downloads GUID:{088E3905-0323-4B02-9826-5D99428E115F}

Music GUID:{3DFDF296-DBEC-4FB4-81D1-6A3438BCF4DE}

Pictures GUID:{24AD3AD4-A569-4530-98E1-AB02F9417AA8}

Videos GUID:{F86FA3AB-70D2-4FC7-9C99-FCBF05467F3A}

::After::

Documents 
GUID:{D3162B92-9365-467A-956B-92703ACA08AF}

Downloads
GUID:{088E3905-0323-4B02-9826-5D99428E115F}

Music
GUID:{3DFDF296-DBEC-4FB4-81D1-6A3438BCF4DE}

Pictures
GUID:{24AD3AD4-A569-4530-98E1-AB02F9417AA8}

Videos
GUID:{F86FA3AB-70D2-4FC7-9C99-FCBF05467F3A}

One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)


Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

10 thoughts on “Notepad++ Tips: Find and Replace, and Text Manipulation Examples”

  1. Hello everyone,
    what I am looking for, and ^£&”6£” google ^*£^£ of course cannot find, is: how to SAVE MY FAVORITE search & replace strings (regex obviously), anyone got a solution for THIS?

    Like my favorite 50 or so, at least, as I am using notepad++ with regex a LOT: It works GREAT 🙂

    Reply
    • I noticed that there are a few points, can we do it differently to make it easier to understand?
      Ex : ” Remove a fixed number of character …” , I did the same thing but I used regex :
      Find what : ^.+ ( a space after the ‘+’ ) . Replace with: I leave it blank.
      And In ” Remove text after a specific character …” I used regex:
      Find what : -.*
      Replace with : I leave it blank. I will get the text before the “- “, and if I want to have the result of the text after the “-” I use : ^.+- (similar to the first regex, instead of after the ‘+’ is a space then here is the “-“) . I have the same your result.
      I have a few questions here. Example 2 in the ” Insert new line (carriage return) at a specific..” method you have used :
      Find what : GUID: , it does not work for me. I have tried many ways but it does not. But I found in your example, there is a space before the word “GUID:”, so I used:
      Find what : I type a space
      Replace with. : \r\n
      And this gave the same result. Before reading your post, I knew very little about Regex, and thanks to this article I knew a lot more. And it will definitely support me.
      I did not mean anything, I just wanted to say: There are a lot of Regex with similar content but it depends on the format of each document, of each line.
      For example, when concatenating emails with “:” and I have not succeeded when using regex in ” Remove text after ‘n’th occurrence … ” Before reading your post, I knew very little about Regex, and thanks to this article I knew a lot more. And it will definitely support me. I did not mean anything, I just wanted to say: There are a lot of Regex with similar content but it depends on the format of each document, of each line. For example if the contents of the lines are formatted as concatenated emails as “,” and I have failed using your regex in “Remove text after ‘n’th occurrence …” And if it is : ahducnw7e, [email protected], a+b=c, 1983, I lay my love
      so what should I do to delete the text ” I lay my love” and other lines (The format of each line is completely different )
      Nice to meet everyone in this content.Thanks add for writing this article. My English is bad, I hope to sympathize.

  2. In Notepad++, in the following Example below, how does one replace the ^ with a | up to the E:\ (the first ten occurences) and keep all of the ^ after E:\ in this example?

    Example: John Doe^123-45-6789^1234567^313313313313^444555^11^Doc-Type-Auto^02102021^11^NoteDisclosure^E:\Archive\John Doe^123-45-6789^1234567^313313313313^444555^11^Doc-Type-Auto^02102021^11^NoteDisclosure.pdf

    Final: John Doe|123-45-6789|1234567|313313313313|444555|11|Doc-Type-Auto|02102021|11|NoteDisclosure|E:\Archive\John Doe^123-45-6789^1234567^313313313313^444555^11^Doc-Type-Auto^02102021^11^NoteDisclosure.pdf

    Reply
  3. How to Find a word in a line and prefix text (//) to the starting of that line
    I want to comment those lines which has Microsoft.VisualBasic.PowerPacks.LineShape

    Public WithEvents _Line_1 As Microsoft.VisualBasic.PowerPacks.LineShape

    Reply
  4. Is this webpage still monitored?
    I can’t force Notepad++ to replace a control-linefeed (/r/n) with 2 control-linefeeds (/r/n/r/n) in order to insert new blank lines in a Notepad++ document.
    Have the Notepad++ coders removed that possibility?

    Reply
  5. Please check my question Below

    I have text like below starting with 4 or 5 digt

    “X9894|bctd” “xxxxx” “xxxxxx” “xxxx”=”123-4587” or
    “9894|bctd” “xxxxx” “xxxxxx” “xxxx”=”123-4587”

    I want to replace it as

    “1234587|bctd” “xxxxx” “xxxxxx” “xxxx”=”123-4587” “xxxx”

    I have 1000+ lines like this, Can someone please help me on this issue.

    Thanks and regards,
    JK

    Reply

Leave a Reply to Amol Cancel reply