Auto-formatting tool for removing double spaces

Discuss the word processor
Post Reply
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Auto-formatting tool for removing double spaces

Post by prr »

I often paste content from the web into a word processor, and would like to be able to eliminate double- and even triple-spacing between paragraphs in one fell swoop. Is there any such autoformatting command that I can use that would accomplish this? I didn't see anything in the OO user guide. I looked in the autocorrect options, and only saw in the Options tab, the option to remove blank paragraphs. Is this what I want to do? Or is there another option I can use? Or would I need a macro of some kind?
LO 7.1.6.2 on Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Auto-formatting tool for removing double spaces

Post by RoryOF »

Find and Replace will do the job. Press the Replace all button
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Auto-formatting tool for removing double spaces

Post by acknak »

I often use something like this:

Edit > Find & Replace
Search for: [:space:]+
Replace with: space
Options: Regular expressions: YES

Click "Replace all"

That will replace one or more consecutive space characters of any kind (including things like non-breaking spaces) with exactly one plain space.

Stuff that's pasted from the 'Net has all kinds of oddball junk--beside the extra spaces--in it. It could be pretty handy to have a nice way to handle it.
AOO4/LO5 • Linux • Fedora 23
User avatar
Robert Tucker
Volunteer
Posts: 1250
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Re: Auto-formatting tool for removing double spaces

Post by Robert Tucker »

prr wrote:I often paste content from the web into a word processor, and would like to be able to eliminate double- and even triple-spacing between paragraphs in one fell swoop.
Do you mean it's creating extra paragraphs breaks, inserting blank paragraphs? You can eliminate these easily by installing AltSearch and searching for \p\p\p and replacing with \p\p. You might also want to look at this macro:

http://www.oooforum.org/forum/viewtopic.phtml?t=6429
LibreOffice 7.x.x on Arch and Fedora.
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Re: Auto-formatting tool for removing double spaces

Post by prr »

Installed AltSearch but looking for \p\p didn't turn anything up. Not sure what the problem is. One click in MS Word does autoformatting which takes care of all of this for me. Hopefully I'll stumble across something here...
LO 7.1.6.2 on Windows 10
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Re: Auto-formatting tool for removing double spaces

Post by prr »

acknak wrote:I often use something like this:

Edit > Find & Replace
Search for: [:space:]+
Replace with: space
Options: Regular expressions: YES

Click "Replace all"

That will replace one or more consecutive space characters of any kind (including things like non-breaking spaces) with exactly one plain space.

Stuff that's pasted from the 'Net has all kinds of oddball junk--beside the extra spaces--in it. It could be pretty handy to have a nice way to handle it.
Can you explain exactly what you type in? What I typed in didn't come up with any hits. What do you type in with the search for, and replace with? The colon and plus sign confused me...
LO 7.1.6.2 on Windows 10
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Auto-formatting tool for removing double spaces

Post by acknak »

You should type in the "Search for:" box exactly the bold text in my post:
  • [:space:]+
In fact, you can copy/paste the blue text above into the box.

Type one space character in the "Replace with:" box.

Make sure that the "Regular expressions" option is on; you have to click on the "More Options" button to see it.
AOO4/LO5 • Linux • Fedora 23
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Auto-formatting tool for removing double spaces

Post by JohnV »

would like to be able to eliminate double- and even triple-spacing between paragraphs in one fell swoop.
I just wrote a macro to do this a week ago for another person with the same request.

Code: Select all

Sub StripEmptyParagraphs
b = CInt(InputBox("Entry maximum empty paragraphs ( 0 is a valid entry ).",,1))
oDoc = ThisComponent
FandR = oDoc.createReplaceDescriptor
FandR.SearchRegularExpression = true
FandR.setSearchString("\n")
FandR.setReplaceString("\n")
oDoc.ReplaceAll(FandR) 'Change line breaks to paragraph breaks
oTC = oDoc.Text.createTextCursor
While oTC.isEndOfParagraph 'Remove blank paragraphs from top of doc.
  oTC.goRight(1,true)
  oTC.String = ""
Wend 
Do While oTC.goToNextParagraph(False)
 p = p + 1
Loop
oTC = oDoc.Text.createTextCursor
Do While oTC.gotoNextParagraph(false)
 pc = pc + 1
 If oTC.isEndOfParagraph then c = c + 1
 If c > b then
  Do While oTC.isEndOfParagraph
  oTC.goRight(1,true)
  oTC.String = ""
  lc = lc + 1
  If lc > p then exit do
  Loop
  lc = 0 
  c = 0
 EndIf
 If pc > p then exit do
