Emacs 26.1

I has the next text:

enter image description here

I need to remove all whitespaces in the beginning of the lines. Every line has different count of whitespaces. The result must be like this:

enter image description here

4 Answers 4


Another way is just to mark the region and call delete-whitespace-rectangle!

  • It's cool!!! Thanks. I don't know about this. Oct 23, 2018 at 7:18
  • 1
    best solution, ty.
    – Wizard
    Jan 4, 2020 at 3:35
  • This is brilliant
    – Jerry
    Feb 17, 2022 at 12:53

IMHO the standard way is:

  1. Go to the top of your buffer.
  2. Type C-M-% for query-replace-regexp.
  3. Input ^\s-+ as regular expression and RET. (See explanation below.)
  4. Leave the replacement string empty, i.e., press RET again.
  5. You are prompted by query-replace-regexp in the minibuffer.
    Press ! to perform all replacements at once.

Explanation of the regular expression:

  1. The caret ^ stands for the beginning of line.
  2. The \s- stands for any character designated as space by the current modes syntax table.
  3. The + stands for one or more contiguous matches.

You can use regex inside emacs (C-M-s). Check here emacs regex. By using regex you can write a rule that matches all the blanks that are at the beginning.


Another thing you can do: use M-xalign-regex, then use SPC as input to this command (i.e. align on space), then use rectangle-editing commands: C-x r k for example to delete rectangle before the lines.

This is not terribly efficient, but it's how I do it because I use both commands very often and they are, sort of, automatic for me (muscle memory, or w/e you call it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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