Is there a way to remove blank lines from a region of code I select?

I am asking because I copy/pasted code and it seems to have several blank lines I want to get rid off.

1 Answer 1

  1. Select what you want to change, or C-x h to select the whole buffer.
  2. Then: M-x flush-lines RET followed by ^$ RET or ^[[:space:]]*$ RET

^[[:space:]]*$ contain the meta-characters:

  • ^ for beginning of string,
  • $ for end of string,
  • [[:space:]]* zero or more spaces.

Ergo, if the first two meta-characters are next to each other or if there is one or more spaces between them, it must be a blank line. Source

You may eventually be interested by shrink-whitespace

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.