Loop
End Sub
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Re: Auto-formatting tool for removing double spaces

Post by prr »

The macro works on some of these spaces between paragraphs, but not all. See, for example, this document. It will remove only a few of the large gaps.

EDIT: Acknak, thanks for clarifying your actions, but the same thing--on the attached doc, the vast majority (if not all) the spaces are not removed.
Attachments
Versailles.rtf
(49.74 KiB) Downloaded 397 times
LO 7.1.6.2 on Windows 10
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Auto-formatting tool for removing double spaces

Post by JohnV »

When I open Versailles.rtf with OOo it is using a paragraph style with spacing both above and below the paragraph. Do Ctrl+A and apply the Default paragraph style and this spacing will go away.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Auto-formatting tool for removing double spaces

Post by RoryOF »

In Text Body style spacing above and below the paragraph is set to 0.42cm (say 12 pts). You should check the Help file on Styles and how to modify them.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Re: Auto-formatting tool for removing double spaces

Post by prr »

RoryOF wrote:In Text Body style spacing above and below the paragraph is set to 0.42cm (say 12 pts). You should check the Help file on Styles and how to modify them.
The spaces I'm talking about are a lot greater than 2/5 of a cm.
LO 7.1.6.2 on Windows 10
prr
Posts: 235
Joined: Wed Feb 04, 2009 5:37 pm

Re: Auto-formatting tool for removing double spaces

Post by prr »

JohnV wrote:When I open Versailles.rtf with OOo it is using a paragraph style with spacing both above and below the paragraph. Do Ctrl+A and apply the Default paragraph style and this spacing will go away.
OK that is one quick solution when this happens---select all, then apply a paragraph style that has no (or just limited) spacing before and after paragraphs. That actually wouldn't even require a macro....
LO 7.1.6.2 on Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Auto-formatting tool for removing double spaces

Post by RoryOF »

prr wrote:
RoryOF wrote:In Text Body style spacing above and below the paragraph is set to 0.42cm (say 12 pts). You should check the Help file on Styles and how to modify them.
The spaces I'm talking about are a lot greater than 2/5 of a cm.
They are most probably 0.42 cm after the para above and 0.42cm before the para below. Modify your Text Body Style to remove these two settings and see where that gets you.
 Edit: Also, turn ON Display Formatting characters and look at your file again. There are several extra paragraph marks which need to be removed. 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Murph
Posts: 16
Joined: Thu Mar 18, 2010 2:12 pm

Re: Auto-formatting tool for removing double spaces

Post by Murph »

I am having the opposite problem - I have text with a mixture of single and double spacing and I would like to make sure it is all double spaced. I tried replacing ". " with ". " but this adds the extra space to an existing double space. Is there a code for a non-space character? e.g. say * = represents any letter or number I could replace ". *" with ". *"

Thanks

Murph
OOo 3.2.1, WinXP
User avatar
Robert Tucker
Volunteer
Posts: 1250
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Re: Auto-formatting tool for removing double spaces

Post by Robert Tucker »

Murph wrote:I am having the opposite problem - I have text with a mixture of single and double spacing and I would like to make sure it is all double spaced. I tried replacing ". " with ". " but this adds the extra space to an existing double space. Is there a code for a non-space character? e.g. say * = represents any letter or number I could replace ". *" with ". *"
Search: ([^:space:]) ([^:space:])
Replace: $1 $2
(Regular expressions checked, of course)

for some reason has a problem with "I"s.

You could replace all spaces by two spaces then repeatedly replace three spaces by two spaces until no more three spaces are found.
LibreOffice 7.x.x on Arch and Fedora.
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Auto-formatting tool for removing double spaces

Post by acknak »

Search: ([^:space:]) ([^:space:])
I don't think does what you expect it to do :shock:

I think it should work if you just use plain spaces in the pattern: ([^_])_([^_]) (where the "_" represents a plain space character).
AOO4/LO5 • Linux • Fedora 23
User avatar
Robert Tucker
Volunteer
Posts: 1250
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Re: Auto-formatting tool for removing double spaces

Post by Robert Tucker »

acknak wrote:
Search: ([^:space:]) ([^:space:])
I don't think does what you expect it to do
No. I thought it would find any character except a space separated by a space from any character except a space. It does but not when an "I" is involved. I'd be interested in the explanation for that.
acknak wrote:I think it should work if you just use plain spaces in the pattern: ([^_])_([^_]) (where the "_" represents a plain space character).
Yes, I'd tried with (^ ) (^ ) but didn't have any success, of course.
LibreOffice 7.x.x on Arch and Fedora.
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Auto-formatting tool for removing double spaces

