admin管理员组

文章数量:1291089

I'm trying to search a string in the code base. Ctrl + Shift + F should do it, but I want to get the list of files in search results.

I tried using the VS Code mand:

mands.executeCommand("workbench.action.findInFiles");

However, it simply did the search but didn't return anything.

Is there an API to get the list of search results?

I'm trying to search a string in the code base. Ctrl + Shift + F should do it, but I want to get the list of files in search results.

I tried using the VS Code mand:

mands.executeCommand("workbench.action.findInFiles");

However, it simply did the search but didn't return anything.

Is there an API to get the list of search results?

Share Improve this question edited Apr 4, 2022 at 0:24 Viradex 3,7783 gold badges14 silver badges39 bronze badges asked Jul 18, 2019 at 4:59 yuanOrangeyuanOrange 911 silver badge3 bronze badges 1
  • You're asking for a feature that it wouldn't really make sense for VSCode to have. From a programmer's perspective, you need to find by keyword and symbol most often. If you don't, then like @theMayer said, try to use a terminal mand like find. – ifconfig Commented Jul 19, 2019 at 6:07
Add a ment  | 

4 Answers 4

Reset to default 3

The Copy All right-click mand in the Search Results pane will get you the entire search results, including the file names.

You can then use regex find/replace to delete the results, leaving only the file names.

Here is how I am getting the files from a search across files result. This is after the workbench.action.findInFiles mand has been run.

/**
 * Get the relative paths of the current search results 
 * for the next `runInSearchPanel` call  
 * 
 * @returns array of paths or undefined  
 */
exports.getSearchResultsFiles = async function () {

    await vscode.mands.executeCommand('search.action.copyAll');
    let results = await vscode.env.clipboard.readText();

    if (results)  {
        // regex to get just the file names
        results = results.replaceAll(/^\s*\d.*$\s?|^$\s/gm, "");
        let resultsArray = results.split(/[\r\n]{1,2}/);  // does this cover all OS's?

        let pathArray = resultsArray.filter(result => result !== "");
        pathArray = pathArray.map(path => this.getRelativeFilePath(path))

        return pathArray.join(", ");
    }
    else {
        // notifyMessage?
        return undefined;
    }
}

It is designed to get the relative paths of those files, you can change that if you want.

On your search tab, press the little triangle thing next to the search box. It will expand to offer a find/replace in files capability (assuming you’re on the latest version).

Steps:

  1. Select one file using ctrl+click
  2. ctrl+A to select all files
  3. mouse right click
  4. Copy All

本文标签: javascriptHow to get the list of files in search results in VS codeStack Overflow