FAQ Desk: Search and Replace Across Files



  • Hello, and welcome to the FAQ Desk.

    You have likely been directed here because you asked a question about search and replace (or “regular expressions”, or “regex”) across multiple files.

    The answer depends on what you mean by “across files”.

    The good news: YES

    Can I do the same search and replace in multiple files in one go?

    Absolutely, yes!

    The main Find and Replace tabs have buttons that do their action in All in All Opened Documents, which uses the files open in tabs of the current instance of Notepad++ for deciding which files to work on.

    The Find in Files tab is a whole dialog devoted to finding and replacing text for all files (or a subset of files) in a folder or folder hierarchy.

    The new Find in Projects tab is similar to Find in Files, but it uses the active Project panel to decide which files to work on.

    The important thing to note about these functions is that they all apply to one file at a time. The search engine only has one “buffer” (file, effectively) open at a time, and will process that whole file before moving on to the next. There is no saving of data from the previous file to be used in the active file.

    The bad news: NO

    Can I search for data in one file, and copy or move it to another? What about if the files are in separate folders, too?

    or

    Can I search and replace in one file, using a list of search terms from a second file (possibly with a list of replacement terms in a third, or also parsed from the second)?

    or

    Can I merge lines (or matching lines) from two files into one of them ?

    Sorry, no to all of those. At least, not using only the search and replace function. That requires saving data or state across multiple file buffers, which is not implemented in the search and replace engine, as described above.

    The silver lining: NO, but…

    Notice that it was said “not using only the search and replace function”.

    If you can manually copy the data from one file into another, maybe with a separator line or other way to mark which data comes from where, it might be possible to craft a regular expression that will do what you want in the edited file… but you might run into the memory limitations of how much text can be spanned or captured in a single regex search and replace.

    It is possible to use the Mark feature to bookmark certain matching text, and then use Search > Bookmark > Copy (or Cut) Bookmarked Lines to get them into the copy buffer, which you can then paste into the second file, and do more manipulations there.

    Alternately, with scripting plugins (like PythonScript, LuaScript, and others), you have access to the buffers for all the open files, along with the full benefits of the programming language that the scripting plugin gives access to. With that, the answer is “definitely”, but you need to know how to program in that language, and you will have to look up the documentation for each for how to access the text from the open files. Sometimes, when you’ve hit this level of complexity, you will find that it’s easier or more efficient to skip the middle-man, and instead of doing it all inside Notepad++ and plugins, you just use Notepad++ to write a script that uses your chosen language’s own file IO functions to perform the task, rather than going through the plugin’s interface to grab the text in open files.