PhpStorm Regular Expression search and replace & Macro function
As a long time Vim user I’ve learned to rely on a couple of time savers that take some time getting used too.
Mainly this would be regex search and replace and the use of macro’s. Both have a bit of a learning curve and have fairly niche applications. However, when such a situation presents itself they are an invaluable time saver.
First is the one that I use almost daily. Regular expression search and replace. Every time you have to do a batch of change operations on line(s) that are almost but not quite the same often regex search and replace can save you a lot of time.
Today for instance I decided to convert the config system of an application I was working on from a native PHP array format to a YAML file. I don’t hate using arrays as config files for simple things, but putting them in a data storage format allows for some more flexibility. In this case I needed to have a main config file, and a “local” config file with mutations to the main config file. That way I can save the main config file in the project repository and people can keep a local config file that can overrule certain settings, like database settings/credentials and such.
Anyway, besides the point. What this file had in common was a lot of repetitive lines that where in the same format, but obviously did not contain the same value. Regex to the rescue.
On the left you can see the original PHP array file, at the top somewhat enlarged the search & replace screen, and on the left the result after hitting the “Replace all” button.
The regex itself is not a thing of beauty, and it doesn’t have to be. It’s for a one-time use operation, it just has to do the job. I won’t go over it in detail, but just as a short summary, I want to match the entire line, so I start with “^” and end with “$”. Then first I want to match the whitespace and put it in a capture group so I can copy the indentation. Then I want to match whatever is between the initial single quotes, they are all single quotes so no need for complicated quote matching. Then I put whatever is in between them in another capture group, I set the 0,n modifier to not-greedy so I just get the contents I want. Then I have a throw away “match all” until the “=>” brackets. Then for capturing the value of the array notation I had to modify the catch all to exclude “(“. This because I did not want to match the “‘something’ => array(” lines.
Then I wanted to replace the matched lines with the contents from capture group 1 and 2. The indentation and key, add the colon, and then a space and the value, and done.
Once you’ve used these types of operations a few times it becomes really fast to come up with the initial expression, making it a very fast way to reformat 10 lines or a 100. PhpStorm visually shows you what you are matching and how it will be replaced which makes it very easy to write the expression with immediate feedback on what is going to happen.
The the second feature, macro’s.
By default the macro functions aren’t bound to hotkeys, to fix this I bound them to ctrl+shift+q to start/stop recording, and shift+q to play the last recorded macro. As one would imagine, macro’s allow you to record keystrokes and then replay them.
Now to really make use of macro’s you will have to learn efficient ways to navigate the code. I have to admit in Vim this was a bit easier since all the ways of moving trough your code where more direct, while in PhpStorm I don’t use them that often, and when I looked in the keymap most of them aren’t even bound to keys by default.
Either way, 2 useful ones are the Home&End key to go to the beginning of the text and the end of the line, and CTRL + left/right to skip to the next word. I must say I really miss shift+% from Vim which allows you to go to the matching bracket/parenthesis, which allowed for some really powerful stuff. But the only equivalent I could find in the keymap was to switch between ending/beginning of brackets. Which is nice, but only about half as awesome as being able to have the same hotkey for parenthesis.
Anyway, just hit the record key, do some semi-intelligent code modifications, making good use of relative movement. Then save by hitting the record key again and replay on the spots you need it.
It’s admittedly not as powerful as the regex search and replace, but in some cases search and replace just can’t do exactly what you need it to do and then macro’s can be a real time saver.


