TextEdit Pattern Search and Replace

A hidden feature of TextEdit will allow you to search for patterns like email addresses, phone numbers and URLs. You can also construct complex search patterns. You can use these patterns to do complex text replacements such as switching around the order of items in your text.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: TextEdit Pattern Search and Replace.

Now let's say you wanted to search for some text inside of TextEdit. Easy enough. You can choose Edit and then Find, or use Command F, and search for the text. It will find it for you in the text. You can use these little arrows here to jump to each one and hit Done to exit.

But suppose you wanted to find something more complex. There's actually a way in TextEdit to find some complex data. It's called Insert Pattern. It's part of the Find function. So I'm going to do Command F for Find and clear it out. Then I'm going to use this little magnifying glass here and you can see I have some options. I can Ignore Case, Wrap Around during my search. That kind of thing. Starts With, Full Word, all sorts of stuff.

Now there's also something called Insert Pattern. Let's select that and we get this second menu here. We can see that there's a whole bunch of special stuff in here. At the bottom there's some very special things like finding an email address. Let's select that. We can see that it found one. See that little one there and there's the email address in the text. We can also try to find some of those other things.

Notice Insert Pattern has Control, Option, Command P as a shortcut. We'll use that from now on. It's a lot easier than selecting this. We can choose URL and it will find the one URL here. Clear that out and I'll do that keyboard shortcut, Control, Option, Command P and let's choose Phone Number and it is going to find two phone numbers. There's the first one and there's the second one. You can see they're even slightly different in their formatting. So it's kind of interesting to be able to use these to quickly find these things. How else would you be able to search for them in TextEdit since you don't know what the phone number or what the email address is.

But there's some other stuff inside of Insert Pattern that allows you to do even more complex searches and even replacements. Let's say, for instance, you want to find two words but they aren't next to each other. There's going to be a word between them. Let's just take these words here, the first and the last one. So I'm going to do a Find here. I'm going to search for the first word and then I'm going to search for something in the middle. So it's space and then I'm going to use Insert Pattern and say Any Word, and you can see it inserts a little thing there that says word. Then I'm going to do another space and another word. Now it's going to find twelve instances where it will find any word that's these three words even if the middle word is something different.

So let's change this to something else and Find. We'll see it found it here and it found it here even though the middle word is different because we're using this kind of wild card for a word there in the middle.

Now let's say you wanted to search and replace with a tricky replace here. For instance you've got a list here of states and their capitals and the date they became a state. It's perfect except you want it to be the state name first and then the abbreviation. So we can use Find and Replace to fix this.

So the first thing we'll do is we'll find a special pattern for a word. Then we'll do a special pattern for Tab, so Any Word and then the Tab. Then we'll do special pattern again and instead of word we'll say Any Characters. The reason we don't say word is because you know some states have two words like New Jersey for instance. Now we'll look for Tab again. We can see here it's still capturing a bunch of odd things. So let's at the beginning say, well let's have this all start with a Line Break. So it only happens after the start of a new line. So now we can see it found 49 of them. We can go through them and we can see here we've got all of them except the first one because, of course, the first one doesn't start with a line break. So we'll insert a line there temporarily and we'll search here again and now we see it found 50 of them. Great.

So let's check Replace here and then let's work on a replacement for this. So what we'll do is we'll say we'll let's first start with a Line Break again. Okay. Now we'll go and we want to put the second thing first. So we'll do a selection from here. See below this line is all the things that we're finding. So we can select any of these and we can reverse the order. So then it's going to find a Line Break, a Word, a Tab, Any Set of Characters, and then a Tab and then it's going to put them back but swapping out the Any Set of Characters and the Word. I hit Replace All and you can see it did exactly that. It's got the state abbreviation now after the state name. So it does what we want.

Notice that if you have more than one thing like say we were to do Any Set of Characters. Let's Undo this and let's say Any Characters for the next set and do a Tab again and then to complete it we'll take the date there. We'll do Any Characters and then we will do a Line Break and then we'll get rid of this one here at the beginning because we don't need it anymore if we're going to search the entire line. Now you notice it can find 49 of them. It can't find the last one because there's no Line Break at the end. So I'll add a Line Break at the end there. We can see that it found 50 of them. We can go through them and notice that if there's Word, then Any one, Any two, and Any three. So we can now Insert Any three, Tab, Any two and then Line Break. So now we can see that we're basically reversing the first two. Word, Any one. Any one and Word. Then Any two, Any three and Any three, Any two. So it's going to reverse the state abbreviation with the state name. Then it's going to reverse the capital with the date. I'll hit Replace all and there we go. It does it.

So you can do really complex things. It doesn't have to be an entire line like that. It can be some sort of complex format, codes, or things like that. You can use this, in TextEdit, to do some really interesting complex searches and replacements.

Comments: 6 Responses to “TextEdit Pattern Search and Replace”

    1 year ago

    That’s really useful. Clearly, it uses regular expressions behind the scenes but obviates the need to remember the regular expression syntax. Thanks Gary. I wasn’t aware of this facility.

    1 year ago

    I’ve used TextEdit for a lot of years without having realized it could do pattern match search and replace. So much friendlier than GREP. Thanks for the thorough tutorial.

    1 year ago

    Hands down the best tip I’ve read in eons! Thanks very much for this.

    1 year ago

    Your best tip in ages, Gary – thank you so much. A really straightforward yet powerful new tool already on my Mac, just waiting to be revealed.

    1 year ago

    Thank you – I knew about ‘Insert Pattern’ but not that it could used as in your US states example. I would’ve pasted the text into a spreadsheet and dragged columns around…

    Am I missing something tho’? Is it possible to Save (rather than Replace) what you’ve asked ‘Insert Pattern’ to find…to create a new text file?I grep spam emails to get senders’ addresses in BBEdit. Once I’ve got them I can ‘extract’ and save the text as a black list. Any ideas on doing this with TE?

    1 year ago

    Jasper: Nothing comes to mind as a way to do that in TextEdit. If you have it working in BBEdit, probably best to stick with that. BBEdit is very powerful.

Comments Closed.