Seems like if I set the Text property, the internal undo buffer is cleared and ctrl-z can no longer undo. Is there a way to replace all the text without loosing the undo?
Yes, the Text property is only for data-binding purposes and shouldn't normally be used.
The document has a SetText method with an overload where you can pass an ITextChangeType to it. If you use that overload, it will replace the text without clearing the undo history.
now, ctrl+z does undo back to the previous text, but the entire document is highlighted. Is there a way to do it so that the document is not highlighted on a ctrl+z?