Formatting Code in InDesign with GREP Styles
You don't have to be a geek to love the power of GREP styles.
If you’ve seen issue 32 of InDesign Magazine, you might have noticed that I am a fan of IDML. I am excited by its potential, and geek out on how everything in an InDesign document can be represented in XML that you can actually read and edit. I’ve been working on some IDML documentation, including a collection of simple documents showing certain page elements and their corresponding IDML.
To make my guide, I used to just open the IDML in Oxygen and take a screenshot of the code and paste that into InDesign. But this doesn’t work well for long pieces of code. So I switched to copy and pasting the XML into InDesign text frame. Just one problem: I could only paste without formatting, and it’s hard to find stuff in a sea of black 12 pt Monaco. If I turned on InDesign’s Clipboard Handling preference to paste all information (including the code colors), I got nothing. The Paste command just didn’t work. Even tried copy and pasting through an intermediate application, and that didn’t work the way I wanted.
But every obstacle is also an opportunity, right? This was a perfect time to whip up some GREP styles. IDML (or any well-formed XML) is a predictable, structured string of text. Elements always reside within tags, tags always reside within brackets, attributes are always followed by an equals sign, and then a value in quotes.
And the party always starts with a declaration, stating this is XML.
By matching these patterns with GREP styles, I can create any kind of look for my code and apply it with a single click. I chose to imitate the default look of XML in Oxygen, since I’m familiar with that: elements in black, tags in blue, attributes in orange, quoted values in brown, and the declaration in purple. So I first created character styles to apply all the colors except for black (which will be applied by the paragraph style).
Now I’ll admit, I don’t live and breathe GREP every day, so I expect one of the GREPmeisters out there can offer improvements on the following code, but it works for what I wanted to accomplish.
I have one paragraph style that sets the foundation: 12 pt Monaco in black.

Then to that paragraph style I added a GREP style to color the tags blue. It starts with (?x) to let me space out the parts of the expression for better readability.
![]()
It matches an opening bracket, an optional forward slash, one or more word characters, an optional colon, one or more word characters or an optional forward slash and a closing bracket. This fellow matches all varieties of opening and closing tags, including ones with a namespace prefix (indicated by the colon).
Next comes a GREP style to color the quoted values brown. It matches everything in quotes, plus the quotes themselves.
![]()
Next I added a GREP style to color the attributes orange. It matches one or more word characters, an optional colon, one or more word characters again, and an equals sign.

Finally, a GREP style to make the declaration purple.
![]()
It matches an opening bracket followed by a question mark and any characters that come after, up to the end of the line. By placing this style at the bottom of the list, I ensure it’s applied after the others, so it overrides them. Here they are all together.

Now I can copy IDML (or other XML), paste it into my document, unformatted…
…and apply my IDML Code paragraph style to the whole thing. Bingo. It’s all styled instantly.
Truly a thing of geek beauty. I unstyle it and restyle it just to watch the magic over again. Yes, I am easily amused.
Now I’m guessing you probably don’t need to style XML like this, but I love how it illustrates the power of GREP styles. Try ’em out. Then when someone makes a template with dozens of paragraph styles, you can say, ‘Tsk, tsk, I would’ve just used one.’
This article was last modified on March 15, 2023
This article was first published on November 5, 2009




