Google Docs – Replace Whitespace with Tabs at Paragraph Start

google docs

I am using Google Docs to edit digitalized documents. Those documents end up with whitespace at the beginning of every paragraph, which I have to replace with tabs so they do not take up space at a Braille Display. How can I replace these whitespace sequences with one tab?

Best Answer

This can be done with the script given below. Enter it under Tools > Script Editor, and run. It will replace any amount of whitespace at the beginning of a paragraph with a single tab. Other whitespace will remain untouched.

function spaces2tabs() {  
  var paras = DocumentApp.getActiveDocument().getBody().getParagraphs();
  for (var i = 0 ; i < paras.length; i++) {
    paras[i].replaceText("^\\s+", "\t");
  }
}