Handle replacement properly to maintain line wrapping