Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shovels's avatar

Convert <space> to <underscore>

I'm trying to find an easier way to type underscores in test method names. e.g.

/** @test */
    public function it_should_do_something_when_this_happens()
    {
        ...
    }

I'm using Linux and PHPStorm - I've looked into remapping keys, but before I embark on that journey I'd like to find a more simple solution if possible.

Cheers, Neal.

0 likes
8 replies
willvincent's avatar

phpstorm supports regex for find/replace so if you can work out the regex to find just spaces between the first character following function and the opening paren, you could use that pattern to find all the spaces the change to underscores in your entire file. And that could probably be turned into a macro or something.

1 like
Shovels's avatar

@mstnorris I'm using the following live template...

/** @test */
public function it_$NAME$()
{
    $this->markTestIncomplete('pending');$END$
}

Not really played with these much - can you point me in the right direction?

Thanks, Neal.

willvincent's avatar

Seems like what you really would need is a custom livetemplate expression function that does the opposite of the included underscoresToSpaces() one.. then that could be applied to the name variable, or you could do something like this:

/**
 * @test
 * (It $DESC$)
 */
public function it_$NAME$()
{
  $this->markTestIncomplete('pending');$END$
}

and that custom function would be setup so that $NAME$ gets populated with a lowercased/underscored version of whatever is put into DESC. Unfortunately a function for that does not seem to exist, and while it could be added with a custom plugin, not sure how difficult that would be to accomplish..

Shovels's avatar

Thanks @willvincent I ended up using the following live template:

/** @test ($NAME$) */
public function $VALUE$()
{
    $this->markTestIncomplete('pending');$END$
}

Edit Variables --> VALUE (Expression) = capitalizeAndUnderscore(NAME)

This allows me to type the method name as plain text. The VALUE is converted to UPPERCASE_SNAKE_CASE.

I also installed the PHPStorm CamelCase plugin. This enabled me to write the method name and then press Alt + Shift + U to convert to lowercase. It's so much quicker now!

Thanks for your input :)

(Note to anyone else playing with Live Templates: Jeffery's video on live templates and how to set the expressions: https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/7)

willvincent's avatar

It's too bad there's not a lowercase function.. decapitalize only does the first char.. if there were a lowercase function you could do this: lowercase(capitalizeAndUnderscore(NAME)) and save the extra Alt-Shift-U (or Cmd-Shift-U on a mac) step.

Shovels's avatar

@willvincent haha, yep - tried that as well.

I did look at creating a plugin - Apparently you can create a PHPStorm plugin which extends the available functions, but I couldn't find any specific documentation (just something about taking a Macro and using that as a starting point).

All the best, Neal.

willvincent's avatar

yeah I think I saw the same thing (on stack overflow).. I dug a little further than that and it looks like you need to set up a full java dev stack to be able to build plugins for the intellij suite. Unless I was going to build a suite of various plugins it felt like a lot of unnecessary effort :)

1 like

Please or to participate in this conversation.