Skip to main content
Dan Rodney
Community Expert
Community Expert
Jan 31, 2017 4:51 PM UTC
Answered

Remove Trailing Whitespace without removing extra lines

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.

Obi-wan Kenobi
Obi-wan KenobiCorrect answer
Legend
Jan 31, 2017 7:48 PM UTC

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.


\h+(?!\n)$

17 replies

Participating Frequently
Nov 6, 2026 2:16 AM UTC

I have changed my grep to horizontal space then tried vertical space. Both worked great except when I have a in-line table or art that are in there own paragraph. The paragraph symbol in the line before the in-line item gets deleted and i have to reinsert them. Is there something I can use in its place? Currently I am using \v+$ to remove double paragraphs and it is not working perfectly. 

Translate
Willi Adelberger
Community Expert
Community Expert
Oct 3, 2022 1:49 PM UTC

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

Translate
James Gifford—NitroPress
Legend
Oct 3, 2022 5:41 PM UTC

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
Community Expert
Oct 3, 2022 1:45 PM UTC

@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
Dan Rodney
Community Expert
Community Expert
Jan 31, 2017 6:55 PM UTC

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)

Translate
Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
winterm
Legend
Jan 31, 2017 6:40 PM UTC

What if simply exclude carriage return?

\s+[^\r]$

Translate
winterm
Legend
Jan 31, 2017 6:57 PM UTC

Your reply disappeared for me...

However, here's "improved" version:

\h+[^\r]$

it works on my side...

Translate
Dan Rodney
Community Expert
Community Expert
Jan 31, 2017 7:00 PM UTC

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

Translate
Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor
amaarora
Inspiring
Jan 31, 2017 6:35 PM UTC

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
Dan Rodney
Community Expert
Community Expert
Jan 31, 2017 6:41 PM UTC

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

Translate
Adobe Certified Expert & Instructor at Noble Desktop | Web Developer, Designer, InDesign Scriptor