Using “Query replace regexp” to remove blank lines from a file in Emacs

This guy took years just to discover how to use a carriage return in an EMACS regex search. Yep… just do C-M-s to fire up the search… then ^ C-Q C-J and you’re rocking. Nifty.  For some reason I didn’t think it would work for a while, but the same trick does indeed work from regex search and replace (C-M-%) as well.  How long does it take me to figure out how to apply this trick to an editing problem? It ended up taking… well… more than an hour. (Grumble grumble….) Never mind how long it took for me to realize that this was the key to solving the problem! (Grumble grumble grumble….)

In the hopes of preventing similar pains in other peoples lives, I will explain my discoveries….

What I wanted to do is delete all of the blank lines in a file. C-c C-o just kills the next chunk of whitespace that follows the line you’re in. I’d like it do the entire file….  (Picky me.)  Many a google a search will lead you to bark up the wrong tree, but Derek Slager’s cool screencast was the place to go all along.

Drop into regex search and replace with C-M-% and for the “where” part, type C-q C-j C-q C-j +. (That’s two carriage returns followed by a plus sign.) For the “with” part type C-q C-j and press return. (That’d be a single carriage return.) What we’re doing is replacing each chunk of 2 or more carriage returns with a single carriage return.

An alternative solution would be to replace ^ C-q C-j with “nothing”.  (The caret matches all lines that match the text that follows it.)

Pretty cool!

I forgot what I was trying to do before I got sucked into this, but now that I’ve solved this minor gotcha there’s nothing to prevent me from mastering Emacs in another ten years or so! Yee-ha!

Too bad I’ve got carpal tunel syndrome….  (Oh the pain!)

About these ads

6 Responses to “Using “Query replace regexp” to remove blank lines from a file in Emacs”

  1. Fidel Says:

    Thanks – I was looking right for this aticle to get better feeling for emacs.
    Sorry for the carpal tunnel syndrome.

    Fidel

  2. marko Says:

    cool, was looking for that as well! tnx mate

  3. How NOT to fix your Delete key in EMACS « Learning Lisp Says:

    […] How NOT to fix your Delete key in EMACS I’ve been run emacs off of a weird server. I was running x on cygwin and had used “xhost MYSERVER” to pick up the emacs session. Cool. The version of Emacs 20.7.2, though. Anyways… I get there and backspace and the delete key both work by backing over the previous character. I wanted to make the delete key “eat” the next character. This should be easy to fix, right? Doh! It took me more than an hour. Below is my lame trial and error process of finding a lame solution. I haven’t had this much fun since I had to figure out how to enter a carriage return! […]

  4. Crystal Says:

    awesome! you rock! Thanks for the tip!

  5. ignacio paz posse Says:

    It might be useful to know that another way to remove all the blank lines is to use a perl regex within emacs (which is great for a lot of tasks)

    1) select all (c-x h)
    2) do M-x (or Alt – x, in windows) “Shell command on region:”,
    3) then type ” perl -pe ‘s/$\n^//g’ and press enter.
    4) voila!

  6. benlr Says:

    M-x flush-lines ^$

    is much simpler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: