How can I count a specific variable in the code in Visual Studio? - visual-studio

HI
I understand that we can search specific word/Variable in VS2010 editor. For example, If I used a variable called (MyTest) and I want to count how many times I used this variable/word in the code of say (10000) lines.
If possible, how can we do it in the current form or count the word in all form the project?
Thanks!

Assuming you wont to search for "MyVariable" press Ctrl+H and the Find/Replace Dialog should open. Enter "MyVariable" into the "Find what" field and again into the "Replace with" field. Set the Scope to "Current Document" and press the "Replace all" button. A message will popup telling "xxx occurrence(s) replaced.", where xxx is the number you are looking for.
Press the "undo"-Button once, if you want to revert the document-state to unchanged.

Can you not highlight the variable and press Shift+F12? This will tell you all of the matches found.

If you want to find the text "MyTest", here is a hacky way to do it.
Use the find dialog (CTRL F) to search for the text. You can modify the scope. eg Document, project etc
Click Bookmark all
Then open the Bookmarks window (View-Bookmark window) to see a list of all the bookmarks. It doesn't show the total number but you could easily count them manually here.

Ctrl + Shift + F to pull up the Find in Files dialog
Enter your search variable name and scope (document, project, solution etc)
Find all
Scroll to the bottom of the resulting Find Results and you'll see Matching lines: xxx

Related

How to search and bookmark multiple strings at once in emeditor?

I want to search and bookmark multiple string at a same time because it will save a lot of my time.
I have a huge text file of 12gb it has 214M lines. I wish to search and bookmark the lines which contains any one of the following;
gmail
yahoo
aol
hotmail
comcast
Its taking huge time to search and bookmark each one of them I was wondering if there was a way to do it in a single go.
You can use the Batch Find feature of EmEditor. To do this:
In EmEditor, click Ctrl+F to bring up the Find dialog box.
Click the Batch >> button if the dialog box has not been extended already.
Copy all the text you want to search for, separated by a newline, (In your sample, copy all 5 lines, between gmail and comcast) to the Clipboard.
Press Ctrl+V after you click the Batch list on the right side of the Find dialog box.
If necessary, press Ctrl+A, right-click on the list, and select Match Case or Match Whole Word.
Click the "Batch Bookmark" button.

What is the Visual Studio search/replace free entry "look in" field for?

In Visual Studio 2015 "Find and Replace" window, you can freely type anything you want into the "Look in" field:
The preset dropdown values are things like:
Current Window
All open documents
Entire solution
etc.
What is the purpose of allowing typing in this field? What could I possibly type in here which would make any sense?
And if I do type something like "foo bar" in there, what is the behavior when one of the "Find..." buttons is clicked? It seems to just search normally - maybe in the current solution (??).
Note - it even seems like mistaken entries can get stuck in there.
That allows one to specify directories. Or even multiple directories. Or even combinations of built in locations and directories. Separate each location with a semicolon.
Example:
Entire Solution;C:\Source\18.1
Then to remove an entry, click on the ... button. This will bring up a dialog box titled "Choose Search Folders". Then on the right hand side, selected the entry you wish to remove and then click on the button that looks like a less- than, or left-angle bracket. This will remove the entry.

How can I find and replace inside a selection in Xcode?

