Change spaces at right margin to line break

I need to take documents convert them to plain text, remove all the hidden characters except spaces, set a right margin and finally replace all the spaces right before the margin with line breaks. I have the first three worked out however none of my searches have given me a regular expression(s) that will replace the spaces just before the right margin with line breaks.

Thanks in advance.

Just so it is clear, you want to achieve this?

You could find the first space after a certain number (90) of characters
Find: (.{90})(\s)
Replace: $1\n

But that will give paragraph breaks. You could instead replace with a token and then use AltSearch to replace the token with a line break. For some reason my older version of AltSearch is baulking at the find.

1 Like

Here’s a sample of what I’m looking for. The AltSearch example almost works.

Thanks

I’d rather suppress the paragraph breaks at end of line (except where it is a real paragraph break) and adjust right indent of the paragraph style.

Looking carefully at the screenshot, I notice that character encoding is misinterpreted: plain text is obviously UTF-8 but displayed as some ISO-8859-x variant. So there is already something wrong with the file.

I don’t know what this means precisely. Notepad++ or other text editor seems to be the proper tool but OP is using Writer.

I thought you want line breaks, not paragraph breaks. The expression I gave earlier will work in Writer’s own Find & Replace dialogue if you change the number to 18

If you want line breaks then use AltSearch, I tested this after downloading the latest version:
Find: (.{18})(\s)
Replace: \1\n

So the gibberish text is from a test document and irrelevant. I’m on a quest to do this because I need to reformat these files for teleprompter software so that there is less to do in the actual teleprompter app. The (.{18})(\s) example is selects text about midline and therefore puts the line breaks in the wrong place.

Thanks

Okay I think it works. My problem was that I hadn’t reset the margins and it was giving me mixed results more on the side of what I was viewing.

Thanks

If 18 is too many characters, change it to a lower number.
If you don’t mind cutting a word at a random place then don’t include the (\s) to get an exact number of characters

The margins are not content of the text, but defined by the page style, probably modified by the paragraph style or manual indentations.
Therefore no standard means of searching (F&R with Regex e.g.) can find them. Also the old extension AltSearch doesn’t allow for such a search.
A workaround based on counting characters requires the text to be in monospace at least - and is a doubtable crutch anyway.
Thus you will have to write special user code - or find a more intelligent teleprompter software.
= = Editing = =
Don’t see clear progress.
Therefore I sketched a little Basic Sub showing with what you could start.
With a simple example it worked for me.

Sub replaceAsTechnogeekcaWants()
REM Should replace any single space at the end of a line with a hard linebreak.
REM Staring at:
vCur = ThisComponent.CurrentController.ViewCursor
'On Error Goto fail
Do While True
 vCur.gotoEndOfLine(False)
 vCur.goLeft(1, True)
 If vCur.String=" " Then vCur.String = Chr(10)
 vCur.goRight(1, False)
 If vCur.isAtEndOfLine() Then Exit Do
Loop
fail:
End Sub

fmt(1) - Linux manual page