'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?
The feature I'm looking for is implemented in Notepad++, but I'd prefer not to have to switch editors for this task.
Solution 1:[1]
Solution 2:[2]
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.
The v1.55 pre-release notes mention a "checkbox", but it isn't in Insiders Build so I think they mean the icon.
This is in the Stable Build v1.53 behind an experimental "flag":
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. It is not enabled by default in the Stable Build (also requires a reload after enabling).
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.
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).
Solution 3:[3]
"Search: target particular set of files #20530" located here: https://github.com/Microsoft/vscode/issues/20530
Is tracking a search "scope" such as "all open files".
Solution 4:[4]
Solution 5:[5]
I don't think there's a way to do this. You can make a feature request on github. I think an extension could do it though.
Solution 6:[6]
There is a checkbox for this in the "files to include" field since version 1.55 (march 2021).
Solution 7:[7]
The best way to find something you are searching for within a specific file in vs code would be:
- Ctrl + p then pressing
@
on the search bar.
It will give you a list of all functions on the file, which makes it easier to track something down.
Solution 8:[8]
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. This would be in one of these locations depending on which OS you are running:
- Linux: /tmp/ (someone else running Linux can verify this)
- macOS: ~/Library/Application\ Support/Code/Backups/
- Windows: %APPDATA%\Code\User\
Solution 9:[9]
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. The directory .
is the directory where VSCode is opened ie the root directory. The filter ./
excludes all files in the VSCodes root directory so all files are excluded. However opened files ignore any exclude filter. So if you exclude all files only the opened files will be searched.
Solution 10:[10]
This extension is what I use for this purpose. You may need to change the keybinding if there is a conflict.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow