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

::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

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

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 (from Windows SysInternals)
D:\Tools\SysInternals\AccessEnum.exe (from Windows SysInternals)
D:\Tools\SysInternals\Procexp.exe (from Windows SysInternals)

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

To insert a new line (carriage return) after at a specific character — 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.


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, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)


About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and he has been a Microsoft Most Valuable Professional (MVP) for 10 consecutive years from 2003 to 2012.

Leave a Comment