[SOLVED] How to auto-remove double spaces in line...

Discuss the word processor

[SOLVED] How to auto-remove double spaces in line...

Postby Harry_Steele » Fri Nov 22, 2019 3:11 am

This is a follow-on from my previous question about removing page breaks, which was solved, thanks for that.

When I followed those suggestions I did get what I wanted, the lines were all correctly aligned in paragraphs.

However, there now remain several instances, in every paragraph, or hypenated words, presumably where those words occurred at the end of lines and carried over by hyphens. I could go through each and every one of course, but it's a 300-page book. so I'd love to know if there's a way of removing these. If I use the "find and replace" function, how can I replace a hyphen with a "space".

Thanks again for any suggestions. (And I'll remember to put "solved" in the heading if I fix it. ;) )
Last edited by robleyd on Fri Nov 22, 2019 11:04 am, edited 2 times in total.
Reason: Add green tick
OpenOffice 4.1.6 on MacOS 10.9
Harry_Steele
 
Posts: 14
Joined: Sun Nov 03, 2019 12:03 pm

Re: How to auto-remove double spaces in lines, in a paragrap

Postby robleyd » Fri Nov 22, 2019 3:56 am

Simply type a dash [ - ] into the Find box and in the Replace box press the space bar once.

It may be that the hyphens are not a simple dash; if this is the case you can simply copy one from your document and paste it into the Find box.
Cheers
David
Apache OpenOffice 420m2(Build:9821) - Slackware 14.2 - 64 bit
LibreOffice 6.0.7.3 - Slackware 14.2 - 64 bit
Apache OpenOffice 4.1.4 - Windows 7 Virtual machine
User avatar
robleyd
Moderator
 
Posts: 3368
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How to auto-remove double spaces in lines, in a paragrap

Postby RusselB » Fri Nov 22, 2019 4:18 am

Please note that, depending on your settings and the specific word, replacing the hyphenation character with a space may get you words that are incorrectly spelled.
The spell checker takes into account automatic hyphenation and recognizes hyphenated words as single words. Replacing the hyphen with a space will create two different words, which will (probably) seem to be incorrectly spelt.
I'm actually having a hard time coming up with some examples of words that would be correct when hyphenated and when split into two words.... One option I did come up with is the word "cannot"
If you have the word "cannot" at the end of a line with only 4 characters available, the hyphenation rules would make it "can-not", and replacing the - with a space would give you "can not"

There are, I suspect, more words that would appear to be spelt incorrectly using a space rather than a hyphen, than those that would actually end up making two properly spelt words. Take the word "properly" as an example.. it would be hyphenated as "prop-erly" or "proper-ly".. in either case, you get one word, the part before the hyphen is actually a correct word, but the part after the hyphen is not a correct word.

All of the above is based on the lessons I learned in school regarding the structure and usage of the English language.
OpenOffice 4.1.7 and LibreOffice 6.3.3.2 on Windows 7 Pro & Ultimate
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
RusselB
Moderator
 
Posts: 6045
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: How to auto-remove double spaces in lines, in a paragrap

Postby Harry_Steele » Fri Nov 22, 2019 6:36 am

robleyd wrote:Simply type a dash [ - ] into the Find box and in the Replace box press the space bar once.

It may be that the hyphens are not a simple dash; if this is the case you can simply copy one from your document and paste it into the Find box.


I tried that already, Rob, but it didn't do anything. :(
OpenOffice 4.1.6 on MacOS 10.9
Harry_Steele
 
Posts: 14
Joined: Sun Nov 03, 2019 12:03 pm

Re: How to auto-remove double spaces in lines, in a paragrap

Postby Harry_Steele » Fri Nov 22, 2019 6:42 am

RusselB wrote:Please note that, depending on your settings and the specific word, replacing the hyphenation character with a space may get you words that are incorrectly spelled.
The spell checker takes into account automatic hyphenation and recognizes hyphenated words as single words. Replacing the hyphen with a space will create two different words, which will (probably) seem to be incorrectly spelt.
I'm actually having a hard time coming up with some examples of words that would be correct when hyphenated and when split into two words.... One option I did come up with is the word "cannot"
If you have the word "cannot" at the end of a line with only 4 characters available, the hyphenation rules would make it "can-not", and replacing the - with a space would give you "can not"

There are, I suspect, more words that would appear to be spelt incorrectly using a space rather than a hyphen, than those that would actually end up making two properly spelt words. Take the word "properly" as an example.. it would be hyphenated as "prop-erly" or "proper-ly".. in either case, you get one word, the part before the hyphen is actually a correct word, but the part after the hyphen is not a correct word.

All of the above is based on the lessons I learned in school regarding the structure and usage of the English language.


I get what you're saying, Russel. However the words that are being hyphenated aren't (necessarily) words that would normally be hyphenated. That's why I assumed the hyphens had been inserted by the program when it had to split a word at the end of the line. That's bad practice for a person, but even worse for a word app!

Maybe if I disable spell check. Or one of the other settings, maybe? What do you think might have an effect, if it was disabled? Keep in mind, I'm fairly new to this program.
OpenOffice 4.1.6 on MacOS 10.9
Harry_Steele
 
Posts: 14
Joined: Sun Nov 03, 2019 12:03 pm

Re: How to auto-remove double spaces in lines, in a paragrap

Postby FJCC » Fri Nov 22, 2019 7:32 am

Can you upload a small part of the document that contains one or more of the hyphens that you want to replace? Just copy and paste a part of your document into a new file. To upload a file, click Post Reply and look for the Upload Attachment tab just below the box where you type a response.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to auto-remove double spaces in lines, in a paragrap

Postby robleyd » Fri Nov 22, 2019 8:05 am

I tried that already, Rob, but it didn't do anything.


I assume that you mean no matches were found by the search? Did you also try my suggestion of copying the character from your document into the search box?
Cheers
David
Apache OpenOffice 420m2(Build:9821) - Slackware 14.2 - 64 bit
LibreOffice 6.0.7.3 - Slackware 14.2 - 64 bit
Apache OpenOffice 4.1.4 - Windows 7 Virtual machine
User avatar
robleyd
Moderator
 
Posts: 3368
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How to auto-remove double spaces in lines, in a paragrap

Postby Harry_Steele » Fri Nov 22, 2019 10:47 am

robleyd wrote:
I tried that already, Rob, but it didn't do anything.


I assume that you mean no matches were found by the search? Did you also try my suggestion of copying the character from your document into the search box?


Yes, I meant that nothing whatsoever happened when I did that.

BUT, I'll be damned! That C&P of the actual character and following space trick worked, Rob. The document is now as it should be.

Thanks, all, for your kind attention and expert help. :bravo:
OpenOffice 4.1.6 on MacOS 10.9
Harry_Steele
 
Posts: 14
Joined: Sun Nov 03, 2019 12:03 pm

Re: How to auto-remove double spaces in lines, in a paragrap

Postby John_Ha » Fri Nov 22, 2019 10:50 am

Use a regular expression (see the Tutorial) to find the double spaces and replace them with a single space.

This macro finds all instances of multiple spaces and replaces them by a single space.

Code: Select all   Expand viewCollapse view
sub Spaces
'jh Converts multiple spaces to single space

'jh MsgBox(prompt, buttons, title)                                                             
'jh 1 means two buttons - OK (resp = 1) and CANCEL (resp = 2)

   response = MsgBox( "Converts multiple spaces to single space",1,"Converts multiple spaces to single space")

   If (response = 2) Then goto end_spaces           ' 2 = cancel, so do nothing and jump to end

   REM save cursor position
   oViewCursor = ThisComponent.getCurrentController().getViewCursor()
   oTextCursor = oViewCursor.Text.createTextCursorByRange(oViewCursor)

   REM disable screen update
   ThisComponent.lockControllers
   ThisComponent.CurrentController.Frame.ContainerWindow.Enable = False

'  REMEMBER - the called sub uses Regular Expressions, so be sure to use \. and not . for special chars 

   FindReplaceRegExp ("([:space:]+)"," ","all")                 '   + = 1 or more spaces, * = 0 or more
   
'jh  Remove search for regular expressions by running a dummy RESET search with them unticked
   FindReplaceReset ("abc","abc","all")                             ' change a abc to abc

   REM restore cursor position
   oViewCursor = ThisComponent.getCurrentController().getViewCursor()
   oViewCursor.gotoRange(oTextCursor,false)
   
   REM reenable screen update
   ThisComponent.CurrentController.Frame.ContainerWindow.Enable = True
   ThisComponent.unlockControllers
   
   end_spaces:    ' jump here if user cancels

End Sub

Sub FindReplaceRegExp (sFind,sReplace,sScope)

'jh  A standard macro for simplifying a Find and Replace search

   REM sFind: regular expression
   REM sReplace: if empty, function selects all occurrences of sFind
   REM sScope: set to "all" for entire document, or "selection" to restrict the scope.
   REM function searches backwards (necessary to correctly catch quotes)

   dim document   as object
   dim dispatcher as object
   document   = ThisComponent.CurrentController.Frame
   dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

   dim args1(18) as new com.sun.star.beans.PropertyValue
   args1(0).Name = "SearchItem.StyleFamily"
   args1(0).Value = 2
   args1(1).Name = "SearchItem.CellType"
   args1(1).Value = 0
   args1(2).Name = "SearchItem.RowDirection"
   args1(2).Value = true
   args1(3).Name = "SearchItem.AllTables"
   args1(3).Value = false
   args1(4).Name = "SearchItem.Backward"
   args1(4).Value = true
   args1(5).Name = "SearchItem.Pattern"
   args1(5).Value = false
   args1(6).Name = "SearchItem.Content"
   args1(6).Value = false
   args1(7).Name = "SearchItem.AsianOptions"
   args1(7).Value = false
   args1(8).Name = "SearchItem.AlgorithmType"
   args1(8).Value = 1
   args1(9).Name = "SearchItem.SearchFlags"
   If sScope = "all" Then
      args1(9).Value = 65536   ' parse entire doc
   Else
      args1(9).Value = 71680   ' parse selection only
   End If
   args1(10).Name = "SearchItem.SearchString"
   args1(10).Value = sFind
   args1(11).Name = "SearchItem.ReplaceString"
   args1(11).Value = sReplace
   args1(12).Name = "SearchItem.Locale"
   args1(12).Value = 255
   args1(13).Name = "SearchItem.ChangedChars"
   args1(13).Value = 2
   args1(14).Name = "SearchItem.DeletedChars"
   args1(14).Value = 2
   args1(15).Name = "SearchItem.InsertedChars"
   args1(15).Value = 2
   args1(16).Name = "SearchItem.TransliterateFlags"
   args1(16).Value = 1280
   args1(17).Name = "SearchItem.Command"
   If sReplace = "" Then
      args1(17).Value = 1      ' find all
   Else
      args1(17).Value = 3      ' replace all
   End If
   args1(18).Name = "Quiet"
   args1(18).Value = true
   dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

End Sub

Sub FindReplaceReset (sFind,sReplace,sScope)

'jh This sub FindReplaceReset is used merely to untick the Regular Expression and Backwards search boxes.

   REM sFind:     is not a regular expression
   REM sReplace:  if empty, function selects all occurrences of sFind
   REM sScope:    set to "all" for entire document, or "selection" to restrict the scope.
   REM function   searches forwards to untick the box

   dim document   as object
   dim dispatcher as object
   document   = ThisComponent.CurrentController.Frame
   dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

   dim args1(18) as new com.sun.star.beans.PropertyValue
   args1(0).Name = "SearchItem.StyleFamily"
   args1(0).Value = 2
   args1(1).Name = "SearchItem.CellType"
   args1(1).Value = 0
   args1(2).Name = "SearchItem.RowDirection"
   args1(2).Value = true
   args1(3).Name = "SearchItem.AllTables"
   args1(3).Value = false
   args1(4).Name = "SearchItem.Backward"
   args1(4).Value = false                      ' set to false to search forwards so as to untick the box
   args1(5).Name = "SearchItem.Pattern"
   args1(5).Value = false
   args1(6).Name = "SearchItem.Content"
   args1(6).Value = false
   args1(7).Name = "SearchItem.AsianOptions"
   args1(7).Value = false
   args1(8).Name = "SearchItem.AlgorithmType"
   args1(8).Value = 0                          ' set to 0 to untick regular expressions
   args1(9).Name = "SearchItem.SearchFlags"
   If sScope = "all" Then
      args1(9).Value = 65536   ' parse entire doc
   Else
      args1(9).Value = 71680   ' parse selection only
   End If
   args1(10).Name = "SearchItem.SearchString"
   args1(10).Value = sFind
   args1(11).Name = "SearchItem.ReplaceString"
   args1(11).Value = sReplace
   args1(12).Name = "SearchItem.Locale"
   args1(12).Value = 255
   args1(13).Name = "SearchItem.ChangedChars"
   args1(13).Value = 2
   args1(14).Name = "SearchItem.DeletedChars"
   args1(14).Value = 2
   args1(15).Name = "SearchItem.InsertedChars"
   args1(15).Value = 2
   args1(16).Name = "SearchItem.TransliterateFlags"
   args1(16).Value = 1280
   args1(17).Name = "SearchItem.Command"
   If sReplace = "" Then
      args1(17).Value = 1      ' find all
   Else
      args1(17).Value = 3      ' replace all
   End If
   args1(18).Name = "Quiet"
   args1(18).Value = true
   dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

End Sub
LO 6.3.5.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 7706
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: SOLVED: How to auto-remove double spaces in line...

Postby RoryOF » Fri Nov 22, 2019 11:01 am

When one is doing this sort of formatting revision, it is helpful to turn on /View /non printing characters to allow one see the low level formatting.

I find that most document revision, particularly of imported documents, can be done in moments using a small number of Find and Replace passes rather than a macro or F&R using Regular Expressions.
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31108
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: SOLVED: How to auto-remove double spaces in line...

Postby robleyd » Fri Nov 22, 2019 11:03 am

When you do a search, or search and replace, for a search term that isn't in the document, you should get a message that tells you "Search key not found", which isn't exactly "nothing happened".

oo_find_not_found.png
oo_find_not_found.png (10.44 KiB) Viewed 587 times
Cheers
David
Apache OpenOffice 420m2(Build:9821) - Slackware 14.2 - 64 bit
LibreOffice 6.0.7.3 - Slackware 14.2 - 64 bit
Apache OpenOffice 4.1.4 - Windows 7 Virtual machine
User avatar
robleyd
Moderator
 
Posts: 3368
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How to auto-remove double spaces in lines, in a paragrap

Postby Harry_Steele » Fri Nov 22, 2019 12:05 pm

John_Ha wrote:Use a regular expression (see the Tutorial) to find the double spaces and replace them with a single space.

This macro finds all instances of multiple spaces and replaces them by a single space.



Thanks for that, John. My problem is fixed now, but I've saved that code for future reference.

Cheers. :)
OpenOffice 4.1.6 on MacOS 10.9
Harry_Steele
 
Posts: 14
Joined: Sun Nov 03, 2019 12:03 pm

Re: SOLVED: How to auto-remove double spaces in line...

Postby John_Ha » Fri Nov 22, 2019 5:15 pm

RoryOF wrote:I find that most document revision, particularly of imported documents, can be done in moments using a small number of Find and Replace passes rather than a macro or F&R using Regular Expressions.

Indeed - repeated searches for two spaces, always replacing with one space, will fix all multiple spaces if continued until Search key not found is reached.

But macros are fun :-) and do it in one click when you have an icon on a toolbar. From the left they are convert all lone-hyphens to em dash, convert straight quotes to curly, remove End of line, remove New paragraph, convert multiple spaces to single space ...
Attachments
Clipboard01.png
Clipboard01.png (29.42 KiB) Viewed 545 times
LO 6.3.5.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 7706
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK


Return to Writer

Who is online

Users browsing this forum: No registered users and 16 guests