Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Remove Trailing Whitespace without removing extra lines

Community Expert ,
Jan 31, 2017 Jan 31, 2017

InDesign has a buit-in GREP Find/Change Query called Remove Trailing Whitespace which is:

\s+$

It's more powerful than I want. It finds/removes spaces, tabs, etc at the end of paragraphs, but also extra linespaces. Is there a GREP search that will find/remove all the whitespace (spaces, tabs, etc) at the end of a line, but ignore extra linespaces?

Thanks in advance.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
5.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 31, 2017 Jan 31, 2017

\h+(?!\n)$

Translate
Adobe Employee ,
Jan 31, 2017 Jan 31, 2017

Hi,

Does this link help? it has several grep expressions that can be used.

https://www.rockymountaintraining.com/adobe-indesign-removing-unwanted-spaces-fast/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Thanks, but no. That post refers to the built-in Remove Trailing Whitespace query that I mentioned does not work for my needs.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 31, 2017 Jan 31, 2017

What if simply exclude carriage return?

\s+[^\r]$

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 31, 2017 Jan 31, 2017

Your reply disappeared for me...

However, here's "improved" version:

\h+[^\r]$

it works on my side...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

winterm​ Your suggestion of \h+[^\r]$ does also work for me. As always, there are multiple ways to do things with GREP .


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

winterm​ I tried your suggestion of \s+[^\r]$ but it was not working as expected.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 31, 2017 Jan 31, 2017

Oooh, it was a hard day....

Simple \h+$ is just enough, blind me!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

The \h is nice, I didn't know about that! I had to look it up, so for anyone else that doesn't know:

\h is horizontal whitespace

\v is vertical whitespace (finds both hard and soft returns)

I'd probably use +? instead of + for shortest match. Not sure if it's necessary, but it would be safer, right?

So to break \h+?$ down:

\h = horizontal whitespace

+? = repeat one or more times (shortest match)

$ = at end of paragraph

That works very well and I like the elegance of it, but if there are soft returns, it also removes the space before the soft return.

When using \s*?(?=\r) I found it does not remove the spaces before soft returns, which is a little better for me. So far that looks like the winner for my exact needs.

Thanks everyone for the help! I always enjoy learning something new and hope others can benefit too.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2017 Jan 31, 2017

\h+(?!\n)$

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 31, 2017 Jan 31, 2017

Yeah, I’m with Obi-wan here. You should use 'horizontal space', not 'any white space', anyway.

Negative Lookahead works nicely. And no need for 'shortest match', IMHO.

Your 'working' regex \s*?(?=\r) finds a lot of false positives to me (well, harmless), just use Find Next to see...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

Yes, I agree too. Obi-wan Kenobi​'s is the best so far.


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 03, 2022 Oct 03, 2022

@Obi-wan Kenobi this is perfect for what I want to implement but the problem is when I'm adding this in script my InDesign crash

var doc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = "\\h+(?!\\n)$";
app.changeGrepPreferences.changeTo = "";
app.activeDocument.changeGrep();

app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

 

NewInScript_0-1664782958429.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2017 Jan 31, 2017

I'm not sure why the reply didn't get posted here, but I got an email answer that worked:

[[:space:]]*?(?=\r)

I shortened that to the following, which also is working for me:

\s*?(?=\r)


— Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2022 Oct 03, 2022

@Shanpub said: "…this is perfect for what I want to implement but the problem is when I'm adding this in script my InDesign crash"

 

If a GREP expression like

"\\h+(?!\\n)$"

is crashing InDesign, it could be a bug in a specific version of InDesign or a strange condition in your document you are running the code at. To test this just run your code to this point:

var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\h+(?!\\n)$";
app.changeGrepPreferences.changeTo = "";

 

Then open the GUI for GREP Find/Change and check if you see the right expression in the GUI.

Test if the GUI can handle the Find/Change action on the active document.

 

If all goes well with that, return to your code and run it not on the document, but on a selection of text.

If InDesign will crash then, just provide a sample document where you see the issue. Simply attach the document to your reply post.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2022 Oct 03, 2022

Why would anyone wants trailing lines or paragraphs if correct paragraph styles are used?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2022 Oct 03, 2022
LATEST

100% agree, but I do find very selective use of line returns useful (such as dividing a long heading without creating yet another style that folds it neatly).

 

It does sound as if the original OP, long ago, had used them much more liberally, which is indeed a practice to avoid.

 

—

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines