1

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

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.
1

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.

1

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).

1

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

  • It's cool!!! Thanks. I don't know about this. – a_subscriber Oct 23 '18 at 7:18

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.