Search files for a pattern using Notepad++

Notepad++ is a very useful development tool. I always have it open when I’m coding and use it as an auxiliary tool. One thing you can use it for is searching files in a directory for a regex pattern.

For example, let’s say you want to find all classes declared in a C# project directory. You can use Notepad++ to find this information by searching for the regex pattern “public class \w+”. Alternatively, when you don’t want a quick GUI for searching, you can write code or use PowerShell to search for files.

In this article, I’ll show how to do this search using Notepad++. I’ll show the Notepad++ equivalent of the following grep command:

grep -Prh "public class \w+" --include "*.cs" "D:\Projects\aspdotnet-background-dblogger"
Code language: Bash (bash)

Note: -P makes it use the Perl Compatible Regular Expressions (PCRE) engine (which is what Notepad++ uses). -r makes it recurse through the specified directory. -h makes it not output filenames.

1 – Open the Find in Files window

Ctrl+F will bring up the search window. Click on the Find in Files tab.

Notepad++ Find in Files window

Note: This tab has a specific shortcut – Ctrl+Shift+F – and can be found in the Search menu. I find it easier and quicker to use Ctrl+F instead, since this is the standard shortcut for searching in practically every tool. No need to memorize yet another keyboard shortcut.

2 – Write a regex pattern

In the Search Mode section, make sure you have the Regular expression option selected.

In the Find what textbox, type in the regex pattern. In this case, it’s “public class \w+”.

Notepad++ Find in Files window with regex "public class \w+" specified in the Find what textbox

If you’re not sure what regex pattern to use, I recommend using an online regex test tool (like regextester) to figure out your pattern by trial and error. Make sure to use the PCRE engine, because that’s what Notepad++ uses.

3 – Specify a directory to recursively search through

Put the directory to search in the Directory textbox.

In most cases, you’ll want to search recursively so it looks through the subfolders and not just the top-level folder. Make sure to select the In all sub-folders option to make it recurse.

Optionally, you can specify a file name filter in the Filters textbox. This speeds up the search significantly by only looking in the files specified. In this example, it’s only looking for files with the .cs extension (C# source files).

Notepad++: Selecting a directly to recursively search through, filtering by file extension .cs (C# source files)

Notes:

  • Filters is equivalent to –include in grep.
  • In all sub-folders is equivalent to the -r flag in grep.

4 – Get the results

Click Find All and wait for the results.

Notepad++ search results showing a list of files and strings found in those files

This will output all lines that match the regex pattern. It’ll show context about where it found the matching line (file name and line number).

One thing you can do is copy all of the matching lines. To do that, right-click on the Find result bar, and click Copy from the context menu.

Notepad++ search results, listing files and strings within the files that match the specified pattern

Now paste anywhere, like in an empty Notepad++ tab:

    public class RecipesController : ControllerBase
    public class DatabaseLoggerService : BackgroundService, ILoggerService
    public class LogMessage
    public class LogRepository : ILogRepository
    public class Program
    public class Startup
Code language: plaintext (plaintext)

Note: Usually when I’m searching for a list of matching lines, I like to put them a checklist for further processing. I like to paste the resulting list into a Trello checklist. It automatically parses the text into one checkbox per line. This fits my purpose to a T.

Leave a Comment