简体   繁体   English

如何仅在 vscode 中当前打开的文件中进行搜索?

[英]How to search only in currently open files in vscode?

I usually work on larger projects with many files that would contain the search query.我通常处理包含许多包含搜索查询的文件的大型项目。 So I would open the files I want to modify and do a find/replace across all currently opened files.所以我会打开我想要修改的文件,并在所有当前打开的文件中进行查找/替换。 Can this be done in vs code?这可以在vs代码中完成吗?

The feature I'm looking for is implemented in Notepad++, but I'd prefer not to have to switch editors for this task.我正在寻找的功能是在 Notepad++ 中实现的,但我不希望为此任务切换编辑器。

For me somehow it works just by typing ./ in the files to exclude field.对我来说,它只需在文件中键入./即可排除字段。

这里

"Search: target particular set of files #20530" located here: https://github.com/Microsoft/vscode/issues/20530 “搜索:目标特定文件集#20530”位于此处: https : //github.com/Microsoft/vscode/issues/20530

Is tracking a search "scope" such as "all open files".正在跟踪搜索“范围”,例如“所有打开的文件”。

As of v1.55 this will be enabled by default - the icon will always appear, you do not need to enable the setting mentioned below.从 v1.55 开始,这将默认启用 - 图标将始终出现,您不需要启用下面提到的设置。

The v1.55 pre-release notes mention a "checkbox", but it isn't in Insiders Build so I think they mean the icon. v1.55 预发布说明提到了一个“复选框”,但它不在 Insiders Build 中,所以我认为它们是指图标。


This is in the Stable Build v1.53 behind an experimental "flag":这是在 Stable Build v1.53 中的一个实验性“标志”后面:

Search > Experimental: Search in Open Editors

It is enabled by default in the Insiders' Build but requires a reload to take effect - so that the relevant option icon will appear.它在 Insiders' Build 中默认启用,但需要重新加载才能生效 - 这样就会出现相关的选项图标。 It is not enabled by default in the Stable Build (also requires a reload after enabling).它在稳定版本中默认不启用(启用后也需要重新加载)。

v1.53 Release Notes : v1.53 发行说明

This release comes with experimental support for searching in open editors, which has been a highly requested feature for some time.此版本附带了对在开放编辑器中搜索的实验性支持,一段时间以来一直是人们强烈要求的功能 This feature is enabled by default in Insiders, and can be enabled with the search.experimental.searchInOpenEditors setting in Stable.此功能在 Insiders 中默认启用,并且可以通过 Stable 中的search.experimental.searchInOpenEditors设置启用。

See demo:见演示:

在打开的编辑器演示中搜索

As you can see, the icon is at the end of the files to include input so you will have to have that showing (click the three dots ... just below the search options if files to include is not already showing).如您所见,该图标位于files to include末尾files to include输入,因此您必须显示该图标(如果files to include尚未显示,请单击搜索选项下方的三个点... )。

I don't think there's a way to do this.我不认为有办法做到这一点。 You can make a feature request on github.您可以在 github 上提出功能请求。 I think an extension could do it though.我认为扩展可以做到这一点。

As an alternative to using Visual Studio Code itself, you can just search the file contents within the folder where unsaved files open in Visual Studio Code are located.作为使用 Visual Studio Code 本身的替代方法,您可以在 Visual Studio Code 中打开的未保存文件所在的文件夹中搜索文件内容。 This would be in one of these locations depending on which OS you are running:这将位于以下位置之一,具体取决于您运行的操作系统:

  • Linux: /tmp/ (someone else running Linux can verify this) Linux:/tmp/(运行 Linux 的其他人可以验证这一点)
  • macOS: ~/Library/Application\\ Support/Code/Backups/ macOS:~/Library/Application\\ Support/Code/Backups/
  • Windows: %APPDATA%\\Code\\User\\ Windows:%APPDATA%\\代码\\用户\\

1.55 版(2021 年 3 月)以来,“要包含的文件”字段中有一个复选框。

The answer was already mentioned (and accepted above) indeed typing ./ in the files to exclude box of the search window will allow you to only search in open files.答案已经提到(并在上面接受)确实在搜索窗口的文件排除框中键入./将允许您仅在打开的文件中进行搜索。

However the response mentioned that he was unsure why this worked.然而,回应提到他不确定为什么会这样。 VSCode allows you to exclude all files in a directory by writing the directory in the exclude box. VSCode 允许您通过在排除框中写入目录来排除目录中的所有文件。 The directory .目录. is the directory where VSCode is opened ie the root directory.是VSCode打开的目录,即根目录。 The filter ./ excludes all files in the VSCodes root directory so all files are excluded.过滤器./排除 VSCodes 根目录中的所有文件,因此排除所有文件。 However opened files ignore any exclude filter.但是打开的文件忽略任何排除过滤器。 So if you exclude all files only the opened files will be searched.因此,如果您排除所有文件,则只会搜索打开的文件。

The best way to find something you are searching for within a specific file in vs code would be:在 vs 代码中的特定文件中查找您正在搜索的内容的最佳方法是:

  • Ctrl + p, then pressing @ on the search bar. Ctrl + p,然后按搜索栏上的 @。

It will give you a list of all functions on the file, which makes it easier to track something down.它将为您提供文件中所有功能的列表,以便更轻松地跟踪某些内容。

This extension is what I use for this purpose.这个扩展是我用于此目的的。 You may need to change the keybinding if there is a conflict.如果存在冲突,您可能需要更改键绑定。

Hit Ctrl + Shift + F and enable the icon at the end of the 'file to include' text field按 Ctrl + Shift + F 并启用“要包含的文件”文本字段末尾的图标

VS Code 搜索工具栏

I usually work on larger projects with many files that would contain the search query.我通常在大型项目中使用许多包含搜索查询的文件。 So I would open the files I want to modify and do a find/replace across all currently opened files.因此,我将打开要修改的文件,并对所有当前打开的文件进行查找/替换。 Can this be done in vs code?可以在vs代码中完成吗?

The feature I'm looking for is implemented in Notepad++, but I'd prefer not to have to switch editors for this task.我要寻找的功能是在Notepad ++中实现的,但我不想不必为此任务切换编辑器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM