Grep-like functionality for Notepad++?

I would like to have some grep-like functionality in Notepad++.

In it simplest version, I would like to be able to copy all lines from the current buffer that contain the word foo to a new buffer. I can use the TextFX plugin to hide all lines without foo, but that doesn't do all that I want (it's lost on Alt - Tab, I can't copy only the matching lines, etc.).


Solution 1:

Ctrl+F --> go to the Mark tab --> toggle Bookmark line --> Click Mark All.

Select menu Search --> Bookmark --> Copy Bookmarked Lines.

Solution 2:

I was just looking into this same question. I am not sure if you're aware; in the Find dialog, you can Find All in Current Document which will pop up a window at the bottom showing the matching lines. Then you can copy that into a new document.

Unfortunately, it includes some extraneous information in the results (line number, etc.), but you can filter that out using column mode.

Solution 3:

Another great solution is to use the NppExec plugin to add a grep command for Notepad++.

The main advantage over the "Bookmarks" solution is that it runs much faster especially for large files and you don't have to modify your original file. Note that you have to create a small script once to make this feature available in your Notepad++.

My solution example is for Windows and I use findstr, which is a grep-like command tool available on all Windows machines. But you can of course also use any other grep clone of your choice.

Step 1: Install NppExec plugin

Go to Plugins / Plugin Manager / Show Plugin Manager, scroll down to NppExec, select this plugin and click on Install. You may have to restart your Notepad++ to complete the installation process.

Step 2: Create a small script for NppExec

Go to Plugins / NppExec / Execute, enter the following script and save it as "grep":

SET local OUTFILE = "c:\temp\$(FILE_NAME).temp"
INPUTBOX "Input filter string"
cmd /c findstr $(INPUT) "$(FULL_CURRENT_PATH)" >$(OUTFILE)
NPP_OPEN $(OUTFILE)

(please double-check if the path for the OUTFILE works for your environment)

Step 3: Add a menu item for your script (Optional)

Go to Plugins / NppExec / Advanced Options, choose the script "grep" under Associated script and click OK to save. Restart Notepad as requested by NppExec to complete adding the new menu item.

Step 4: Add a shortcut for your script (Optional)

Go to Settings / Shortcut Mapper / Plugin commands, scroll down to find your script command "grep" and define a shortcut for it. e.g. ALT-G