var s1 = document.selection.Text.split("\n"); var s2 = new Array(); for (var i = 0; i < s1.length; i++) { if (s1[i].charAt(0) == "\t") s2.push(s1[i].substr(1).replace(/^\s+/g, "")) else s2.push("\t" + s1[i]) } document.selection.Text = s2.join("\n");