Simeon, thanks! Your solution makes perfect sense (although I see no “Paste without formatting” option in the Edit menu, disabling it permanently works better for me anyway).
I wonder if formatting is also behind the other nasty bug that I can’t pin down (so maybe there’s more than one bug). A Find/Replace will often destroy code on a Replace All or even a few manual replacements within a single Find. What looks like is happening is that the Find is gathering text-location pointers when it first runs, but when Replace changes the text, the pointers aren’t updated so a subsequent replacement will replace the wrong characters.
[One clue to that is that the destruction doesn’t seem to happen if the replacement string is the same length as the search string.]
Sometimes, Replace All also skips instances of found text (doesn’t replace them).
That’s not the entire problem, and might be a red-herring… and I swear I’ve seen both replace and paste actually move lines (i.e. swap their positions).
But either way, I never, ever use Replace All and I am super careful about using Replace. More than once I’ve wasted hours recovering code that was brutally mangled by it.
I wish I could give you more details, but you might look into some connection between either pointers that become stale with each replacement, formatting-during-replace, or both.
Also… undo won’t work after Replace makes a mistake. Sometimes the undo even makes things worse.
Try finding and replacing “test” in the following text with XYZ (shorter), 012345 (longer), or “same” (same length) to see where it skips instances of “test” and where it makes replacement mistakes, and how Undo behaves afterward. FYI, this sample text purposely avoids using Lua keywords, to avoid the formatting complications.
–[[
I like to test things
This is a test
test file
test file
there is no question
there is no answer
Tetse not testable
there is no answer here
This test will succeed
This test may fail
]]