60

I'm using Visual Studio Code 1.14.2, and I'm trying to change name of variables to camelCase eg. set_nominal_wavelength to setNominalWavelength.

Regular expression: _([a-z])

Replace: \U1ドル\E

does not work. Any idea how to achieve it?

asked Aug 2, 2017 at 12:58
6
  • 2
    Use Notepad++, it will work there. Commented Aug 2, 2017 at 12:59
  • 1
    Ok. But is it possible in VScode? Commented Aug 2, 2017 at 13:07
  • 1
    No, it is not possible. Why not just write some C#/JS/PHP etc. code to handle that? It is really a very common issue that can easily be solved with a bit of code. Well, or, as I say, just download a free copy of NPP and enjoy this solution. Commented Aug 2, 2017 at 13:08
  • 2
    Well, I've changed it using sed in terminal. Just been curious if it's possible in VScode. Commented Aug 2, 2017 at 13:13
  • 10
    Good question❗️ I shouldn't need go out from VSCode to do such simple Regex transformation 😢 Commented May 2, 2019 at 0:25

3 Answers 3

69

As of vscode v1.75 there is a Transform to Camel Case command. So you could

  1. Find: (_[a-z]+)+
  2. Alt+Enter will select all those
  3. trigger the Transform to Camel Case command

Pretty easy.


In the 1.47 Insiders Build support for the replace case modifiers (\L, \l, \U, \u) has been added to vscode. And so should be in the 1.47 stable release).

So simply doing your find: _([a-z])

and replace with \u1ドル (since you only want to capitalize the first letter) works nicely in the Insiders Build now.

case modifier demo

Works in both the Find Widget and the Search Panel.

answered Jun 15, 2019 at 4:21
Sign up to request clarification or add additional context in comments.

10 Comments

No need for a snippet here. You can just use ctrl-shift-l, then press escape to clear the find dialog, then use to-uppercase command via pallet.
Sounds like two extra steps though - open the command palette, type to find the command, and trigger it. Instead of just triggering the snippet command. That is assuming you will want to camelCase in the future.
What a nice animated gif!
The keystroke visibility is through vscode's own setting: Toggle Screencast Mode and for the gif I use screentogif.com which works well enough but sometimes gets confused by keystroke input. There are 2 vscode screencast settings to look at too: vertical offset and Only Keyboard Shortcuts (so just control keychords are shown not everything like text you might type).
@Rudey After your comment, support for case modifiers was added to the search across files panel.
|
59

There is a workaround:

  1. Open Replace dialog and enter regex: _([a-z])
  2. Then move focus to the editor area and press Ctrl+F2 ("Change All Occurrences")
  3. Then change case of selection (Ctrl+P>upper)
  4. Then press Left Arrow key and press Delete key
Bernardo Dal Corno
2,1581 gold badge27 silver badges31 bronze badges
answered May 22, 2018 at 12:08

5 Comments

Too bad you can't do this if you're working with global search though.
Saved me soo much time
For Mac OS it is CMD+F2 and CMD+Shift+p
for windows 10, it is Ctrl+F2 and Ctrl+Shift+p
Check out Mark's submission below because this limitation has since been resolved in recent VS Code updates. I just confirmed that working as of today (v 1.60.2 - MacOS)
2

You may use other tools that support change case operators, like Notepad++, sed, R (gsub with perl=TRUE), but VS Code does not support these operators in the replacement pattern.

See this feature request on GitHub:

This is cool to have. This is beyond the scope of what is currently supported by javascript.

We need to come up with our own advanced replace engine to support these cases.

answered Aug 2, 2017 at 13:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.