Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

5 Cool Things You Can Do with GREP Styles

I love GREP Styles in CS4. I’ve said it before: It’s probably my favorite new feature, and although it’s small, I think it makes the upgrade very worthwhile. But what are they, how do you make them, and what can you do with them?

A GREP Style is a way to apply a character style to some text inside a paragraph, based on a GREP pattern. GREP, as we’ve discussed, is a way to describe a text pattern using codes. For example, you can write “all the words that start with a and end with e” with the code \<a\S+?e\>

You can use GREP in the Find/Change dialog box, but only manually — that is, you have to actually do a search and replace each time you want to apply the formatting. GREP Styles don’t change text at all; they only apply formatting. But there are many things you can do with just that.

To apply a GREP Style to a pararaph, you probably want to edit your paragraph style definition. (You can apply a GREP Style to a single paragraph as local formatting by placing the cursor in the paragraph and choosing GREP Styles from the Control panel menu. But I find that in general it’s just more useful using GREP Styles inside a paragraph style.)

To make a new GREP style, click the New GREP Style button:

To apply a character style, click once on Apply Style and it turns into a character style. Then you can choose a style you’ve already made or choose New Character Style if you haven’t made one yet. (That’s another little feature I love in CS4.)

Type the GREP code in the To Text field. For example, the default GREP code \d+ means “one or more digits in a row.”

I encourage you to turn on the Preview checkbox to see how (or if) it works before clicking OK.

Five Cool Things You Can Do with GREP Styles

Okay, enough with the “how.” Now what about “what”? Bob Levine already posted about one way to use them here. Here are 5 more ways I like using GREP Styles. Please feel free to comment below with other ways you’re using this cool feature.

1. Oldstyle Numerals. Apply a character style that applies the OpenType Oldstyle Proportional figure style to all digits in a paragraph.

2. No Break words and phrases. I’m tired of setting individual words to “No Break” or changing dictionaries to instruct InDesign not to break words. If I don’t want to break “InDesign” on to two lines, I’ll just apply a no-break character style to the GREP code “InDesign” (that’s pretty simple grep, eh? Just type the word!)

3. Change Width of Em Dash. In some fonts, the em dash is just too wide for my tastes. No problem. Make a character style with a 75% (or whatever) horizontal scale. Then apply it to the em dash character. Don’t know how to type an em dash code? No problem. Just click the icon to the right of the To Text field. That shows you all kinds of cool codes for things.

4. Make OpenType Fractions. Jamie McKeee reviewed and explained the excellent Fraction script in the recent issue of InDesign Magazine. I added a little sidebar to that review before it went “to press” that explained that if you are using OpenType fonts that include the Fraction style, you can apply them with a GREP Style. Apply a “Fractions” formatting character style to the code \d+/\d+ (which means ?one or more digits, followed by a slash, followed by one or more digits.?) It won?t work if you have commas or decimals in the fractions, but it?s a good start! (Of course, you could make a GREP code to handle those situations, too?)

5. Make Characters Disappear. My colleague and co-author Olav Kvern just wrote me, saying, “You really don’t need to find/change typical typewriter text entry errors (double spaces after periods, etc.) anymore–just make a grep style that does it and make it part of your base paragraph style(s).” Wow. Good point! But how to make a character disappear when you can only apply formatting? Make a character style that applies the fill color of None, a tiny point size, and a horizontal scale of 1%. Boom, it’s “gone.”

Dang, I can’t stop at five. Here’s one more you might find useful:

6. Replace text with icons. What if you want some text to convert into a special character? For example, everywhere you type some normal text, you want it to change into a special icon in a particular font. Here’s the before and after:

I found the ornament/symbol/glyph I wanted could be typed with the number “4″ in Bodoni Ornaments. So then I made up a little easy-to-remember code that would never show up in normal text: “4z”. I then added two grep styles. The first one was 4(?=z) which means “look for the character 4 but only when it has the letter z immediately after it.” I applied my bodoni ornaments character style to that. The second grep style was (?<=4)z which is “find the letter z, but only when it has the number 4 before it.” To that, I applied my disappear character style.

What do you think? Can you think of other cool uses for this excellent feature?

Tags
Related Articles
Comments

