Delete blank lines with flush-lines

Sometimes after I copy-paste code snippets from web page into emacs buffer I get text with superfluous blank lines delimiting code.

Marking region and running M-x flush-lines RET ^$ RET quickly makes it better by removing those pesky blank lines. This is, of course, makes sense only if pasted code doesn’t contain blank lines you want to preserve.

Update:
You may need to use something more complex than simple ^$ (something like ^\W*$ as regexp) if “blank” line contains some whitespace characters.

Example before:

-export([test1/0,

         test2/0,

         test3/0,

         double/1]).

After marking region and applying M-x flush-lines RET ^\W*$ RET (in your case just ^$ may be sufficient):

-export([test1/0,
         test2/0,
         test3/0,
         double/1]).

3 Responses to “Delete blank lines with flush-lines”

  1. hober says:

    There’s also M-x delete-blank-lines RET.

  2. Ruslan Spivak says:

    Yes, that one is useful on its own. It’s just that M-x flush-lines RET ^$ RET will work on region like:
    text1
    _blank_line_
    text2
    _blank_line_
    text3

    producing:
    text1
    text2
    text3

    while C-x C-o works only on consequent blank lines.

  3. Jesper says:

    Give M-x delete-trailing-whitespace RET a go too ;)

Leave a Reply