This is a repost, but it bears repeating since it's powerful, helpful, and not obvious.
(Originally published December 2005)
John V and Gabor at the OpenOffice.org Forum helped me find this solution. See also this blog on bringing text files into spreadsheets.
Call them carriage returns, line breaks, paragraph marks, whatever, sometimes you want fewer of them. Maybe you've brought in some ASCII text that had a line break or two after every paragraph and now with formatted text you don't need it. Or you're turning a spreadsheet or database into text or vice versa.
At any rate, it would be nice to use the Find and Replace window to quickly find'em and change them to whatever you want: nothing at all, or the phrase "el elegante" or whatever.
Note: If you're a macro kind of person, see this page on the ooo forum.
Searching and Replacing, Step by Step
In your OpenOffice.org document, choose Edit > Find and Replace or press Ctrl F. The Find and Replace window will appear.
In the Find and Replace window, enter the symbol for what you want to search for, in the Find field. Here's a quick reference to the symbols to enter for what you're looking for.
- Regular carriage returns are $
- Soft returns inserted with a Shift Return, are \n
- Just an empty paragraph, i.e. a carriage return but with no text on that line, is ^$
- Tabs are \t
In the Replace field, you typically don't enter anything since you're probably just trying to get rid of whatever you're searching for.
- If you want to replace something with a carriage return, put \n in the Replace field.
- If you want to replace one carriage return with two, put \n\n in the Replace field.
- One thing--you can't replace something with soft returns. As you see, a \n in the Replace field turns into a normal hard return.
- Just use \t normally, in both the Search and the Replace fields, for a tab.
Once your Find and Replace fields contain what they should, click the More Options button. Select the Regular Expressions checkbox. This will make the program look for what those codes represent, rather than literally those characters.
If you're using a mix of regular expressions and normal characters, you might need to use a \ in front of anything you want evaluated normally. For instance, if you really are looking for the symbol $ but you want to replace it with a carriage return \n, then you need to actually search for \$ in the Search field and replace it with \n because $ is a special character.
This illustration shows you're looking for a carriage return (any carriage return), and you're going to replace it with nothing.
Click Find. The first instance (from where the cursor was) of the thing you're looking for will be highlighted.
Click Replace to do the replacing.
And so on. Keep going until you're done. Use Replace All only when you're absolutely positive you'll get the results you want.
One thing to look out for using the ^$ string: if you have different paragraph styles on either side of the empty paragraph, one of those styles will become the other.
I usually Replace All with any long document, then scroll through and look for any anomolies especially where I have headings or blockquotes.
Posted by: pond | August 03, 2009 at 02:46 PM
Good point pond; different adjacent paragraph styles will be combined.
Posted by: Solveig | August 03, 2009 at 03:20 PM
Is there any way to make it faster? It took a lot of time when I tried it on a lengthy document.
Posted by: Shantanu Oak | August 03, 2009 at 09:17 PM
Hi Shantanu,
Just the usual; close all other programs and processes.
Posted by: Solveig | August 04, 2009 at 01:42 PM
How change 2 empty lines to one
Posted by: Sérgio Luiz Araújo Silva | September 01, 2009 at 12:16 PM
Is there any way to remove the soft carriage returns for only selected areas of a document? The intent is obviously to enable a line-length change to text containing soft carriage returns while leaving other portions of the text file untouched. It seems like the search \n or $ options are an all-or-nothing method.
Pico does it a paragraph at a time with control-j. That's what I'd like!
Posted by: bev ashley | September 03, 2009 at 04:36 PM
Hi Bev,
You can select nonconsecutive parts of the document (Ctrl select) and then apply the s/r to only selection. You might also be able to do it using more sophisticated regular expressions. Press F1 for the help and search for regular expressions; there's a bunch of stuff in there.
Posted by: solveig | September 03, 2009 at 05:27 PM
I assume control-select means control-select-with-the-mouse, right? Thanks, I'll try that next time I have one of the nasty text files to fix. I'm surprised that there isn't a one-step way to do it -- even WordStar had that facility!
Posted by: bev ashley | September 03, 2009 at 10:42 PM
There seem to be a lot of unexpected and unfortunate behaviors in this feature.
First of all, it should be possible to select a whole paragraph *except* for the last carriage return and use Replace All to eliminate all the other carriage returns. This would speed the process up considerably. (I don't think the user above was worried about an overloaded processor.) But I can't find any way to do it. No matter what I select it doesn't behave as I expect or want it to.
It should be possible (as the user above asked) to replace two empty lines with one. Nope. If I put ^$^$ in the search box, I get "Search key not found." And I get the same result with every variation I can think of, i.e., $$, ^$$, and $^$.
Another available strategy should be to (1) globally replace double returns with a unique string (like DOUBLERETURN); (2) globally replace all remaining (single) returns with a space (or nothing); and (3) globally replace DOUBLERETURN with two returns. Voila, you've stripped out all the returns except the ones you want, at the ends of paragraphs. But I see no way to do this with OO because it apparently can't be made to find a string of returns.
I've had to clean up scanned text where there were random numbers of extra returns, maybe as many as four or five in a row. The quickest way I know to do it is to globally replace three returns with two (or two with one, depending on the intended end result) and repeat this process until the search string is no longer found. But again, no go in OO.
This is the kind of thing that forces me back sorrowfully to Word. I'd love to ditch it, especially now that MS has forced the worst "upgrade" in recent history down my employer (and thus my) throat. But OO just doesn't seem ready for prime time, at least for power users.
Posted by: Drew Trott | September 05, 2009 at 06:35 PM
I might have missed it...but in doing my search and replace functions I often find that I need to replace "something" BEFORE a carriage return, with nothing. As an example often, I find that there are unwanted spaces before a carriage return, and when using a text file which will be subsequently imported into a database those spaces are problems. I have been looking now for about an hour for how to search for the combination of "space" and "carriage return" and not having luck. Perhaps the combination of hex characters representing the space and carriage return would work? Haven't tried that yet. Finding the "$" for searching for carriage returns was great...but after that I have been stymied.
Same comment as Drew above...this is the kind of thing that sorrowfully forces me back to Word, as it is easily done with a caret and some other letters and spaces, but OO is not making it intuitively difficult let alone easy, for me to do this.
Posted by: Jim Cole | October 10, 2009 at 09:22 PM
Yes, the power users need work to much with all forms of characters. To clean docs that comming from other sides and containing spaces before a carriage return when is not necesary; well, I hope see this soon in OO...I will not to use Word for ever.
Posted by: Pablo | October 23, 2009 at 03:52 PM
A Google search for this problem lead me here. This is an annoying problem - finding multiple paragraph marks (ie $$ etc). After reading the discussion I resorted to a desperate, un-elegant, but successful solution. With regular expressions enabled, search for $ (one paragraph mark) and globally replace this with XXXX (four Xs); repeat this until "Search string not found". Now the solution is painful, but effective: search for XXXXXXXX and globally replace with \n. Finally, search for XXXX and globally replace with \n. Obviously you can use something other than XXXX. As I said, painful, but effective.
Posted by: Bill | December 19, 2009 at 04:10 PM
Bill's solution didn't quite work for what I want to do, but it is better if the last S/R is XXXX replaced by a blank space.
My own interest is in moving from .txt files with a $ at the end of each line, $$ at end of paragraphs, to simple paragraphs that I can then shift into readable HTML. So Bill's $ -> #, repeated, then ## -> \n, # -> blank, and later $ -> \n works for me. Pity it's easier in Word, but I'm glad it's possible in OO. (Saving the other way round, however.....?)
Posted by: Ed Brandon | January 13, 2010 at 11:23 AM
Where I have "So Bill's $ -> #, repeated, then ## -> \n, # -> blank, and later $ -> \n works for me" what I actually wrote had bits of HTML in it which doesn't seem to want to be reproduced. So later was a matter of replacing the carriage returns with the HTML paragraph code in reverse order for my convenience (diamond bracket backslash p, diamond-bracket diamond bracket p diamond bracket) with a carriage return in between.
Posted by: Ed Brandon | January 13, 2010 at 11:31 AM
When I use the $ to search for carriage returns or paragraph marks I get nothing. What am i missing here?
Posted by: Edward | February 18, 2010 at 01:41 PM
"One thing--you can't replace something with soft returns. As you see, a \n in the Replace field turns into a normal hard return."
Can this be true? If so, it's another reason I'm considering going back to MS Word (which I *really* want to avoid, but I need to use whatever tool will get the job done).
The distinction between a paragraph mark and a line-break is SO fundamental and basic, I can't believe that OO Writer doesn't let you treat them differently in Search-and-Replace.
In case anyone's interested, here's why I need it. It is conventional when transcribing handwritten documentary sources to indicate line-breaks with a slash: "/". But now I want to replace these marks by real line-breaks in a long document. Apparently Writer will only let me replace them with paragraphs. Really dumb! Or am I missing something?
Posted by: Peter | August 03, 2010 at 10:11 AM
Hi Peter,
Try this extension.
http://openoffice.blogs.com/openoffice/2010/07/fantastic-openofficeorg-writer-extension-for-searching-and-replacing-carriage-returns-two-in-a-row-e.html
Solveig
Posted by: Solveig | August 03, 2010 at 02:31 PM