83 Comments on “5 Cool Things You Can Do with GREP Styles

  1. Hi Jongware,

    Peter Kahrel’s chaining script is actually what will work for me best. Thanks for turning me on to it.

    Jerry

  2. I’m about to embark on investigating using GREP styles for our business, we are involved in text book publication and I’ve been trying to find a way to apply different text colours for nested styles in different chapters or on different pages, rather than needing a new style for each colour

    e.g. one spread will be grammar and all the text styles will be green

    the next page will be vocabulary, all the stylesheets should match exactly except the spread colour is red, so the headings, underlines and exercise numbers change from green to red.

    the next is blue then back to green again etc etc (these vary but the principal is the same, we don’t want to make these changes manually or create a different document for each spread).

    my question is, does GREP recognise page numbers or sections?

    wish me luck!

  3. Hello everyone, I have one problem with GREP style, I cant figure out how to configure it to work right.

    In document there are used different chemical formulas, so that means some numbers are subscript, others superscript…

    the problem is that here: 109/l number 9 should be superscript style – 10(9)/l

    I can get it work that 9 is superscript after digit, but don’t know hot to stop it before / sign.

    One more question, where could I get some more info on GREPs – like which “formula” means what..

  4. I am producing a brochure with acronyms including CO2. Is there a quick way to get the 2 in CO subscripted automatically? So far I do it by hand and it’s very tedious and miss some…

  5. Thanks, Jongware. Isn’t that amazing that people so often write a comment without looking at the comments that have gone before them? Jean-Marc, I don’t mean to pick on you, but just look at the two comments before yours! :)

  6. First off, let me say that GREP is COMPLETELY foreign to me and I really just don’t understand it. So figuring out if GREP can be used for what I am needing is really confusing.

    I have three different block quote Paragraph Styles that I use. We almost always put a space before and after the block quote but not between the paragraphs within the block quote. I have the block quote styles set to automatically add a space before and after so that I do not have to manually do this. Because the before and after space is added with a hard return and when there are multiple paragraphs within a block quote, I do have to manually remove the space. This is very time consuming. I would love to be able to have this space not appear WITHIN my block quote styles and instead only before and after them.

    Can I use GREP to solve this?

    Thanks in advance for any help! (We are a non-profit organization that publishes books. None of us have any “formal” InDesign training, although we have been using it for years and I just know there are ways to work smarter that we still haven’t figured out!)

  7. Hi!

    I’m new to GREP concept and I have this small repetitive task I have to perform and I’m sure the GREPs are my solution. Here’s what I have to do…

    I need to change this formating:
    (enter)
    word
    (enter)
    word

    …to this kind of format:
    (space)word(space)word

    Thank you for helping me!

  8. have a great one for anyone who has to change Mclaren to McLaren and the like:

    (?<=\Mc)[\l\u]

    to find the lowercase letter which should be uppercase; then

    in the style, make sure that the character style is All Caps.

    doesn't sound like much, but when faced with a database of thousands using data merge, applying the style to the surname entry corrects the entries on the fly rather than fixing them in the d'base itself.

  9. I’ve run into an issue with a project where I need to all caps the running headers. With some titles containing registered trademarks.

    When pulling data from the paragraph title style (with superscript registered trademarks forced via GREP), the running header style overrides the superscript and makes the registered trademark large again.

    I tested to see if turning off the paragraph all caps style would allow the superscript GREP style to work, but it didn’t superscript either. However, if i set the paragraph style to superscript all without all caps on, the entire text superscripted.

    Is there a known issue with superscripting individual characters in running headers, or does anyone know of a solution? I’m completely scratching my head on this one. Thanks!

  10. Aaron: the issue at hand is that automatically picked up text such as your running header cannot be selectively formatted. Compare it to attempting to format the first digit of a two-digit automatic page number.

    There is this plugin called “Power Headers”, which I think does allow selective formatting; I usually just forget the automatic way and do it ye olde style: manually.

  11. I thought that might be the case, but was hoping for an automatic solution. Fortunately, if i need to manually do it there are only a handful of chapters in the 300 page technical book that need it. Thanks for the quick response Jongware! BTW, fantastic info here for the chemical GREP styles. Will save me loads of time.

  12. Is anybody still reading this? Hope so. I am not a professional, I am merely laying out a small book for my club (Nevada Native Plant Society). Its all laid out (with some prior help from these forums, thankyou very much), but the problem now is: need the scientific names in the photo caption boxes and text to appear italicised in the index. It seems that I have to learn about GREPS and I have been doing so. The most useful text for understanding how to build expressions was Michael Murphy’s GREP in InDesign CS3 which has examples in color with detailed but simple explanation, as well as explanation of overall principles of assembling GREP expressions. The best one for showing how to incorporate it in a paragraph style was Using GREP Styles in InDesign by Jennifer Jones. The abovecomments are to help out other people in my same boat.

    The other reason I am writing is to ask you experts: what would the metacharacter be to refer to text that is italicized? Can’t find it anywhere. I found caps but not that.

    Then am I right that having the code I need, I need to follow a procedure like that described for subscripts/superscripts and Index Entries and Subscript/Superscript and David Blatner’s Auto Format Superscript and Subscript Numbers using GREP Styles.?

    BUT a set of instructions that describes exactly what I want to do would be just ducky! Including where to put what, and how to run the index to reflect the modifications. It would save me countless (unpaid) hours. Can anyone help me with that? All the future users of our little book will thank you………

  13. Hey pat4141,
    I haven’t tested this but I wondered if you just needed to restrict your query by including a character style (italic) in the Find Format box?

  14. Hi David and group,
    Thanks for this and your previous columns.
    I’m a newbie working with a lot of text with an older version of InDesign. Two things eat up a lot of my time:
    1. Vertical justification (I had to snap the text to baseline and manually review each page to make sure the text’s top and bottom lined up on each double-page spread;
    2. Now I’m battling my way through the proofs, mainly hunting down the “runts” (widows/orphans). This is darn annoying, made easier with DPT Tools. I still have to manually correct each.

    Are both these things more easily accomplished in CS5 and later, through GREP? I know you have a vested interest in DPT Tools, but I still thought I’d ask.

    Thanks very much in advance.

  15. @David, Thanks for answering.
    So what you’re saying is, CS6 can find and fix the runts but not manage justification, right?
    DPT Tools also seems to find the runts but not do justification. “Optical justification” with the flowing text from my novel seems to balance one double-page spread but throw another page off.
    I apologize if I’m asking in the wrong forum. Thanks for your help.

  16. Can GREP automatically put R (superscript) and TM (superscript) after certain words such as “Windows” or “MAC OS”? If so, can it be taught to do this only in the first occurrence of the word and none of the subsequent?

    Thanks!

  17. @Angela
    Grep Styles cannot add characters it can only format them. It can change WindowsTM
    to Windows?, but not add the ? for you.
    Auto Correct can’t handle “fancy characters”

    But you could combine the 2 functions
    First Auto correct Windows to WindowsTM then grep it.

    NOTE that auto correct is not style specific and grep styles are so this is not a great solution unless you want all the paragraph styles to do the grep and then you would have to setup the grep style on every style or base style. (easier to add the ? manually)

    A simple text / grep find / change will do the trick
    I made quite a nice (if I say so myself) script that processes a word list (made in excel (or any text editor – word, wordpad saved a tab delineated txt file) or stuck in the script) this is not a dynamic solution but with a keyboard shortcut can be very effective.

    The script is here http://forums.adobe.com/message/4610558#4610558

    Hope that helps

    Trevor

  18. @Angela:

    Sorry I missed the point about the first occurrence.

    The grep to change the first occurrence in a paragraph of Microsoft to Microsoft?
    would be find:
    ((?=Microsoft(?=.*Microsoft))\s|Microsoft(?!=Microsoft*$))
    change to:
    $1~d
    to find the first occurrence in the story would be a lot more complicated to grep but easy to script.

    I think the most sensible solution is that when you want Windows to be followed by a superscript r of TM would be to type Windowsr or Windows Tm with the following grep style
    (?<=Windows)(tm|TM|r)

    To have every Windows trademarked would be bonkers!!

    Trevor

  19. I have a very large document with figures in a parentes which is a reference number i.e. (1). I need to change all reference numbers in the parantes to oblique. Is there an easy way to find and replace?

  20. Hi all,

    I have a large document with lots of fees in tables and I have been asked to include the Pound sign (£) in front of all the fees. Is there a grep code that can find any number (eg 1,356) and place a ‘£’ in front of it?

    Thanks,

    Deji

  21. I am creating certificates where the line has to read something like this
    on this 2nd day of… where the nd after the 2 needs to be a superscript. Or 3rd, 4th etc. I was able to get the 2 to superscript, but don’t want that. Is there a way to do that? Anyone know? Currently I have 400 certificates to do. I’d prefer not to have to go thru manually.

    Thanks

  22. I’m trying to do simple ‘initial uppercase’ (sadly not included in ID basic character formatting) for words in a header – been trying GREP but no success:
    1.create all caps character style
    2.edit the affected para style
    3.create a grep style
    4.Apply Style: that all caps character style
    5.To Text: \ Beginning of Word)

    The beginning letter of each word remains lower case. -

    -The source character style is definitely all caps.

    -Preview is on.

    -The edited para style character format is set to Case: Normal.

    Surely this common formatting need is possible w grep – what am I missing? thanks!

  23. Sorry, somehow typo on step 5; should be:

    5.To Text: backslash left carat (menu picks are Locations – Beginning of Word)

  24. just wanna ask if there’s a grep expression that can make a paragraph which have a 1 letter 2 line drop cap to 2 letter 2 line drop cap??
    example.
    the paragraph starts with a (Single|Double) quote >> “This | ‘This.
    is it possible for grep to span the drop cap to 2 letters 2 lines instead of only 1 line which drop caps only the quotation mark.

    thanks for future comments.. ^^
    and i enjoy using grep styles and expressions in my work ^^

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>