Removing spaces before paragraph breaks without affecting formatting

I’m working in Scrivener (Mac 3.5.2) and trying to clean up trailing spaces before paragraph breaks across imported text.

What I want to do:

  • Find spaces that occur immediately before a paragraph break

  • Remove only those spaces

  • Preserve all paragraph breaks and formatting

What I’ve tried:

  • Regex find/replace using patterns like \s+\n and +\n

  • Replace with \n

Either nothing is found, or paragraph breaks get removed/altered, causing formatting issues

Is there a reliable way in Scrivener to remove trailing spaces before paragraph breaks without affecting the paragraph structure?

Have you tried this one yet?

\s+$

With this approach you aren’t including the paragraph break itself in the search pattern, so you don’t have to worry about restoring it in the replacement field—you can just leave that blank. One thing to be aware of is that “EOL” in a rich text editor includes line breaks as well as paragraph breaks—which might work to your favour if you do use those.

But if you do need to distinguish, then I’d go with:

\s+(?=\n)

That won’t match line breaks, and since it is in a look-ahead clause it isn’t technically part of the replacement pattern, meaning you can go on leaving it empty.

2 Likes

If you are pasting text from elsewhere, esp. text copied from a web page, you might often be better off using Paste and Match Style, instead of regular Paste. This will drop out lots of formatting stuff you probably don’t want. It is often a good idea to use Zap Gremlins on internet-sourced text, too, to eliminate invisible character codes that can cause anomalous behavior.

1 Like

Thank you. I gave it a try but it said “not found.” (I was using selected text where the problem occurs)

I tried this: \s+(?=\n)
This found the issues but also got rid of all the paragraph spacing that’s intentional, not just the extra space.

I was using Living Writer and exported my manuscript as a .docx that I then brought into Scrivener. Now I’m trying to clean up all the artifacts and things I put in without realizing, and make clean style sheets.

Can I use one of these third party cleanups inside Scrivener? I have TextSoap if that helps.

Also, I have 3 years that are separate manuscripts. The one I already imported, I can clean up manually. For the other two, it sounds like it might be easier to do the cleanup via TextSoap in the Word doc first and then import into Scrivener. Correct?

If you have cases of multiple blank lines that you need to preserve, you can use \h+(?=\n) to trim the extra whitespace at the end of a paragraph without selecting any following empty lines. (\h only matches horizontal spaces, whereas \s will include new lines.)

That said, the RegEx search for \s+$ (or \h+$) should have worked even within a selection since it’s a broader scope than \s+(?=\n) and would’ve caught any cases of an accidental line break instead of new line. Could the focus have shifted accidentally to the wrong editor? Or if you turn on View ▸ Text Editing ▸ Show Invisibles, is there something else showing that might be interrupting the pattern?

2 Likes

Also make sure regular expression is turned on to begin with, via the dropdown in the Find Options area!

1 Like

\h+(?=\n)

That’s the one! It worked really well. Thank you so much. I’m going to test it on several documents first and if it’s good, I’ll run it through the whole manuscript.

1 Like