Search code examples
visual-studio-codevscode-extensionsvscode-tasks

Create a shortcut to open multiple files and keep them open in Visual Studio Code


I have a task that I do several times a day everyday that requires editing 20 files (spread out in different places throughout my project), keeping them open since editing each file mostly requires referring back to 1 or several of the other files multiple times.

I want to create a shortcut that opens these 20 files at once so that I can edit them, without navigating to the location of each file, as this becomes slightly tedious due to how often I have to do this task.

However, I frequently use the preview mode when coding & so don't want to disable preview mode altogether via setting "workbench.editor.enablePreview": false in my settings file.

I also don't want to relocate the files into 1 place, as this would break the general organisation of the project - unless there's a way to create an alias/shortcut of a file in a different location, that can open the file while not moving the actual file from its original location.

I used the top answer from this question to create the below macro. However, this macro only opens each file in preview mode - every time the next file is opened, the previous file is closed, & so the macro essentially only opens the last file in preview mode.

const vscode = require('vscode');

module.exports.macroCommands = {
   OpenFrequentFiles: {
      no: 1,
      func: () => openFrequentFiles()
   },
};

function openFrequentFiles() {

    const workspaceDir = ""/full/path/to/my/workspace"
    const frequentFilepaths = [
        "path/to/file1",
        "path/to/file2",
        ...
        "path/to/file20",
    ]
    for (let i = 0; i < frequentFilepaths.length; i++) {
        fullFilepath = workspaceDir + '/' + frequentFilepaths[i];
        openFile(fullFilepath);
    }
}

function openFile(filename) {
  const document = vscode.workspace.openTextDocument(filename);
  const editor = vscode.window.showTextDocument(document);
}

Is there are way to open the files & keep them open, instead of only opening each one briefly in period mode? I've looked through https://code.visualstudio.com/api/references/vscode-api & tried to write something along the lines of workbench.action.keepEditor, but I haven't managed to make it work.

Or any other suggestions would be great - it would just save me time & tedium in the long term if I could automate the opening of these files without navigating to each one.


Solution

  • Answer provided by @rioV8 in comments - File Group extension allows creating groups of files that can be opened (& kept opened) simultaneously.