6

Emacs 26.1

In buffer

1


2


3


4

I use command "delete-blank-lines". But it NOT delete ALL BLANK LINES. It's delete ONLY ONE BLANK LINE.

Why?

I need to delete ALL BLANK LINES. The result must be like this:

1
2
3
4
  • 1
    Are you saying you used delete-blank-lines once? If you look at the help for the function, it will tell you it deletes the surrounding blank lines, that is the blank lines around point, not all blank lines in the buffer. – Willy Lee Mar 22 at 18:10
  • What @WillyLee said. – Drew Mar 22 at 20:43
  • See also: emacs.stackexchange.com/q/41636/105. The question has been asked more than once, expressed in different ways. – Drew Mar 22 at 20:45
8

From MasteringEmacs.com By Mickey Petersen:

This is a frequent question so I figured I’d mention the solution here:

You want to remove all empty (blank) lines from a buffer. How do you do it? Well, it’s super easy.

Mark what you want to change (or use C-x h to mark the whole buffer) and run this:

M-x flush-lines RET ^$ RET

And you’re done. So what does that mean? Well, M-x flush-lines will flush (remove) lines that match a regular expression, and ^$ contain the meta-characters ^ for beginning of string and $ for end of string. Ergo, if the two meta-characters are next to eachother, it must be a blank line.

We can also generalize it further and remove lines that may have whitespace (only!) characters:

M-x flush-lines RET ^\s-*$ RET

In this case \s- is the syntax class (type C-h s to see your buffer’s syntax table) for whitespace characters. The * meta-character, in case you are not a regexp person, means zero or more of the preceding character.

  • This help me: M-x flush-lines ^$ – a_subscriber Mar 23 at 16:49

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