This writing will be updated with GREP expressions for use in InDesign that I find useful.
First things first: cascading your GREP work.
If you have something that needs to be in all of your paragraph styles, like adjusting InDesign’s hyphenation rules to something that’s not quite so awful or one of the GREP searches on this page, you’ll want to have your paragraph styles based on an existing paragraph style. The best practice is to create a paragraph style based on [Basic Paragraph]. I’ve been creating one simply calledÂ basic. To this new paragraph style, I load in all my defaults for the document — the typeface, sizing, leading and all that for theÂ basic copy paragraph style; any GREP queries like applying a character style to the book title; and setting hyphenation and justification settings to what I prefer. All of my other paragraph styles are going to point back to thisÂ basic paragraphÂ style.
You’ll want to do this to a new paragraph style because you really want to leave [Basic Paragraph] alone. If you wind up copying and pasting text from a document to another — or hand off your document to someone else — that [Basic Paragraph] definition you’ve created can be overwritten by the newer document’s [Basic Paragraph] definitions. There’s more about that in this article at InDesignSecrets.
So you want to have that newly-createdÂ basicÂ paragraph style refer to [Basic Paragraph] and have all other styles be based onÂ basic, or styles whose “based on” trail can be traced back toÂ basic.
For instance, I’ve got five paragraph styles for numbered lists: a numbered list style for items 2 onward (no leading between list item paragraphs); aÂ numbered list, firstÂ style for the first item in the list (this has leading above the list item paragraph); aÂ numbered list, lastÂ style for the last item in the list (which inserts leading only after the list item paragraph);Â numbered list, addl, (which has no leading before or after, but has indenting on left and right sides to fit the paragraph in the list’s margins, plus a first line indent so this reads as an additional paragraph that is part of a list item); and, in case the last list item has an additional paragraph,Â numbered list, addl, last which would have everythingÂ numbered list, addl has, plus some leading below the paragraph to separate the list from any other following content. Whew!
So,Â numbered list, addl, last is based onÂ numbered list, addl, which is based onÂ numbered list… which is based off ofÂ bulleted list (which has a whole ‘nother four bullet lists based off of that), which is based off of basic copyÂ (used for the majority of the book’s text), which is then based onÂ basic. Or in other words:Â basic > basic copy > bulleted list > numbered list > numbered list, addl > numbered list, addl, last. If I make a change toÂ basic, that change cascades all the way down toÂ numbered list, addl, last.
Change all spaces before numbers to non-breaking spaces.
(courtesy Adam Jury)
Search forÂ \s(/d)Â replace withÂ ~S$1
What this does: This searches for any digit (/d) with a space before it, then replaces the digit with a non-breaking space and the digit. Those parentheses create a backreference; the $1 in the replace string looks in the search string for whatever is in the parenthesis and places it there.
Apply a character style to specific lines of text.
In GREP Styles, search for the actual string of characters, separated by | symbols. Optional items are placed in parenthesis and are followed with a ?.
For example, if I want to apply a character style to every reference of Star Trek’s Enterprise that makes it bold and italic, I’d search forÂ (USS\s)?Enterprise and apply theÂ bold italicÂ character style.
What that example does:Â This searches for the word strings “Enterprise” and “USS Enterprise” and applies the character style to either of those.Â TheÂ (USS\s)? part searches for the text string “USS” followed by a space, zero or one times. So this will catchÂ USS Enterprise andÂ Enterprise, but notÂ U.S.S. Enterprise.
If I have a statblock that has lines beginning with Armor Class, Hit Points, and Speed, and I want to have those appear as green and in all caps, I’d search forÂ Armor Class|Hit Points|Speed and apply aÂ statblock inline headerÂ character style.
What this example does: The | symbol is “or”, so when this comes across “Armor Class” or “Hit Points” or “Speed”, InDesign will apply that character style. However, this will capture all instances of the string “Hit Points” (with a capital H and P). You can make sure this only is applied to the start of any line if you have this asÂ ^(Armor Class|Hit Points|Speed). TheÂ ^Â character is a location character for the start of the paragraph.
Get rid of orphans.
In GREP Styles, search for \s(?=\w+[[:punct:]]*\s$)Â and apply theÂ no breakÂ character style. The only thing defined in that character style is in the Basic Character Formats view,Â No Break is checked.
What this does:Â This searches for any space that is followed by a word and any number of punctuation marks and an optional space at the end of a paragraph. It then places theÂ no breakÂ character style on the space character before that last word. In the previous paragraph, this would find ” checked.” and tell InDesign to keep “is checked.” together on the same line. If that last word would be placed as an orphan at the end of the paragraph, it places “is checked.” on that line, avoiding the orphan.
This writing was generously supported by my patrons. You can motivate me to create more writings about layout, game design, and layout in games by becoming a patron. Find out more about my writing goals (plus my secret goal) on this site’s Patreon page.