import re resc = re.escape start = editor.getSelectionStart() input = editor.getSelText() output = resc(input) editor.replaceSel(output) editor.setSelectionStart(start) editor.setSelectionEnd(start+len(output))