Search & Replace unwanted single leading space only

Hi. I have an operation I need to do quite frequently - replace a blank space at the beginning of a line. I can do this with search for '^ ' (^ + space) & replace with [left blank]. However, I need it to replace only one space; some lines have more than one space, and I need to keep the extra spaces. For example (without the quote marks):

" This line has an unwanted space at the start"
"     This line has 5 spaces - 4 are wanted"
after s&r:
"This line has an unwanted space at the start"
"    This line has 5 spaces - 4 are wanted"

And whats the Problem?…your search&replace procedure does exactly what you ask for!

edit: ok I see the Issue…»replace allĀ« consumes recursivly any space at the Start of paragraph. Solution: … simply tick the Button Ā»find allĀ« and then the button Ā»replaceĀ«

2 Likes

The Find works perfectly, however the Replace still works recursively. :frowning:

hm… not here with:

Version: 7.0.4.2
Build ID: 00(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: de-DE (de_DE.UTF-8); UI: de-DE
Raspbian package version: 1:7.0.4-4+rpi1+deb11u3
Calc: threaded

I’m using Version 7.4.5.1

@karolus I tried with 7.0, and it works ā€œrecursivelyā€ there as well.
Which IMO is a bug. The replacement must first find all matches in the paragraph, and then replace them at once.

+1
yes »replace all« should trigger exactly ONE Iteration through Document, but not until it can not consume any search-pattern.

tdf#153408

Ok this should work:

Search for:

^ (.*)$

replace with:

$1

→→»replace allĀ« !

1 Like

It works!!!
Many thanks. :grinning:

But this would still ā€œstealā€ one if the 5 spaces in his example. So, if he repeats this replace he will loose his spaces over time.
.
One solution could be to put the space back in a second step for all lines, wich still have a space in front.
.
Another would be to use a tab in that places, as multiple spaces forbformatting/indentation(?) does not fit in the principles of Writer anyway.
.
Last idea would be a 3 step procedure: Protect two or more spaces at beginning by replacing with a marker like # or |, wich is not in the file, replace leading spaces, then remove the marker.
.
But as he wrote ā€œit worksā€ in the time I wrote this I guess he needs not to

for the same file, but for different files.

That’s correct; ā€˜stealing’ the one space is a desired outcome, as long as it is only the one. And the routine needs to be run only once for a file.

… and saved as a macro :grinning:: Once again, thanks!