Important: The information in this article is over 12 months old, and may be out of date or no longer relevant.
But hey, you're here anyway, so give it a read see if it still applies to you.
Update 26th August, 2021:
The Macro idea in this post still works, but Jetbrains pointed out that the 2021.2 release also included new "Actions on Save" features which means a Macro is no longer required. Find out more
I’ve been using a Macro for years in PHPStorm that, when I hit Cmd+S, reformatted my code based on the project’s settings. While reformatting on every save may be overkill, it does mean that my code does remain clean, neat and consistent.
In PHPStorm 2021.1, this Macro had 5 steps:
Those last two steps allowed the caret to stay in the same position, and to have all of the content de-selected (after step 1’s Select All).
But in PHPStorm 2021.2, the caret behaviour of selected text has changed, and steps 4 and 5 here led to some undesirable behaviour. Steps 1-3 are all fine, but…
4 moved the caret to the top of the document
5 moved the caret down one line
This actually meant that on every save, I was actually moved back to Line 2 of every file. On longer files, that’s a bit silly. Actually, no, it’s a bit frustrating.
In PHPStorm 2021.2, I have re-recorded a Macro, with 4 steps – and to be honest, I feel should actually work in earlier versions:
So now rather than touching the caret at all, a simple Escape deselects all of the content too, and leaves the caret in place.
While this feels like a bug given it has only cropped up with PHPStorm 2021.2, it actually is more a sign of an earlier bug with 2021.1 and earlier: moving the caret up and down when the entire text is selected does actually make sense to move to the top and down one line. So in that regard, 2021.2 actually fixed the issue.
But regardless, if you’re a Format and Save fan like me, tweak your Macro to keep the formatting love on every save.
Update from PHPStorm
The team at Jetbrains reached out and brought to my attention that PHPStorm 2021.2 actually has a new feature too - "Actions on Save" - which is able to do the same thing, without using a Macro. Find out more details in the new features for PHPStorm 2021.2 post.