yuanOrange
yuanOrange

Reputation: 91

How to get the list of files in search results in VS code?

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 command:

commands.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?

Upvotes: 9

Views: 6017

Answers (4)

Ehsan
Ehsan

Reputation: 163

Steps:

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

enter image description here

Upvotes: 0

Mark
Mark

Reputation: 181100

Here is how I am getting the files from a search across files result. This is after the workbench.action.findInFiles command 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.commands.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.

Upvotes: 3

Foo Bar
Foo Bar

Reputation: 338

The Copy All right-click command 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.

Upvotes: 3

theMayer
theMayer

Reputation: 16167

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).

Upvotes: 0

Related Questions