English 中文(简体)
Visual Studio:有没有一种方法可以用一个快捷方式执行“在文件中查找”?
原标题:Visual Studio : Is there a way to Perform a "Find in files" with one single shortcut?

I want to select an expression in code and type Ctrl+Whatever
so it has the same result as [ Ctrl+Shift+F AND Clicking on "Find All" ]

编辑:[Ctrl+Shift+F AND Hitting Enter]可能比点击更快,但我仍然想要更具体、更快的东西

备注:我对查找所有引用快捷方式不感兴趣。

最佳回答

你可以使用一个宏。我在VS2010中录制并修改了一个:

Sub FindAllFiles()
    DTE.Find.FindWhat = DTE.ActiveDocument.Selection.ToString()
    DTE.Find.Target = vsFindTarget.vsFindTargetFiles
    DTE.Find.MatchCase = False
    DTE.Find.MatchWholeWord = False
    DTE.Find.MatchInHiddenText = True
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    DTE.Find.SearchPath = "Entire Solution"
    DTE.Find.SearchSubfolders = True
    DTE.Find.FilesOfType = ""
    DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
    DTE.Find.Action = vsFindAction.vsFindActionFindAll
    If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
        Throw New System.Exception("vsFindResultNotFound")
    End If
End Sub

可以将宏设置为键盘快捷键。请参阅:http://msdn.microsoft.com/en-us/library/a0003t62(v=对80).aspx

问题回答

我并没有意识到这一点。Ctrl+Shift+ENTERENTER而不是点击“Find All”)可能是最接近的。如果你是一个打字高手,它就像一个快捷键一样快。

更新

现在,这个问题已经改变了,我的答案再也没有意义了。使用类似Fosco回答了这个问题。

我有一个类似的宏在使用,比如@Fosco。

  Members for the search methods
Private matchCase As Boolean = True
Private searchWindowOne As Boolean = False

Public Sub SearchFiles(ByVal fileTypes As String, ByVal searchPath As String)
    searchWindowOne = Not searchWindowOne
    DTE.Find.Target = vsFindTarget.vsFindTargetFiles
    DTE.Find.MatchCase = matchCase
    DTE.Find.MatchWholeWord = matchWholeWord
    matchCase = True
    matchWholeWord = True
    DTE.Find.MatchInHiddenText = True
    DTE.Find.Action = vsFindAction.vsFindActionFindAll
    DTE.Find.SearchPath = searchPath
    If (searchWindowOne) Then
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
    Else
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults2
    End If
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    DTE.Find.SearchSubfolders = True
    DTE.Find.FilesOfType = fileTypes
    DTE.Find.FindWhat = GetClipboard()
    If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
        Throw New System.Exception("vsFindResultNotFound")
    End If
End Sub

Public Sub ChangeMatchCase()
    matchCase = False
    matchWholeWord = False
End Sub

It adds a little more flexibility to the original approach. One of the good things is that is searches into both find windows alternating. That means your last two searches are always accessible. Of course this can t be used to be directly mapped to a shortcut but it allows to do this:

Sub SearchInProject()
    SearchFiles("*.*", "Current Project")
End Sub
Sub SearchInCode()
    SearchFiles("*.h;*.cpp", "Entire Solution")
End Sub

...and so on. These can then be mapped to shortcuts and allow real one key searching. As you might have noticed I added a switch for match case that can be activated by the macro ChangeMatchCase for the next search. In my setting I mapped different searches to double keystrokes. So Ctrl+F,Ctrl+G searches globally, Ctrl+F,Ctrl+Dsearches in the project, ... you get the point. I have similar mappings for all debug stuff starting with Ctrl+D,. This was maybe the single most important performance boost I had in the last years.





相关问题
building .net applications without Visual Studio

I m interested to hear about people working with building .net applications using MSBuild, NAnt or similar tools. What are you using, why are you using it instead of the VS IDE? I like to use ...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Visual Studio 2010 Beta 2: Can I print in color?

I have to turn in a hard copy of some code with an assignment. Is there any way in Visual Studio 2010 to print C# source code with syntax highlighting? PS: The assignment is solving a math problem, ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

热门标签