Batch renaming

Tutorial
by Rainer Erich Scheichelbauer
en fr zh

1 August 2022 Published on 15 June 2012

Need to rename many glyphs? It’s so easy you’ll wonder why you don’t do it all the time.

Replacing

When you need to change many glyph names, there’s Edit > Find > Find and Replace (Cmd-Shift-F). Calling this menu item gives you this dialog:

It works just like Find and Replace in any other application, except that Glyphs applies this functionality to the names of all selected glyphs.

A simple example: imagine you want to prepare stylistic set alternatives for all your capital letters. Just copy and paste all letters from A to Z inside your font. Glyphs creates copies named A.001 through Z.001. Now, select those .001 letters and call the Find and Replace dialog. There, you search for .001 and replace it by .ss01. Done.

Adding a suffix

The dialog has a hidden extra function: If you just want to add a suffix to a range of glyphs, then just leave the Find field blank and put the desired suffix into the Replace field.

By default, Glyphs puts most ligatures into the dlig OpenType feature. But you want all your ligatures to go into the liga feature. Just select your ligatures, choose Find and Replace, put ‘.liga’ into the Replace field and hit the Replace button. All that’s left to do is to recompile your feature code in the Font Info window, and Glyphs takes care of the rest. Cool.

Final quick tip: In order to quickly select all glyphs with a certain suffix, just enter the suffix in the Search field (Cmd-F) in the top left of the Font View. Want to see all ligatures? Search for an underscore (‘_’).

Regular expressions

Noticed the Regex checkbox? It allows to find and replace with regular expressions. Regular expressions are the holy grail of finding and replacing, and you can do cool stuff with it. I’ll give you two examples.

Preserving part of the search string

Imagine you need to switch all your .cvXX glyphs to .ssXX suffixes. In other words, .cv01 should become .ss01, .cv02 should become .ss02, and so on. You could do it without regular expressions, and just search for cv and replace it with ss and hope to get away with, but that might cause an unwanted replacement in your glyph names somewhere else.

Enter regular expressions. They allow us to say, ‘look for .cv plus two digits thereafter, then replace it with .ss and the exact same two digits. This is how you do it: Bring up the Find and Replace dialog with Cmd-Shift-F and turn on its Regex checkbox. Then in the Find field, you type:

\.cv(\d\d)

…where \. means a literal dot. In a Find expression (not in the Replace expression), . has the special meaning ‘any character’. So we ‘escape’ it with a preceding backslash. The parentheses ( ) mean that the Find and Replace dialog will remember this part for replacing. The enclosed \d\d means two digits. Sounds logical so far, let’s move on. In the Replace field, type:

.ss\1

…where the dot means just a dot. In the Replace string, we are just telling it what to replace the search string with, so we do not need to escape dots here. Finally, the \1 means ‘the first parenthesis expression in the Find string’. In our case, that was the two digits we put between round parentheses upstairs.

So this will reuse the parenthesized parts of the Find string for replacing. And wit \d the search term applies to any digit. That way, our finding and replacing becomes more general, more abstract, or as coders like to put it: more regular. Hence the term ‘regular expressions’.

Changing suffix order

Another popular thing to do with regular expressions is to reorder parts of the glyph names, e.g., suffixes. Say we have a couple of glyphs ending in .liga.loclNLD but we need the suffixes the other way around. Here is what we do. In the Find field, enter:

(\.liga)(\.loclNLD)

Nothing new here, really. Just that we have two parenthesized expressions this time. The escaped dot we already know. Now comes the fun part. In the Replace field, we write:

\2\1

Which means, take the second expression from the Find field, followed by the first expression from the Find field. So easy to reorder stuff.

And .liga.loclNLD will change into .loclNLD.liga. It becomes more interesting when you do it more abstract with (.liga)(.locl[A-Z]{3}) which matches any uppercase letter [A-Z] repeated three times {3}, and thus catch any of the ISO language codes.

These two scenarios, repeating part of the search term, and reordering name parts, may be the most frequent scenarios for which you can use Regex when finding and replacing in glyph names. There is, of course, much more you can do with regular expressions, we have only scratched the surface here. There are countless resources on the web about regular expressions, so it is best to keep a cheat sheet like this one around. But really just do a web search any time.


Update 2022-07-19: updated title, related articles, minor formatting.
Update 2022-07-25: updated screenshots.
Update 2022-08-01: added chapter about regular expressions.