查找和替换文本Find and replace text

可以使用 。You can find and replace text in the Visual Studio editor by using Find and Replace ( Ctrl+F or Ctrl+H ) or Find/Replace in Files ( Ctrl+Shift+F or Ctrl+Shift+H ). 也可通过使用 。You can also find and replace only some instances of a pattern by using multi-caret selection .


如果要重命名代码符号(例如变量和方法),最好 重构 它们,而不是使用查找和替换。If you're renaming code symbols such as variables and methods, it's better to refactor them than to use find-and-replace. 重构不仅智能而且知道应用范围,而查找和替换会盲目替换所有实例。Refactoring is intelligent and understands scope, whereas find-and-replace blindly replaces all instances.

查找和替换功能在此编辑器中、其他某些基于文本的窗口(例如“查找结果” 窗口)中、设计器窗口(如 XAML 设计器和 Windows 窗体设计器)中以及工具窗口中均提供。Find-and-replace functionality is available in the editor, in certain other text-based windows such as the Find Results windows, in designer windows such as the XAML designer and Windows Forms designer, and in tool windows.

可以将搜索范围限制到当前文档、当前解决方案或一组自定义文件夹中。You can scope searches to the current document, the current solution, or a custom set of folders. 还可以指定一组文件扩展名,用于多文件搜索。You can also specify a set of file name extensions for multi-file searches. 使用 .NET 正则表达式自定义搜索语法。Customize search syntax by using .NET regular expressions.


查找/命令框可用作工具栏控件,但默认为不可见。The Find/Command box is available as a toolbar control, but isn't visible by default. 要显示“查找/命令”框,请在“标准”工具栏上选择“添加或删除按钮”,然后选择“查找” 。To display the Find/Command box, select Add or Remove Buttons on the Standard toolbar, and then select Find .

“查找和替换”控件Find and Replace control

  • 按 Ctrl+F 作为查找当前文件中的字符串的快捷方式 。Press Ctrl+F as a shortcut to find a string in the current file.
  • 按“Ctrl”+“H”作为快捷方式,以查找和替换当前文件中的字符串 。Press Ctrl+H as a shortcut to find and replace a string in the current file.

“查找和替换”控件显示在代码编辑器窗口的右上角 。The Find and Replace control appears in the upper right corner of the code editor window. 此操作会立即突出显示给定搜索字符串在当前文档中的每个匹配项。It immediately highlights every occurrence of the given search string in the current document. 通过在搜索控件上选择“查找下一个”按钮或“查找上一个”按钮,可以从一个匹配项导航到另一个匹配项 。You can navigate from one occurrence to another by choosing the Find Next button or the Find Previous button on the search control.

在 Visual Studio 中查找和替换

通过选择“查找”文本框旁边的按钮,可以访问替换选项 。You can access replacement options by choosing the button next to the Find text box. 若要一次替换一个,请选择“替换”文本框旁边的“替换下一个”按钮 。To make one replacement at a time, choose the Replace Next button next to the Replace text box. 若要替换所有匹配项,请选择“全部替换”按钮。To replace all matches, choose the Replace All button.

若要更改匹配项的突出显示颜色,请依次选择“工具”菜单、“选项”、“环境”、“字体和颜色” 。To change the highlight color for matches, choose the Tools menu, select Options , and then choose Environment , and select Fonts and Colors . 在“显示设置对象”列表中,选择“文本编辑器”,然后在“显示项”列表中,选择“查找突出显示项(扩展名)” 。In the Show settings for list, select Text Editor , and then in the Display items list, select Find Highlight (Extension) .

搜索工具窗口Search tool windows

通过选择“编辑” > “查找和替换”或按 Ctrl+F,可以在代码或文本窗口(如“输出”窗口和“查找结果”窗口)中使用“查找”控件 。You can use the Find control in code or text windows, such as Output windows and Find Results windows, by selecting Edit > Find and Replace or pressing Ctrl+F .

某些工具窗口也提供某版本的“查找”控件 。A version of the Find control is also available in some tool windows. 例如,通过在搜索框中输入文本可以在“工具箱”窗口中筛选控件列表 。For example, you can filter the list of controls in the Toolbox window by entering text in the search box. 可以在其中搜索内容的其他工具窗口包括“解决方案资源管理器”、“属性”窗口和“团队资源管理器” 。Other tool windows that allow you to search their contents include Solution Explorer , the Properties window, and Team Explorer .

“在文件中查找”和“在文件中替换”Find in Files and Replace in Files

  • 按 Ctrl+Shift+F 作为查找多个文件中的字符串的快捷方式 。Press Ctrl+Shift+F as a shortcut to find a string in multiple files.
  • 按 Ctrl+Shift+H 作为查找和替换多个文件中的字符串的快捷方式 。Press Ctrl+Shift+H as a shortcut to find and replace a string in multiple files.

“在文件中查找/替换”与“查找和替换”控件类似,区别在于可以定义搜索范围 。Find/Replace in Files works like the Find and Replace control, except that you can define a scope for your search. 不仅可以搜索当前在编辑器中打开的文件,还可以搜索所有打开的文档、整个解决方案、当前项目,及所选文件夹集。Not only can you search the current open file in the editor, but also all open documents, the entire solution, the current project, and selected folder sets. 还可以按文件扩展名搜索。You can also search by file name extension. 要访问“在文件中查找/替换”对话框,请在“编辑”菜单上选择“查找和替换”(或按 Ctrl+Shift+F) 。To access the Find/Replace in Files dialog box, select Find and Replace on the Edit menu (or press Ctrl+Shift+F ).

