'VSCode: Store and reopen a group of file tabs so that I reset a specific environment in the future

VSCode remembers the file tabs that are open in my Workspace from the last environment, and so if I close VSCode and re-open, I have the same files opened.

Over a day or two of work, I may switch between 2 or 3 different feature branches while my PR's are going through review.

Each feature branch is usually on a totally different area of the code base, which means, I will want to open different groups of files when working on each branch.

I wonder if there is a way to save snapshots of open tabs so that I can re-reopen in future.

A really nice flow would be to have those files open automatically whilst other files close when VSCode detects a branch change.



Solution 1:[1]

  1. Get a "snapshot" of currently opened files.
  2. Save this snapshot somewhere; make it easy to change.
  3. Use the snapshot to open all of its files; close all other files first.
  4. Be able to make multiple snapshots and call each one easily.

(1) is harder than you might think. There are a number of vscode issues about searching only within the currently opened files and the problem remains largely unsolved after a few years for this reason.

Demo: get the relative paths of all opened files (unfortunately the gif creation software did a poor job of capturing all the keystrokes used in all these demos) :

get relative paths of opened files demo

Holy crap, what just happened. One keybinding and they are collected and formatted in a specific way.

A number of things happened. The only way I know of to efficiently get a list of opened files (maybe true even in an extension) is through the "Open Editors" view. So we

(a) focus that Open Editors view,

(b) select the entire list, and fortunately there is a

(c) copyRelativeFilePath command (or copyPath for the full path) that will get them all in a list.

But the list initially looks like:

1.html
simple\\gulpfile.js
test1.txt

which isn't going to do us much good. But it is now on the ClipBoard and there is an extension, Replace Rules that is able to run the Clipboard content through a series of regex's (without modifying the Clipboard content either) and paste the result somewhere. So you will need that extension and a macro extension, here using multi-command to run all the steps. Here is the macro which goes into your settings.json:

  "multiCommand.commands": [

   {
      "command": "multiCommand.getOpenedEditorsForTaskOpenAll",
      "interval": 50,

      "sequence": [
        "workbench.files.action.focusOpenEditorsView",
        "list.selectAll",
        "copyRelativeFilePath",
        // "copyFilePath",
        "workbench.action.focusActiveEditorGroup",
        {
          "command": "replacerules.pasteAndReplace",
          "args": {
              "ruleName": "Prepare file list for task open"
          }
        },
        "editor.action.formatSelection",
        "cancelSelection",
        "deleteLeft"
      ]
    }
  ]

Here is the replacerules rule that is used in the macro:

"replacerules.rules": {

   "Prepare file list for task open": {
    "find": ["(\\\\)", "^(.+)$"],
    "replace": ["\\\\", "\"'$1'\","]
  }
}

It just modifies that bare file list into something we can use in a task. Here is a keybinding to run that macro (keybindings.json):

{
  "key": "ctrl+shift+i",
  "command": "extension.multiCommand.execute",
  "args": {
    "command": "multiCommand.getOpenedEditorsForTaskOpenAll"
  },
}

You should be able to test this already to see if it'll dump the formatted file list to wherever your cursor is in the current text editor.


One way to open up all these files at once is to put them into a task (in tasks.json):

{
  "label": "Open snapshot 1",
  "command": "code",
  "args": [

  ],
  "type": "shell",
  "problemMatcher": [],
  "dependsOrder": "sequence",
  "dependsOn": [ "close all editors" ]
},

{
  "label": "close all editors",
  "command": "${command:workbench.action.closeAllEditors}",
  "type": "shell",
  "problemMatcher": []
},

You see the task Open snapshot 1 is dependent on the close all editors task so that happens first. In the Open snapshot 1 the command is code to open all the arg files. Put your cursor in the args array and that is where the properly formatted list of files to open will be written by the macro. Demo:

demo of inserting opened files into a task

If you want to update that file list you can just select them and rerun the macro. And you can now set up as many Open snapshot <n> tasks as you want (with whatever labels you want to give them).

Now, to trigger the task we will use a keybinding as well:

  {
    "key": "alt+s 1",
    "command": "workbench.action.tasks.runTask",
    "args": "Open snapshot 1"
  },
  {
    "key": "alt+s 2",
    "command": "workbench.action.tasks.runTask",
    "args": "Open snapshot 2"
  },

etc. As noted earlier, running this task will first run the dependent task which closes all currently opened files. If you just wanted to open a batch of files you frequently use without closing the others, just remove the "dependsOn": [ "close all editors" ] option.

Here is a final demo of the task closing the open files and opening the snapshot files (I just changed the file list above a little to make it look different).

demo of tasks opening and closing files


Two things to remember:

(1) the Editor > Open Editors: Visible setting must be enable with a number high enough to show all your opened files. The Open Editors can be hidden so you don't have to look at it all the time if you don't want, but it will be opened automatically by the macro - that can't be avoided. You can see it opening in the demos. It can be hidden by its context menu.

(2) The terminal is used, so you see it opening.


It seems like a lot of set-up but the operation is actually pretty simple - just a couple of keybindings to remember.

Solution 2:[2]

Try an extension called File Group.
My searching reveled that a lot of people are looking for this option but vsCode does not seem to have any good way to do it. This extension lets you list out the files with full path and associate them to a key shortcut.

Three files below will load by hitting ctrl-alt-1 if you add this to youProject.code-workspace:

"1": {
    "files": [
        "C:\\temp\\file1.txt",
        "C:\\svn\\foo.txt",
        "C:\inetpub\wwwroot\iisstart.htm"
        ]
    }

(It is a pain to set up if you are new to vsCode, hopefully it gets developed further.)

Solution 3:[3]

This worked for me. But it may have side affects when git does refresh https://marketplace.visualstudio.com/items?itemName=gkotas.restore-git-branch-tabs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivailo Manolov
Solution 2 Jake v1
Solution 3 dsazonov