Multiple word search and replace in notepad++
Install Python Script plugin from Plugin Manager.
Create a file with your substitutions (e.g., C:/Temp/Substitutions.txt), separate values with space:
good bad
great worse
fine not
Create a new script:
with open('C:/Temp/Substitutions.txt') as f:
for l in f:
s = l.split()
editor.replace(s[0], s[1])
Run the new script against the text you want to substitute.
I needed to run the substitution on several files.
So, based on Mauricio Morales's answer, I created the follow script.
with open('C:/Temp/Substitutions.txt') as f:
files = notepad.getFiles()
for file in files:
notepad.activateFile(file[0])
for l in f:
s = l.split()
editor.replace(s[0], s[1])
f.seek(0) # reset file input stream
Try a regular expression replace of (good)|(great)|(fine)
with (?1bad)(?2worse)(?3not)
.
The search looks for either of three alternatives separated by the |
. Each alternative has ist own capture brackets. The replace uses the conditional form ?Ntrue-expression:false-expression where N is decimal digit, the clause checks whether capture expression N matches.
Tested in Notepad++ 6.3
Update:
You can find good documentation, about the new PRCE Regular Expressions, used by N++, since the 6.0 version, at the TWO addresses below :
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
The FIRST one concerns the syntax of regular expressions in SEARCH
The SECOND one concerns the syntax of regular expressions in REPLACEMENT
And, if you can understand "written French", I made a tutorial about PCRE regular expressions, stored in the personal site of Christian Cuvier (cchris), at the address below :
http://oedoc.free.fr/Regex/TutorielRegex.zip
(Extracted from a posting by THEVENOT Guy at http://sourceforge.net/p/notepad-plus/discussion/331754/thread/ca059a0a/ )
I looked around for some software that would replace several terms at the same time, Notepad ++ has a character limitation (limit of 2046 characters in the replace field), so I decided to make my own replacement version in HTML, here is the site:
https://jsfiddle.net/Byte/y50q1e7g/
window.onload = function(){
$("textarea,input[type='text']").focus(function() { $(this).select(); } );
}
function replace(){
// Get Configuration
let separatedBy = new RegExp( $("#separatedBy").val() );
let useRegex = $("#useRegex").prop("checked");
let isCaseSensitive = $("#caseSensitive").prop("checked") ? "" : "i";
// Get 'Find' and 'Replace'
let find = $('#find').val().split(separatedBy);
let replace = $('#replace').val().split(separatedBy);
// Make result equal origin
$("#result").val($("#original").val());
// Run in each 'find' item
for(i=0; i<find.length; i++){
// check if not use Regex to escape the regex
find[i] = useRegex ? find[i] : find[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
// Replace
$("#result").val($("#result").val().replace(new RegExp(find[i],"g"+isCaseSensitive), replace[i]));
}
}
*{
font-family: Sans-Serif;
box-sizing: border-box;
}
body{
padding:1em 10%;
}
textarea,input[type='text']{
font-family: monospace;
}
textarea{
border-radius: 5px;
width: 100%;
padding:1em;
}
.config{
width: 100%;
border-radius: 5px;
border: 1px solid #888888;
padding: 1em;
display: inline-block;
color: #888888;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
Original text:<br />
<textarea id="original" rows="4" cols="50"></textarea>
</p>
<p>
Items to find:<br />
<textarea id="find" rows="4" cols="50">item1
item2
item3</textarea>
</p>
<p>
Items to replace:<br />
<textarea id="replace" rows="4" cols="50">replace1
replace2
replace3</textarea>
</p>
<p class="config">
<b>Configuration:</b><br><br />
- Items separated by <input id="separatedBy" type="text" value="\n" size="3" /> (Regex)<br />
- <input id="useRegex" type="checkbox"/> Use Regex in each item<br>
- <input id="caseSensitive" type="checkbox" checked/> Use case sensitive
</p>
<p>
<input type="button" value="Replace" onclick="replace()" />
</p>
<p>
Result:<br>
<textarea id="result" rows="4" cols="50"></textarea>
</p>