12

Often I find that I copy text from a gedit (linux) text editor document into a word processor like LibreOffice Writer. Of course I have hard linebreaks coded into the text.

How can I do a search and replace to reflow the text in LibreOffice Writer? In other words, I need the linebreaks removed.

Edit: Since it was suggested, I will add that I did tick regular expressions and being an old programmer I did use \n just in case it has any meaning for LibreOffice. The search key was not found when I opted for the find-and-replace.

Markus
  • 121
  • 7
H2ONaCl
  • 1,396
  • 3
  • 20
  • 35
  • May I ask what LibreOffice version do you use? With version 4.0 I also got the error "search key not found" [where it is working with version 3.6](http://superuser.com/questions/625093/how-to-convert-text-to-numbers-for-all-cells-in-a-spreadsheet/625152#625152). – nixda Aug 10 '13 at 08:15
  • AutoCorrect has a setting that will remove the new lines that I paste in from Emacs (they become paragraph breaks in Writer): https://help.libreoffice.org/Writer/Removing_Line_Breaks – Qsigma Oct 05 '15 at 19:29

3 Answers3

11
  1. In the Edit > Find and Replace dialog, tick "Use regular expressions", put "\n" in the 'search for' box, nothing in the replace with box, click Replace all. This removes line breaks that you can insert with Shift+Enter (manual line breaks). Here's a link to LibreOffice documentation on regular expressions.
  2. LibreOffice Writer can't replace paragraph breaks by default. Use the AltSearch add-on for OpenOffice, as it also works with LibreOffice.
  3. To do the paste-and-remove-linebreaks automatically, you can try recording a macro (help.libreoffice.org/Common/Macro), just paste and perform the replace and have LibreOffice record them in a macro.
jokerdino
  • 2,465
  • 1
  • 23
  • 33
  • 3
    I did think of using `\n` and I get "search key not found" when I search for `\n`. – H2ONaCl Aug 29 '12 at 08:54
  • 1
    This means you have no **line breaks** in the document. Use (2) to replace **paragraph breaks**. –  Aug 31 '12 at 04:10
  • In my case, using Find and Replace with regular expressions, and then searching for \n gives no matches, but searching for [:cntrl:] (a nonprinting character according [1]) gives me matches. Not sure what character is matching [:cntrl:], maybe is not a line break. [1] https://help.libreoffice.org/6.4/en-US/text/shared/01/02100001.html – Pin Mar 12 '20 at 18:21
  • Ok, after some investigation, I improve my previous comment: If search for \n don't work because there is other non printable character you can also search for [:cntrl:] which finds ASCII non printable charcters (codes from 000 to 037 and 177 ) that means backspace, carriage return, etc ... [1] https://help.libreoffice.org/6.4/en-US/text/shared/01/02100001.html [2] https://www.gnu.org/software/grep/manual/html_node/Character-Classes-and-Bracket-Expressions.html [3] https://en.wikipedia.org/wiki/ASCII – Pin Mar 12 '20 at 18:41
3

The answer you are looking for is to do find and replace, find $ and then replace with 1 blank space

Anon
  • 31
  • 1
2

Realising that \n in the search and \n in the replacement part mean different things is helpful to know.

\n in the search matches "hard returns" - what you get if you type Ctrl-Enter.

\n in the replacement means "end of paragraph".

In my case, I was struggling to replace lines where the visible paragraphs were all in fact just one paragraph with blank lines encoded as two "hard returns". This became obvious by turning on visibility of non-printing characters in the View menu.

So in my case, since my paras were like this (denoting "hard return as {ent}" and end of para as {eop}):

Para1{ent}{ent}Para2{ent}{ent}Para3{eop}

So a Find all then a Replace All of:

\n\n

by

\n

(with regexp turned on), changed that to:

Para1{eop}Para2{eop}Para3{eop}

It may also help to know that you can search for an extra empty paragraph by searching for $^ (actually, ^$, too).

Stephen Rauch
  • 3,091
  • 10
  • 23
  • 26
LukeJKendall
  • 41
  • 1
  • 5