Visual Studio 2012: How to handle code replacement over multiple lines - visual-studio

I often have to replace multiple lines in multiple documents. In Visual Studio 2012 there seems to be the possibility to put muliple line code in a simple one-line-input-box only.
In the "embedded" search box there is also only a single-line input field:
After inserting the often very long search terms and trying to put a regular expressions for line breaks into it, i have to move horizontally inside a one-line-input-field which isn't comfortable at all.
Is there another way to use the find/replace functionality for replacing muliple lines in Visual Studio 2012?

In order to replace multiple lines first you have to check regular expression option on search window. In following example it's search the
first
second
third
and replace it with
fourth
fifth
sixth
search text : first([\s\n])second([\s\n])third
replace text : fourth$1fifth$2sixth

This worked exceptionally well for me:
http://vlasovstudio.com/sync-block-edit/

Related

How to make Intellisense in Visual Studio 2012 not to substitute text right to the cursor?

While programming I often realize that I need to add something before already typed code. For example I type the name of the variable:
input[0]
and then I realize that my array is of type string and I need to convert it. So, I move to the beginning of the word (with Ctrl-Left Arrow) and start typing
Convert.To|input[0]
with pipe used to show the position of my cursor. I get some suggestions from Intellisense, including the ToInt32() method I am looking for. But as long as I confirm this suggestion with Tab or Space, I get the following:
Convert.ToInt32(|)[0]
So, the text from the cursor position to the end of the word is substituted with suggestion, and this is definitely not what I want.
This problem is not specific for VS 2012 and might be due to some extensions I have installed, but my attempt to pursue its origin did not yield anything. I have following extensions installed: ReSharper, PowerCommands, Productivity Power Tools.
If you are entering an unrelated expression before an identifier, add a space before you start typing the new expression. This will prevent the completion from replacing the existing identifier.
For example, if | marks the caret, the following scenario would avoid the problem you are facing.
Convert.To| input
This code completion feature is designed to prevent the insertion of incorrect identifiers. If Visual Studio behaved like some other IDEs I know of, using the code completion feature in your original example would result in the insertion of ToInt32input, which would never be valid.
If you are interested in additional thoughts regarding this feature in general, I have described this as the Extend (default for Visual Studio) and No-extend (default for NetBeans, Eclipse, and others) modes in my blog article Code Completion filtering, selection, and replacement algorithms.
A two years later answer. But it might still be useful for some.
What helped for me in VS2015 (which might also work in VS2012) is to add the a space character to the list of 'Member List Commit Characters' in the Intellisense settings.
After this the characters after the cursor are not removed by an auto-completion.

Exclude comments when searching in Visual Studio

