0

What is an easy way to remove all leading spaces from lines in a Google Doc?

E.g. if copy-pasting a large amount of text that has been indented with spaces, such as

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Nullam mollis nulla sit amet metus eleifend, in eleifend metus facilisis.
  Aenean tempor diam sed quam fringilla dignissim.
    Pellentesque in sem aliquam, ultricies purus et, dapibus mi.
  Aliquam sit amet diam sed eros aliquam faucibus ac auctor sapien.
    Suspendisse consectetur mauris commodo tellus dignissim bibendum in in risus.

Should become:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nullam mollis nulla sit amet metus eleifend, in eleifend metus facilisis.
Aenean tempor diam sed quam fringilla dignissim.
Pellentesque in sem aliquam, ultricies purus et, dapibus mi.
Aliquam sit amet diam sed eros aliquam faucibus ac auctor sapien.
Suspendisse consectetur mauris commodo tellus dignissim bibendum in in risus.
0

You can use the following script:

function LeadingBlanks() {

  var document = DocumentApp.getActiveDocument();
  var lblanks = "";

  var regExp = "^[[:blank:]]*";
  document.replaceText(regExp, lblanks);

}

EDIT

As suggested by Vincent Scheib the above code can be further compacted/simplified

function LeadingBlanks() {
  var document = DocumentApp.getActiveDocument();
  var regExp = "^ *";
  document.replaceText(regExp, "");    
}
  • Nice that the Apps Script works. Sad that any regular expression using "^" fails in the editor. – Vincent Scheib Aug 26 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.