37

Is it possible to replace to upper case in Visual Studio using "Find and Replace" dialog and RegEx (?) à la: . => Upper(.)?

Say I have:

m_<b>a</b>blabla

I want:

_<b>A</b>blabla
abatishchev
  • 98,240
  • 88
  • 296
  • 433
serhio
  • 28,010
  • 62
  • 221
  • 374
  • How many occurrences of the expression are there? If the number is not huge, then I would probably create an ad-hoc temporary macro to do this. – RickL Apr 30 '10 at 10:58
  • @RickL: If you create a macro, the number of expressions should it matter?! I just wonder if possible with Find/Replace. – serhio Apr 30 '10 at 11:14
  • Kind of yes, because I would create a macro which would find the next string and uppercase it appropriately. So you would need to run the macro multiple times to replace all the expressions. I guess if it's a lot, then it's easy to edit the macro and put a for.. loop in. I'll answer below how to solve this using temporary macros. – RickL Apr 30 '10 at 13:32

3 Answers3

30

You can solve this by using Visual Studio temporary macros. This is a very powerful, flexible feature which I use all the time for performing repetitive code manipulations.

I'm assuming you're using the C# default key bindings here.

  1. Press CTRL+SHIFT+F to bring up the find in files dialogue.
  2. Click use "Regular expressions"
  3. Set "Find what:" to "<m_:Ll" - words that begin with m, underscore, then a lower case letter;
  4. Click "Find all" to search for all occurrences;
  5. Press CTRL+SHIFT+R to start recording temporary macro;
  6. Press F8 to find next occurrence of search expression;
  7. Press right cursor, right cursor, SHIFT + right cursor (to skip "m_" and then select the lower case letter);
  8. Press CTRL+SHIFT+U to uppercase the lower case letter;
  9. Press CTRL+SHIFT+R to stop recording temporary macro;
  10. Press CTRL+SHIFT+P to replay temporary macro, which will jump to next expression and uppercase the first letter after the "m_". You need to press CTRL+SHIFT+P as many times as there are expressions.
serhio
  • 28,010
  • 62
  • 221
  • 374
RickL
  • 2,811
  • 3
  • 22
  • 35
  • 15
    Worth noting macros have been removed from VS2012 ... no can do any more :( Used to use these all the time. – noelicus Sep 11 '13 at 10:14
  • 6
    I used Notepad++ for this since it's not in VS2012: http://stackoverflow.com/questions/1039226/regex-to-change-to-sentence-case – northben Jan 10 '14 at 16:30
  • 1
    [Adding macro support for VS2012/2013/2015](http://stackoverflow.com/a/13353364/1548895) – Vadim Ovchinnikov Dec 26 '16 at 12:41
  • Would it be possible to use a macro to "replace all" 26 times (one for each letter)? Then you'd only have to run the macro 26 times, or maybe only once if you can do it in a single macro. – mbomb007 Aug 23 '17 at 15:24
  • There are no alternatives for macros at the moment, the [Text Macros for Visual Studio 2012-2017 extension](http://visualstudiogallery.msdn.microsoft.com/8e2103b6-87cf-4fef-9410-a580c434b602) only support text actions, so nothing about *find next occurence*, but it does uppercase for example. – Thomas LAURENT Oct 12 '18 at 11:15
  • For Visual Studio 2019: https://marketplace.visualstudio.com/items?itemName=XavierPoinas.TextMacrosforVisualStudio201220132015 – Matt Arnold Dec 08 '20 at 11:57
10

If you use Visual Studio Code (instead of Visual Studio) you can use the modifiers: \u\U\l\L

  • Sample text: m_<b>a</b>blabla
  • Find: m_<b>(.*)</b>
  • Replace: m_<b>\U$1</b>
  • Sample text after replace: m_<b>A</b>blabla

Note: This is only possible in Visual Studio Code 1.29 and later (released August 2020). See https://code.visualstudio.com/updates/v1_49#_case-changing-in-regex-replace

DBolton
  • 506
  • 4
  • 9
9

No, Visual Studio does not support that. For a reference of the regular expressions capabilities in VS check:

Regular Expressions (Visual Studio)


(Original answer, given due to misinterpreting the original question)

Assuming Visual Studio C# Default key bindings.

There are different ways you can achieve this.

If it's a (variable, method, property, etc) you can use the Rename refactoring to change all instances. This refactoring is invoked by pressing F2 key while on the instance you want to rename.

If you perform the change on the definition itself you can also use SHIFT+ALT+F10 to invoke the active refactorings popup and then do the rename all instances.

If it's a string literal you can use the shortcut CTRL+U (lowercase) and CTRL+SHIFT+U (uppercase) to rapidly switch the case of the selection. This is valid for all text shown in the editor, but most useful for string literals.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • @serhio, I'm afraid Visual Studio only allows you to create tagged expression that you can then reference (/1, /2, etc), but does not support to change those values on the fly. – João Angelo Apr 30 '10 at 11:11
  • 2
    FWIW: [VSCode can](https://code.visualstudio.com/updates/v1_47#_editor). Copy to it, do your magic and then copy back. – LosManos Aug 21 '20 at 07:31
  • @LosManos This is pure gold! Whoever approved this change request needs to be given the Turing Award IMO! There's only one thing missing from the feature: the ability to use it in the match string as well! Do you know whether there are any plans to implement this? – Kenny83 Nov 07 '20 at 21:03