Post by acknak »

It does but not when an "I" is involved. I'd be interested in the explanation for that.
Robert Tucker wrote:
acknak wrote:
Search: ([^:space:]) ([^:space:])
I don't think does what you expect it to do
No. I thought it would find any character except a space separated by a space from any character except a space. It does but not when an "I" is involved. I'd be interested in the explanation for that.
Anything other than "[:space:]" is not recognized as the named class. So [^:space:] is a plain, ordinary character class: it matches any character except the letters s, p, a, c, e or colon.

At least I think that's what happens--does that square with your "I" example?
AOO4/LO5 • Linux • Fedora 23
User avatar
Robert Tucker
Volunteer
Posts: 1250
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Re: Auto-formatting tool for removing double spaces

Post by Robert Tucker »

acknak wrote:...does that square with your "I" example?
Looking at it again it seems more than the "I"s are affected. I get this result for Search for: ([^:space:]) ([^:space:]) Replace with: $1 $2.

Before:
space_replace-01.png
space_replace-01.png (6.68 KiB) Viewed 36816 times
After:
space_replace-02.png
space_replace-02.png (6.84 KiB) Viewed 36816 times
From your explanation I can understand that it doesn't change to a double-space between "opposite" and "problem". But why no double-space between "I" and "am" and "the" and "opposite" and so on?
LibreOffice 7.x.x on Arch and Fedora.
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: Auto-formatting tool for removing double spaces

Post by Bill »

There's another problem when single non-space characters are both preceded by a space and followed by a space. During a first run of Search and Replace, the combination of a character-space-"I" is found and replaced. The search then continues with the space following "I", so the combination of "I"-space-character isn't found during the first run. Search and Replace has to be run a second time to find the "I"-space-character combinations.
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Auto-formatting tool for removing double spaces

Post by acknak »

... why no double-space between "I" and "am" and "the" and "opposite" and so on?
The entire pattern has to match--if either one of the characters around the space matches the excluded class, the whole pattern fails to match. So "I a" doesn't match because "a" is a member of the excluded set; "e o" because "e" is excluded.

The problem posed is tough to do with a regular expression for English prose because English has complicated rules for punctuation. It often requires some analysis of the context to determine for sure where the end of a sentence is--at least for the general case.
AOO4/LO5 • Linux • Fedora 23
bro.norman
Posts: 1
Joined: Sun Aug 19, 2012 3:04 pm

Re: Auto-formatting tool for removing double spaces

Post by bro.norman »

Acknak's remedy resolve the same issue in LibreOffice 3.5. I was searching for the the tag in LibreOffice and after applying all of it, nothing worked. I know OpenOffice uses the same source code as LibreOffice so I googled the tag, this time instead of LibreOffice, I used OpenOffice. I tried Acknak''s remedy which was slightly different than that in LibreOffice' community. It worked. Thanks acknak.
LibreOffice 3.5
nikkitytom
Posts: 17
Joined: Sun Dec 16, 2012 3:17 am

Re: Auto-formatting tool for removing double spaces

Post by nikkitytom »

[quote="acknak" quote] Thanks so much. I’ve spend days looking for this solution. Typing [:space:]+ into the Find All box works like a charm. But ‘typing one space character’ into the Replace All box needs clarification. There’s no “character” for one space, instead you have to just press the space bar once ... be careful that the cursor is in position before you press that space bar. Only ONCE. What is confusing is that the space bar doesn’t show anything in the Replace All box. The cursor has merely advanced one space.
This works like a charm, but I’ve pulled out half my hair searching for it. :bravo:
OpenOffice 3.1 on Windows Vista
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: Auto-formatting tool for removing double spaces

Post by Bill »

A space is a character. It's the first "printable" character on the list of ASCII characters.

ASCII

ascii codes
AOO 4.1.14 on Ubuntu MATE 22.04
nikkitytom
Posts: 17
Joined: Sun Dec 16, 2012 3:17 am

Re: Auto-formatting tool for removing double spaces

Post by nikkitytom »

Sorry, I stand corrected ... I was confused by the blank space. I followed your link ASCII and it is as complex String Theory. There are so many queries regarding this simple operation that I'm guessing I'm not alone in my befuddlement. "Hit the Space Bar once" would have solved the problem.
OpenOffice 3.1 on Windows Vista
Post Reply