Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In VS Code, Store a group of file tabs so that I can restore a specific environment in the future

VS Code remembers the file tabs that are open in my Workspace from the last environment, and so if I close VS Code 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 VS Code detects a branch change.

like image 439
David Cruwys Avatar asked Nov 22 '25 13:11

David Cruwys


1 Answers

UPDATE: As of April 2024 (v1.89) release, VS Code now support saving/restoring editors on branch switch, so if that is all you need, give it a go: https://code.visualstudio.com/updates/v1_89#_source-control


NOTE: The layout restoration is only supported on Insiders as vscode.getEditorLayout was introduced recently and should go to Stable on v1.77.0

What do you need to create an extension for it:

There are a few pieces you will need:

  • vscode.window.tabGroups.onDidChangeTabs to listen for tab changes so you are able to store the opened tabs.
  • vscode.commands.executeCommand to run built-in commands (vscode.getEditorLayout and vscode.setEditorLayout) so you can store and restore the tabs position (I did this based on viewColumn that can be found within the tab.group).
  • This one is a bit hacky but I've not found a way to open tabs without opening many text documents. For this you will need to use vscode.workspace.openTextDocument to create the document and vscode.workspace.showTextDocument so it appears as a tab on vscode.

The magic with this is once you are able to restore the editor layout, and you set viewColumn on vscode.workspace.showTextDocument the layout configuration should be restored as expected.

Couldn't generate a link pointing to each item, but overall details can be found here in VS Code API doc or in my extension source code:

  • vscode.window.tabGroups.onDidChangeTabs
  • openTextDocument and showTextDocument
  • In the same file if you look for executeCommand you will find the get and set layout usage.

If you don't want to create one, I've started working on something like this in 2019 for this same reason, hopefully this might also be useful for those that have this problem:

This is the extension: https://marketplace.visualstudio.com/items?itemName=marlom.keep-context

Here is a screen share of how it works:

enter image description here

It manages to store the layout position (Only on Insiders for now as pre-release, should be good to be released once vscode 1.77.0 comes out), files opened and git branch used.

like image 84
Marlom Avatar answered Nov 25 '25 10:11

Marlom