189

At my company we have two different style guides for java vs sql. In java I have a field named historyOfPresentIllness and when i write the sql, I want to name it history_of_present_illness. Is there a keyboard shortcut to switch from one to the other when I have the phrase highlighted? Or perhaps a plugin that can do this?

While I'm asking, I may as well ask if there's a way to turn historyOfPresentIllness to history-of-present-illness. That's from java to clojure style.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

5 Answers5

322

Two plugins offer this feature:

I use a plugin called String Manipulation which does what you need (and more).

Select historyOfPresentIllness and press Alt / option+M to bring up the plugin menu, then press:

  • 5 - To snake_case (or to camelCase) which converts to history_of_present_illness
  • 6 - To hyphen-case (or to snake_case) which converts to history-of-present-illness

To make this easier, you could set up a shortcut at File | Settings | Keymap.


There also is the CamelCase plugin.

SHIFT+Alt / option+U toggles the selection between formats:

historyOfPresentIllness --> history_of_present_illness --> HISTORY_OF_PRESENT_ILLNESS --> HistoryOfPresentIllness --> historyOfPresentIllness

You can also undo your changes (now that a bug in the plugin got fixed).

Claes Mogren
  • 2,126
  • 1
  • 26
  • 34
DannyMo
  • 11,344
  • 4
  • 31
  • 37
  • 1
    @tieTYT Yeah, it's pretty annoying because you get a pop-up message telling you that the file has changes that cannot be undone...if not for that, it would have been the more convenient choice. – DannyMo Jun 27 '13 at 19:06
  • 2
    @tieTYT Just occurred to me that to simplify the first approach, you could record a macro `Edit > Macros > Start recording macro` and then add a simple key-mapping for your macro `Settings > Keymap > Macros > YourMacro (right-click > Add Keyboard Shortcut)` – DannyMo Jun 27 '13 at 20:43
  • String Manipulation now also contains actions to toggle between all kinds of cases...You can setup a shortcut to any action (there are too many of them to have some default). – Meo May 27 '15 at 22:02
  • @DannyMo - For me, it's `5` for `SCREAMING_SNAKE_CASE` and `J` is for `Trim all spaces`. Using Mac OS X 10.10.5, PhpStorm 10.0.1, String Manipulation 4.1.135.445.1. – Pang Nov 19 '15 at 09:54
  • I can confirm that the CamelCase plugin works fine with IntelliJ 2016.2.3 – Wim Deblauwe Sep 09 '16 at 11:11
  • Both plugins only renames selected variables but aren't searching for all usages of these variables. Is it only me or you have the same behavior? Is it possible to rename a variable with all of its usages? – Defozo Jun 23 '17 at 15:30
  • 1
    Oh, nevermind! Just click (or make a macro): SHIFT + F6 (refactor), then change case with one of the plugins and then hit ENTER. – Defozo Jun 23 '17 at 15:34
99

Very simple press Clr + F to open Find/Replace panel and check [✓] Regex copy past regex

Find: [_]{1,1}([a-z])

Replace: \U$1

Press [Replace all] button, Enjoy


Thanks @piotrek for _some_awe_var to _someAweVar

Use Find: (\w)[_]{1,1}([a-z])
Replace: $1\U$2

Qamar
  • 4,959
  • 1
  • 30
  • 49
  • 6
    Just for completeness sake, from camelCase to snake_case: find `([A-Z]{1,1})` and replace by `_\l$1`, with both *Regex* and *Match case* optiones toggled. – dic19 Feb 26 '19 at 22:09
  • This is great but in TypeScript, often private members begin with an `_` and this will make them begin capitalized. – Stack Underflow Jul 30 '19 at 21:03
  • 1
    @StackUnderflow use *Find:* `(\w)[_]{1,1}([a-z])` *Replace:* `$1\U$2` – Qamar Aug 01 '19 at 07:42
  • 1
    I suppose that "very simple" is sarcasm, isn't it? I chose rather the *String Manipulation* plugin from the accepted answer :) – Honza Zidek Oct 05 '21 at 18:47
  • 1
    I think (\w)_([a-z]) would be simpler? No need for [ ] and { } just for one symbol. – akvyalkov Dec 01 '21 at 12:16
22

From snake_case to CamelCase

  • Find: (\w)[_]{1,1}([a-z])
  • Replace: $1\U$2
  • Settings:
    • Match Case
    • Regex

From CamelCase to snake_case:

  • Find: ([A-Z])
  • Replace: \_\L$1
  • Settings:
    • Match Case
    • Regex
Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
  • 1
    For your CamelCase to snake_case, you better make sure you have something selected. As written it will replace every single capital letter in your project to a lowercase proceeded by an underscore. That seems bad to me. At very least you need to limit it to something like `(?<=[a-z])([A-Z])` to get only capitals that follow a lowercase. – Rex Schrader Sep 24 '20 at 02:14
  • Thanks! Just copied a lot of snake_case fields from database schema and using this it really saved me a bit of time. Especially great that you can just navigate through every match manually – JustAnotherCurious Mar 17 '21 at 13:31
  • How would one adapt the camelCase to snake_case solution so that it could be used within an XML context? For example: from `` to ``. – payne Aug 09 '21 at 18:20
7

If you are OK with PyCharm also refactoring usages, launch the "Rename" tool (Refactor > Rename). The window pops up with drop down list and you should see the snake_case version of the text in the list (you can open the window and switch to the snake_case with key-strokes so it can be pretty fast).

Rafe
  • 1,937
  • 22
  • 31
5

Answer above is almost perfect, but note that it will change variables like _something or this._something into Something and this.Something. I didn't want that in my case, as leading _ was used to denote "private" variables (old JS project). I slightly modified this approach:

Find: (\w)[_]{1,1}([a-z])

Replace: $1\U$2

This will ensure that only variables with _ is in the middle will be affected.

piotrek
  • 157
  • 1
  • 5
  • 2
    "above" is meaningless because answers can be sorted - your "above" is not everyone's "above" so please avoid using "above" or "below" to describe content on this site, instead give the author's name, thanks – Eric Aya Feb 13 '19 at 16:52
  • The "above" answer referenced is [@Qamar](https://stackoverflow.com/a/46645833/3291390) – Stack Underflow Jul 30 '19 at 21:07