72

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.).

4 Answers 4

106

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

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

4
  • 7
    Nice, thanks. You can 'cut bookmarked lines' for a poor man's grep -v too.
    – ARRG
    Jul 15, 2013 at 15:02
  • 3
    In later versions, e.g. 5.4.5, there isn't any "Mark" tab. Instead, there is a checkbox, "Mark Line", in the "Find" tab. And "Copy Bookmarded Lines" is direcly in the "Search" menu. May 4, 2014 at 22:23
  • @ARRG Can't find the cut bookmarked lines or Copy bookmarked lines in current version, where is it ? Feb 2, 2016 at 9:11
  • Working like Charm :-) Sep 11, 2018 at 19:34
17

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.

4
  • 1
    The added line numbers might even be a feature :) Apr 14, 2012 at 7:37
  • 2
    You might find this plugin useful code.google.com/p/searchplus
    – Amarghosh
    Oct 5, 2012 at 14:14
  • @Amarghosh Nice plugin, but lacks a bit of ergonomy (why buttons and no keyboard shortcuts ?). Nice catch :)
    – Benj
    Apr 5, 2013 at 11:58
  • @Benji Alt-Q will launch the plugin. Even though there are no shortcut keys within plugin window, basic tab navigation has been enabled. If there is enough interest in the plugin, I will try to add more features as I get some time. Thanks for the feedback :)
    – Amarghosh
    Apr 6, 2013 at 13:42
6

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

5
  • First of all, thanks for a great answer that's been waiting years to be upvoted... : - ) I thought I was having some problems with the script, it didn't work. Then I noticed we're supposed to adapt the paths, etc., but you didn't mention it explicitly so I just copy-pasted blindly! I work so fast it slows me down : - )
    – pgr
    Feb 1, 2017 at 11:32
  • thanks a lot for the feedback. I will add that to my answer. Feb 1, 2017 at 11:40
  • Another small note: $(FULL_CURRENT_PATH) won't return any path if the file is not saved yet. You might expect it to reside somewhere (temp directory?), but not really. This unsaved file situation might be quite common, you just paste something from a log to grep it... we might perfect this script to save the file if it isn't saved yet - but for simplicity I'll just remind myself to save manually before grepping. Thanks.
    – pgr
    Feb 1, 2017 at 11:51
  • Does this work if the search string contains space(s)?  or other special characters? Jan 13, 2018 at 19:25
  • Yes that does work Jan 13, 2018 at 19:28
-1

Step 5: Create script for quick save and grep document

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

SET local AUTOSAVEFILE = "%tmp%\NpPPautosave.tmp"
SET local OUTFILE = "%tmp%\NpPPautosaveToGrep.temp"
NPP_SAVEAS "%tmp%\NpPPfile.tmp"
INPUTBOX "Input string for GREP"
cmd /c findstr $(INPUT) $(AUTOSAVEFILE) > $(OUTFILE)
NPP_OPEN $(OUTFILE)

5
  • Where is step 1, 2, 3 and 4? Jan 13, 2018 at 17:54
  • (1) We prefer, to the greatest extent possible, that answers stand alone.  It looks like there’s (at least) one step that has to be done before somebody can use your answer (hint: Step 1); please include it in your answer.  (2) It appears very strongly that your answer is standing on the shoulders of Erik Kalkoken’s answer.  If you’re going to do that, you should explicitly say so. … (Cont’d) Jan 13, 2018 at 18:05
  • (Cont’d) …  (3) Is your answer better than Erik’s? If so, please explain how and why. (In fact, it would be nice if you would explain what the answer does, and how to use it.) If not, why have you posted it? (4) Do you believe that it is somehow superior to use a hard-coded output filename rather than one that is based on the current filename? (5) Is there some advantage to running findstr on the autosavefile rather than the current file? (6) Why does your answer use both C:\temp and C:\tmp? … … Please do not respond in comments; edit your answer to make it clearer and more complete. Jan 13, 2018 at 18:05
  • (1) "one step that has to be done before somebody" - all Erik spteps from 1 to 4. (2) You must have 50 reputation to comment. (to comment Erik post) (3) Yes, my answer small better, because Erik answer doesn't work before document was be saved to harddisk. I want't save temporary file just for grep it. "what the answer does, and how to use it" - my code just save opened text in notepad++ to temporary file in hdd. This is line "NPP_SAVEAS" just automatically save text on hdd. (4) Not! exactpy not. (5) Yes! The Erik core doesn't work, before save to harddrive, look to my (3) answer.
    – Kasumiru
    Mar 6, 2018 at 13:13
  • (6) Sory, I was in a hurry and mistake, my windows cygwin have tmp and temp dirs. "Please do not respond in comments" - sory, but I waswrite my answer before read this comment
    – Kasumiru
    Mar 6, 2018 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .