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