In Xcode < 4, you could hold the "option" key, and the "Replace All" button would change to "Replace in Selection". As of Xcode 4, this does nothing. Anyone know if there's a new way to do it, or is it bug filing time?
This appears to be working again now, at least in Xcode 4.4.1.
When the find/replace bar appears at the top of the editor, holding down the option key on the keyboard causes "Replace in Selection" to appear in lieu of "Replace All."
I'm glad, because this was an ANNOYING omission.
Another workaround:
In Xcode, select the text, press copy
In a terminal session:
pbpaste|sed 's/SOURCETEXT/NEWTEXT/g'|pbcopy
Return to Xcode window, press paste
Since the original should still be selected, it will just be replaced. You could probably build a simple shell script to do this.
Doug
An few images to supplement the chosen answer:
And holding down Option:
See also
Find/Replace in Xcode using Regular Expressions
Seems like missing functionality. You should file a bug report.
I'm upset that they took out this functionality, as I used it constantly, but here's my workaround. Copy your selected text from Xcode4 to TextEdit or some other word processor, do the find and replace there, and then copy the results back into Xcode.
It's not sexy but it's worth it if you do a lot of these "find and replace on my selection", and you leave the word processor open in Spaces as you work.
They should add "my selection" as an alternative to "workspace" and "my scope".
There is another way only replace the matches you find, rather than just this one or all of them.
I suggest you save a copy first, just in case....
In Find and Replace, Show Find Options (you can do this by pressing the magnifying glass).
Press Preview.
Uncheck all the ones you don't want replacing.
Press Replace
Hope that helps, it did me.
Not ideal, but not too bad:
Do a find and replace in workspace (cmd-opt-shift-f) enter your desired find/replace
Enter your desired search term and hit return
Select the range of replacements from the list of matches on the left
Hit replace (not replace all)
To replace text in a selection using Xcode 9
Press Option-Command-F to bring up the find/replace box.
Enter the search and replace string. Changing the search string will lose any existing selection, so..
Make your selection (again). (If you don't do this, the selection will be the first search string found only)
Hold down the key and "Replace All" will change to "Replace Selection", then click it.
Once you understand that you make your selection AFTER you have entered the search string, then this is not that clumbersome and works fine.
I find alt-command-f easier for local find and replace (4.3) and then working around your selection.
EthenA.Wilson asked in a comment to the OP a couple of days ago:
"Is there a way to do this in Xcode 5?"
For the benefit of those who, like me, had been searching for it, here's how:
After you put your Find and Replace terms in the bars at the top left-hand side of the editor page, select the text you want to search in, then look at the top right-hand side (same bar). You'll see where it says "All", right next to "Replace." Now press the Option key. "All" will change to "All in Selection." Click it, and you're done. Could be a bit more intuitive, but the functionality is there in Xcode 5.
Naturally, good idea to take a snapshot before you click!
HTH!
Not sure which feature prior to Xcode 4 you're referring to, but the shortcut Command+Shift+E gives you "Use Selection for Replace". If you're talking about "Find and Replace in Workspace" (Command+Option+Shift+F), then what you need to do is run your find and then hold down "Shift" or "Command" on the selections shown and then hit "Replace".

Shortcut for Replace button in "Find and Replace" dialog

I call up Find and Replace dialog with Ctrl+H. On F3 it finds the result but then I must hunt the Replace button using the mouse. And on every new find, it changes the screen position. Is there any way to use this dialog using only keyboard?
It will save me a few precious seconds multiplied by some very large number (I'm in the middle of heavy refactoring).
In visual studio 2015:
Ctrl+H opens the Replace dialog.
Ctrl+F opens the Find dialog.
Within that dialog:
Alt+N - Focus "Find" entry field
Alt+P - Focus "Replace with" entry field
Alt+C - Toggle Match Case
Alt+W - Toggle Whole Word
Alt+E - Toggle Regular Expressions
Alt+L - Open "Look In" dropdown list. Use Up/Down and Enter to select from this list:
Current Block
Selection
Current Document
All Open Documents
Current Project
Current Solution
Alt+R - Replace Next
Alt+A - Replace All
Esc - Dismiss dialog
For example, to replace "foo" with "bar" within the current selection:
Ctrl+H - Opens the Replace dialog, with focus on "Search Term".
Enter "foo"
Alt+P - Focus is now on "Replacement Term"
Enter "bar"
Alt+L, choose "Selection", press Enter.
Alt+W - Whole word only
Alt+R - Replace first term
Esc - Close replace dialog.
Sorry,
As microsoft there is no keyboard shortcut as Replace Next,
You can check this url
http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx
You could simply copy your desired replacement text before starting to F3 through your source text, and hit Ctrl+V when you hit the next match as VS selects the match text for you.
It is a hack, but it works.
If you dock the find/replace window then it will not move with each new find.
You can then alternate with 1 finger on F3 to move to next find, while hovering the mouse over the non-moving replace button to replace what you need.
It is possible to deactivate Resharper default behavior of "Alt+R" which provides a quick access to Resharper menu in Visual Studio.
Just uncheck it in Resharpers options:
enter image description here

Save searches in Visual Studio

Is there a way to save a search information in Visual Studio. For example if I'm constantly searching for "This string", match case, match whole word, look in 'Current Project'. Can I store this somehow as a saved search?
This is easy to do with a macro. Start with Tools + Macros + Record. Do a search, setting all the options you want, click Stop Recording. View + Other Windows + Macro Explorer. Rename the "TemporaryMacro" to something more suitable. Tools + Customize + Keyboard and assign a keystroke to the macro.
You now have a single keystroke to execute the search.
I found that if you do the following in VS2012:
1) Copy and paste the file types list back into the "Look at these file types" search box
2) Click and unclick the "Use Regular Expressions" checkbox
3) Press "Find Next" before "Find All"
4) Close visual studio and reopen the solution
For the actual search string I think if you make sure your cursor is positioned on a blank line, it will also reuse the last search string, otherwise it will pick the string under the cursor.
Then (at least VS2012) will remember the last search options. You may or may not also need to click your heels and perform some voodoo magic that i was doing at the time on the side (that part is a secret, sorry)

Resources