Is there a way to perform searches (Find / Find in Files) in visual studio that will exclude matches in comments? While sometimes it is useful, other times it is the opposite. For all of the options presented, I figured it would be in there, but I can't find it if it is.
I am using VS 2010/2012 about equally by the way.
Here's the regex that works for me for newer versions of Visual Studio:
^(?![ \t]*//).*your_search_term
Note that the syntax changed as of VS 2012:
Visual Studio 2012 uses .NET Framework regular expressions to find and
replace text. In Visual Studio 2010 and earlier versions, Visual
Studio used custom regular expression syntax in the Find and Replace
windows.
Reference: https://msdn.microsoft.com/en-us/library/vstudio/2k3te2cs(v=vs.110).aspx
you could try the regex as below:
^~(:b*//).*your_search_term
Short explanation:
^ from beginning of line
~( NOT the following
:b* any number of white spaces, followed by
// the comment start
) end of NOT
.* any character may appear before
your_search_term your search term :-)
saw this at another post.
I don't believe it's an option in VS. You could try regular expressions, but those are limited by how creative you can be. It seems like it would be not entirely difficult to search for lines not beginning with // using a regex.
You probably mean any comments, both single- (//) and MULTI-LINE (/**/).
The other answers only deal with single-line-comments. So here comes my VS Code-compatible Regex for both of them at the same time:
(?<!//.*)(?!(/\*).*)YOUR_SEARCH_TERM(?![^/\*]*?\*/)
Explanation:
(?<!//.*) is a negative look-behind against single-line-comments (//)
(?!(/\*).*) invalidates your search term if it is preceded by /*
(?![^/\*]*?\*/) invalidates your search term if it is followed by */ (short of a /* between your search term and itself, because then your search term would be outside of /**/ and you want it to be matched again).
To use it in VS Code, just type Ctrl + F and tick the regex-icon in the search-field popping up (with void being your exemplary search term):

Visual Studio : Search Uncommented Code only

Is there any way to restrict the Find/Search to uncommented lines only ?
(Maybe using regex would be a good lead)
Lets say, if you need to search all occurrences of an uncommented text "VPEntity" then try using the following regular expression in Find in files after selecting the Use RegEx option
^((?!//|/\*).)*VPEntity
Hope it works for you

How to find empty method/class XML summaries in Visual Studio?

i have noticed a number of empty method and class summary sections throughout a solution. It's rather large, hundreds of files/classes in a dozen projects. The empty summaries look something like this:
///<summary>
///</summary>
My question is: How do i form a regex expression in the Visual Studio file search to find all of the empty summaries in my solution?
Thanks!
No need to get your hands dirty - just use MS StyleCop. It's free, checks (among many other things) exactly what you need and gives a detailed report about it.
HTH!
^[ \t]*/// \<summary\>\n[ \t]*/// \</summary\>
if you type, watch the spaces. There are 4: before every \t and before every \<
If you want to allow space after the first line, the regexp becomes:
^[ \t]*/// \<summary\>[ \t]*\n[ \t]*/// \</summary\>
but msstylecop may be better, i don't know it yet ;-)

Global Find and Replace in Visual Studio

I have a solution with multiple projects and we need to do some serious global replacements.
Is there a way to do a wildcard replacement where some values remain in after the replace?
So, for instance if I want every HttpContext.Current.Session[“whatevervalue”] to become HttpContext.Current.Session[“whatevervalue”].ToString() the string value being passed in will be respected? I don’t want to replace “whatevervalue” I just want to append a .ToString() where the pattern matches.
Is this possible in Visual Studio?
First, Backup your Projects, just in case... Always a good idea before mass replacements.
Then, in the Find/Replace Dialog, select the Use Regular Expressions checkbox:
In the Find box, use the pattern:
HttpContext\.Current\.Session\["{.#}"\]
and in the Replace box, use:
HttpContext.Current.Session["\1"].ToString()
Easy...use regular expressions and grouping.
Find what:
(HttpContext.Current.Session[“whatevervalue”])
Replace with:
\0.ToString();
Remember to check the Use: and select Regular expressions
You want to open the "Find Options" expander and select the "Use Regular Expressions" option. After you've done that, you want these as your find/replace entries:
Find:
HttpContext\.Current\.Session\[{("([^"]|\")*")}\]
Replace:
HttpContext.Current.Session[\1].ToString()
Additional Note:
Once you've enabled regular expressions option, you'll be able to use the right-pointing triangle buttons to access snippets of Visual Studio's Regex syntax.
Also note that Visual Studio's Regex syntax is pretty ghetto, as it hasn't changed since the days of Visual Studio 6 (or earlier?)--so don't take any syntax elements for granted.
For example, one might expect that my find regex above is broken because the backslash before the double-quote is not properly escaped, but in reality, putting a double-backslash there will break the expression, not fix it.
None of these answers seem to work in Visual Studio 2013, as that version seems to have finally made the switch to standard RegEx. However, those who are non-RegEx Experts or those who are used to the old VS Find/Replace RegEx syntax will find the RegEx Shortcut buttons very useful.
Please see this answer for more information, including Find/Replace/Surround With examples:
Visual Studio 'Find and Surround With' instead of 'Find and Replace'
You can use Visual Assist for tasks like this. It's a powerful tool for different kinds of refactoring.
You could also consider using the free download tool Refactor available at http://www.devexpress.com/Products/NET/IDETools/RefactorASP/
It does a whole lot more than just find & replace, which they call renaming members with more understandable names. Its various features will easily help you to improve your code.

Resources