Multiple line replacing doesn't work?
-
I find the “find in files” feature to be very useful, and fairly often “replace in files” as well. It can save me a lot of time! But I’m having a problem now I didn’t used to have.
The ‘Replace with’ field does not allow you to paste multiple lines of text. It gets cut off after the first line. You can get multiple lines in the ‘Find what’ field by selecting them in a file and hitting <CTRL> + <F> but no way to replace a multi-line entry.
So this seriously cuts into the utility of this feature. For one, the ability to be very specific with your strings by selecting longer blocks is lost. But other uses are lost too.
What I don’t know is this - did I possibly inadvertently change some setting that caused this? Is this just how things are now(don’t remember upgrading between this working and now though)? If not a setting in the GUI, is there any file I can check/edit?
For reference, I’m on Windows 7 x64, and using version 6.9. I did see there are newer versions and I’ll update, but I wonder if there is anything else I need to know about this. I don’t expect anyone to fix it for me… just point me in the right direction to fix it myself, thanks.
-
Hello, Andrew,
I can set your mind at ease : you can, perfectly, fill up the replacement field, with multiple lines :-)) However this will work in the Extended or Regular expression search mode, ONLY !
Indeed, in Normal search mode, the ONLY way to type a multi-lines text, in the Find what: field, is to select the multi-lines block of text, first, then, open the Find, Replace, Find in Files or Mark dialog . And, although you cannot see the different line breaks, there are correctly inserted and a search does select that block of lines !
Unfortunately, in Normal search mode, this nice behaviour can’t be reproduced in the Replace with: field :-((
So you need to use, either, the Extended OR the Regular expression search mode ! In these two modes, just replace any line break that is to match, by the expression :
-
\r\n
, if your current file is a classical Windows file -
\n
, if your current file is an Unix file
So, for instance, after checking the Extended search mode, the original text, below :
abcde fghij klmno
With the two fields :
SEARCH
abcde\r\nfghij\r\nklmno
REPLACE
This is\r\na very\r\nsmall test
Would be changed, in a Windows file, into :
This is a very small test
REMARK :
Even in the Extended or Regular expression search mode, a previous selection of a multi-lines block, before opening any Find window, does insert this block, in the Find what: field ! But this operation is not allowed in the Replace with: field !
Best regards,
guy038
-
-
One thing to be aware when switching the type of search from “Normal” to “Regular expression” is that some of your text might contain special regular expression characters that need to be “escaped” before use. So, while selecting a multiline block of text and invoking Find to get that block placed into the “Find what” box will work often, it may not give you the intended results ALL of the time.
Here’s a list of the “troublesome” characters:
.^$*+?()[{\|
and sometimes these are “trouble” as well (inside character-classes): ^-]\
If these occur in your find or replace fields when you are intending to just handle normal text, any of these special characters occurring must have a backslash (\) inserted before them; this is called “escaping” the special meaning of the following character.
-
@guy038 said:
Indeed, in Normal search mode, the ONLY way to type a multi-lines text, in the Find what: field, is to select the multi-lines block of text, first, then, open the Find, Replace, Find in Files or Mark dialog . And, although you cannot see the different line breaks, there are correctly inserted and a search does select that block of lines !
Unfortunately, in Normal search mode, this nice behaviour can’t be reproduced in the Replace with: field :-((
It used to be possible(I know for fact because I used to use it this way!), and that’s far more useful a time saver than what you’re describing. Also I worry that extended mode would take my escape characters(part of strings) and apply them to the files… which is clearly undesired behavior.
While it is regrettable that I’d have to go that route, I’d rather revert versions of notepad++ to regain this simple ability. Do you happen to know the last version before this change occurred?
-
Here’s a short Pythonscript that will form properly escaped strings that can be used in the “Find what” and “Replace with” boxes when the search mode is Regular expression. Run it after making a selection from the text you wish to use (source text must come from a Notepad++ editor tab window). Running it will place the escaped string in the clipboard, ready for pasting into either of the Find window boxes:
import re def escape(orig_text): escaped_text = re.sub(r'([\\.^$*+?\-[\](){}|])', r'\\\1', orig_text) escaped_text = re.sub(r'\x0D', r'\\r', escaped_text) escaped_text = re.sub(r'\x0A', r'\\n', escaped_text) return escaped_text if editor.getSelections() == 1 and not editor.getSelectionEmpty(): editor.copyText(escape(editor.getSelText()))
Hopefully I covered all of the characters that need escaping, but if I forgot any, they can easily be added to the first parameter in the first call to re.sub().
-
Note that the reason I didn’t simply call re.escape() in the above Pythonscript is that it escapes too much…for example, it will escape the ’ character (single quote), and Notepad’s regex search doesn’t like that…there may be other characters as well…
-
Thanks for that
-
I do have some more information to add. While I don’t exactly at what point this changed, I can tell you that version 6.4.2 works the way I want… or it did. But I can’t just download it and expect it to work though. The program itself, yes. But it seems whatever cause the change somehow affects all versions I’ve tried(versions prior to the change). This retroactively affects behaviors of previous version installs.
How did I realize this? Well I had a few versions already on my computer dating back to 5.9, but simply opening it and attempting this has the same behavior as 7.2 does. However… I happened to have 6.4.2 installed on a virtual machine I have setup. Guess what? It does what I want!
So that tells me a strong possibility something has changed in either a config file somewhere, or registry rather than in compiled code. And that means with some digging, I may be able to fix this while keeping up to date. If I find anything, I’ll be glad to share. Sure running through a script is manageable, but it still can’t beat simple <CTRL>+<C>, <CTRL>+<V>
-
I’d be interested in hearing about your findings if you get the old behavior for the replace box working. Regarding,
“can’t beat a simple <CTRL>+<C>, <CTRL>+<V>”, if you tie the script to a key combo, it sure comes close to that simplicity (just a different keystroke than <CTRL>+<C>). :-) -
Axiomatic Ones:
I made a similar request in the ‘Help’ forum:
https://notepad-plus-plus.org/community/topic/13808/search-replace-of-multiline-text-blocs
“Search/Replace of Multiline Text Blocs”I made a reference to another great freebie: ‘Replace Text’ by Ecobyte. I use it to replace text in hundreds of Web pages at once. It has a special feature: ‘Advanced Edit’. I copy-and-paste blocs of text into ‘Advanced Edit’. It converts the bloc into a one-line string with special characters between the lines in the original bloc to be searched/replaced. It takes a few seconds to complete and it is very accurate and reliable. No complicated regex coding needed.
If not present, Notepad++ should add such feature to its arsenal. I thought the ‘Extended (\n, r, …)’ search option would do it. It doesn’t work for me. Maybe because I placed \n ahead of \r and added spaces… It would be great to perform such a task in Notepad++ without regex, instead of exiting and running Ecobyte.
But, hey, even the post editor here can use significant improvements!
Ion Saliu
“A good man is an axiomatic man; an axiomatic man is a happy man. Be axiomatic!”