//
you're reading...
emacs

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

Discussion

4 thoughts on “Delete blank lines with flush-lines

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

    Posted by hober | October 22, 2007, 6:31 pm
  2. 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.

    Posted by Ruslan Spivak | October 22, 2007, 7:34 pm
  3. Give M-x delete-trailing-whitespace RET a go too ;)

    Posted by Jesper | February 20, 2008, 9:04 am

Trackbacks/Pingbacks

  1. Pingback: Emacs – Shortcut (include File Management) - December 26, 2011

Leave a comment

Categories