Deth

Fitness and everything else

Kate Regex Search And Replace

Today I was mass editing some files and got to wondering if Kate would be a suitable option. I could have used sed, but I wanted to see and test the changes visually this time. I started wondering whether the editor could use search and replace. Sure enough, it can. There’s a tiny icon with { } on it that I hardly noticed because of my vision, but that allows it to use regular expressions in the search and replace function.

In the files, I had title = 'The Title' and description = 'The description' in the markdown files now. I wanted to change them to double quotes, which I am able to see just a tad bit better. The only reason is for looks and to make it a little more accessible to me. There were thousands of files, so doing each one wasn’t reasonable.

Once I figured out that Kate supports regular expressions, it was as simple as building one. I don’t get a ton of practice using them, so it was nice that Kate shows what’s selected. I finally ended up using title = '([A-Za-z0-9-,.!? ]*)' in the search box. That found lines starting with title = ' and having any numbers, letters, or punctuation between the apostrophes. There were a few edge cases that were missed, such as dashes and things like that, but they covered the vast majority of the occurrences.

In the replace box, I had title = "\1" which took everything in the parenthesis and put that in the \1 was in the replace text box.

Of course, I noticed the nice little dropdown when you click on the () in the search box itself after the fact. That would have saved me a good bit of searching on the internet, since I don’t use regex enough to memorize it.