在 Visual Studio 中的文件中查找

查找结果Find Results

选择“查找全部”后,“查找结果”窗口随即打开,并列出搜索的匹配项 。When you choose Find All , a Find Results window opens and lists the matches for your search. 在列表中选择一个结果会显示相关联的文件,并突出显示匹配项。Selecting a result in the list displays the associated file and highlights the match. 如果文件尚未打开进行编辑,则可以在选项卡右侧的预览选项卡中打开。If the file is not already open for editing, it is opened in a preview tab in the right side of the tab well. 可以使用“查找”控件在“查找结果”列表中搜索 。You can use the Find control to search through the Find Results list.

创建自定义搜索文件夹集Create custom search folder sets

通过选择“查找范围”框旁边的“选择搜索文件夹”按钮(类似于...),可以定义搜索范围 。You can define a search scope by choosing the Choose Search Folders button (it looks like ... ) next to the Look in box. 在“选择搜索文件夹”对话框中,可以指定要搜索的一组文件夹,并且可以保存规范,供以后重复使用 。In the Choose Search Folders dialog box, you can specify a set of folders to search, and you can save the specification so that you can reuse it later.


如果已将远程计算机的驱动器映射到本地计算机,则可以指定要在远程计算机上搜索的文件夹。If you've mapped a remote machine's drive to your local machine, you can specify folders to search on the remote machine.

创建自定义组件集Create custom component sets

通过选择“查找范围”框旁边的“编辑自定义组件集”按钮,可以将组件集定义为搜索范围 。You can define component sets as your search scope by choosing the Edit Custom Component Set button next to the Look in box. 可以指定已安装的 .NET 或 COM 组件,以及包含在解决方案或任何程序集或类型库(.dll、.tlb、.olb、.exe 或 .ocx)中的 Visual Studio 项目 。You can specify installed .NET or COM components, Visual Studio projects that are included in your solution, or any assembly or type library ( .dll , .tlb , .olb , .exe , or .ocx ). 若要搜索引用,请选择“查找引用”框 。To search references, select the Look in references box.

多个插入点选择Multi-caret selection


本部分适用于 Windows 上的 Visual Studio。This section applies to Visual Studio on Windows. 对于 Visual Studio for Mac,请参阅块选择For Visual Studio for Mac, see Block selection.

在 Visual Studio 2017 版本 15.8 中引入Introduced in Visual Studio 2017 version 15.8

使用“多个插入点选择”同时在两个或多个位置进行相同编辑 。Use multi-caret selection to make the same edit in two or more places at the same time. 例如,可同时插入相同的文本或修改多个位置的现有文本。For example, you can insert the same text or modify existing text in multiple locations at the same time.

在以下屏幕截图中,在三个位置选择了 -0000;如果用户按 Delete,则删除所有三个选项 :In the following screenshot, -0000 is selected in three locations; if the user presses Delete , all three selections are deleted:

Visual Studio 中 XML 文件中的多个插入点选择

要选择多个插入点,请像往常一样单击或进行第一个文本选择,然后在单击或选择其他位置的文本时按 Alt 。To select multiple carets, click or make first text selection as usual, and then press Alt while you click or select text in each additional location. 还可自动添加匹配的文本作为其他选择,或选择一个文本框以在每行上进行相同的编辑。You can also automatically add matching text as additional selections, or select a box of text to edit identically on each line.


如果已选择 Alt 作为鼠标的修改键,请单击“工具” > “选项”中的“转到定义”,禁用多个插入点选择 。If you've selected Alt as the modifier key for mouse-click Go to Definition in Tools > Options , multi-caret select is disabled.


对多个插入点选择行为使用以下键和操作:Use the following keys and actions for multi-caret selection behaviors:

快捷方式Shortcut 操作Action
Ctrl+Alt + 单击Ctrl+Alt + click 添加辅助插入点Add a secondary caret
Ctrl+Alt + 双击Ctrl+Alt + double-click 添加辅助字选择Add a secondary word selection
Ctrl+Alt + 单击 + 拖动Ctrl+Alt + click + drag 添加辅助选择Add a secondary selection
Shift+Alt+ .Shift+Alt+. 添加下一个匹配文本作为选择Add the next matching text as a selection
Shift+Alt+;Shift+Alt+; 添加所有匹配的文本作为选择Add all matching text as selections
Shift+Alt+ ,Shift+Alt+, 删除上次选择的匹配项Remove last selected occurrence
Shift+Alt+/Shift+Alt+/ 跳过下一个匹配的项Skip next matching occurrence
Alt + 单击Alt + click 添加框选择Add a box selection
Esc 或单击Esc or click 清除所有选择项Clear all selections

“编辑”菜单上的“多个插入点”下也提供了一些命令 :Some of the commands are also available on the Edit menu, under Multiple Carets :

Visual Studio 中的“多个插入点”飞出式菜单的屏幕截图

另请参阅See also