6

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

a_subscriber
  • 3,854
  • 1
  • 17
  • 47

4 Answers4

10

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

D. Dimakakos
  • 146
  • 6
8

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.
Tobias
  • 32,569
  • 1
  • 34
  • 75
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.

Kadir Gunel
  • 233
  • 1
  • 10
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).

wvxvw
  • 11,222
  • 2
  • 30
  • 55