Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Terminal to persist / restore instance settings #766

Closed
zadjii-msft opened this issue May 14, 2019 · 57 comments · Fixed by #10972 or #11083
Closed

Enable Terminal to persist / restore instance settings #766

zadjii-msft opened this issue May 14, 2019 · 57 comments · Fixed by #10972 or #11083
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-User Interface Issues pertaining to the user interface of the Console or Terminal Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.

Comments

@zadjii-msft
Copy link
Member

zadjii-msft commented May 14, 2019

Let the user set a global setting to have the terminal re-open with whatever profiles were last open.

Thoughts:

  • what happens when a user has two windows open, closes one with 3 tabs, then opens a new window? Should it open the 3 tabs? Or should it start a fresh, single tabbed window?
  • how do we persist the last session?
  • Do we only persist the last tabs open when the last terminal window is closed?
  • Do we add a "quit terminal" option that closes all open windows, and persists all their open profiles?

THIS IS NOT A DISCUSSION OF PRESERVING WINDOW CONTENT, ONLY OPEN WINDOW PROFILES

This also includes restoring things like previous window positions.

@zadjii-msft zadjii-msft added Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Area-User Interface Issues pertaining to the user interface of the Console or Terminal Area-Settings Issues related to settings and customizability, for console or terminal labels May 14, 2019
@zadjii-msft zadjii-msft added this to the Windows Terminal Backlog milestone May 14, 2019
@ghost ghost added the Needs-Tag-Fix Doesn't match tag requirements label May 17, 2019
@miniksa miniksa added Product-Terminal The new Windows Terminal. and removed Mass-Chaos labels May 17, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label May 17, 2019
@glen-84
Copy link

glen-84 commented May 21, 2019

  1. Will this also restore split pane configurations?
  2. Will this restore the current directory (CWD/PWD) of each tab (as opposed to just the configured starting directory)?

IMO:

what happens when a user has two windows open, closes one with 3 tabs, then opens a new window? Should it open the 3 tabs? Or should it start a fresh, single tabbed window?

Open a fresh window. If you later add support for window history, they could use a "Recently closed windows" menu to restore the window with the same set of tabs.

how do we persist the last session?
Do we only persist the last tabs open when the last terminal window is closed?

That would be the simplest solution probably, but it might not be ideal if the application is improperly shut down. An alternative would be to continuously track open windows, tabs, panes, and working directories as changes are made, and persist those changes immediately (or on a timer).

Do we add a "quit terminal" option that closes all open windows, and persists all their open profiles?

Firefox and Chrome have an "Exit" option, which closes all windows. When you start the application again, the windows are restored.

In general, I think that it would be great if Windows Terminal mimicked the behaviour of browsers.

@mdtauk
Copy link

mdtauk commented May 21, 2019

I know this is just about tabs and pane arrangements, but why not restore the buffer with all the previous output strings in place?

@glen-84
Copy link

glen-84 commented May 22, 2019

I was thinking, if the state is stored in a human-readable format, it could also be hand-edited by users, and generated/modified by an external script or programme.

A made-up example:

{
    "version": "0.2.0",
    "state": {
        "windows": [
            {
                "maximized": false,
                "positionX": 500,
                "positionY": 200,
                "width": 1000,
                "height": 2000,
                "tabs": [
                    {
                        "title": "Project X",
                        "panes": [
                            {
                                "profile": "EF3E32A7-5FF6-42B4-B6E2-96CD7D033F00",
                                "directory": "C:/...",
                                // position/dimensions
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

(As usual, I wish that people would move away from JSON and instead use something more suited to configuration files, like YAML.)


@mdtauk,

I know this is just about tabs and pane arrangements, but why not restore the buffer with all the previous output strings in place?

To limit the scope of the feature and get it released sooner. The format that I show above could easily be extended at a later stage.

@ExE-Boss

This comment has been minimized.

@JCKodel
Copy link

JCKodel commented Jun 22, 2019

what happens when a user has two windows open, closes one with 3 tabs, then opens a new window? Should it open the 3 tabs? Or should it start a fresh, single tabbed window?

Use the browser behavior. It works and it is familiar. Also, would be nice to detach/reatach tabs (as Chrome).

how do we persist the last session?

It would be nice to persist everything (including history) of each tab. It would be amazing to have up arrow history persisted as well. You can use all my %USERPROFILE% space for that =)

Do we only persist the last tabs open when the last terminal window is closed?

As in a browser, when a tab is closed, its state remains (so I can reopen closed tabs with Ctrl+Shift+T). This would be awesome. Perhaps some settings to limit the number of persisted sessions?

Do we add a "quit terminal" option that closes all open windows, and persists all their open profiles?

As in a browser, [X] closes all tabs (I can reopen them with Ctrl+Shift+T - it will restore entire windows if needed).

@bitcrazed
Copy link
Contributor

Updated title to better describe the ask, which also came-up in this Twitter thread: https://twitter.com/stanzillaz/status/1176675911275556866

@DHowett-MSFT DHowett-MSFT changed the title Enable Terminal to persist / restore sessions Enable Terminal to persist / restore instance settings Sep 26, 2019
@DHowett-MSFT
Copy link
Contributor

Renaming it again because it really seems to pertain to saving info about specific instances of specific profiles. We can quibble about session/profile/instance later. 😄

@Rosefield
Copy link
Contributor

I apologize if the naming is confusing. The issue, I think, is that a "session" is not well defined yet anywhere, and the wording was based on other applications (such as Edge) to be forward looking for when things like #961 are also implemented.

The exact behavior of this feature when the setting is enabled is, from the docs (https://docs.microsoft.com/en-us/windows/terminal/customize-settings/startup#behavior-when-starting-a-new-terminal-session-preview)

Note: [when enabled] Currently, Windows Terminal will save the following information:
- Each window's position, size, and name
- The tab layout of each window, including the layout and profile of each pane, but not any contents of those panes

@Poopooracoocoo
Copy link

Poopooracoocoo commented Oct 20, 2021

Oh I see. That should be on by default to match every other Windows app then. Why is it an option like alrz said? :/

The actual restoring of content is completely separate to me.

@Rosefield
Copy link
Contributor

Currently the feature is being locked to the preview build while it is tested for stability (since there were a bunch of changes under the hood). While I cannot speak for them, I am sure the team will reevaluate whether it is a default or not depending on how it does.

@zadjii-msft
Copy link
Member Author

Yep, basically that. It's not on by default because we wanted to see how it landed. If it's stable enough, then it very well may end up as the default behavior in 1.12 stable. That being said, people FUCKING HATE defaults being changed on them, so no promises.

@Poopooracoocoo
Copy link

That being said, people FUCKING HATE defaults being changed on them, so no promises.

[Insert XKCD]

but i honestly expected that behaviour to not be behind both a preview release and an opt-in.
the very question of whether it becomes default or not makes me sad. it just makes sense to match standard window conventions :/
like i've been waiting for like two years here :'( but hopefully it'll be there in 1.12!!! ^.^

@Rosefield
Copy link
Contributor

Rosefield commented Oct 20, 2021

Since you're already using 1.12 preview you can use this today. Open your settings (press ctr+,) and then make sure the settings is enabled. Once it is enabled windows will be saved when you use the quit action, or by pressing the X icon on the window.

image

You can also enable the feature by adding
"firstWindowPreference": "persistedWindowLayout"
to your settings.json

e.g.
image

@igorakkerman
Copy link

igorakkerman commented Oct 21, 2021

This works for me when launching Terminal regularly. However, when I launch it as an administrator, neither does it open the last session I had as a regular user, nor does it restore the last administrator session or even the window's position and size.

@zadjii-msft
Copy link
Member Author

@igorakkerman That's unfortunately by design for now. I've got a fix that's almost ready in #11222, but that didn't make the cut for 1.12

@joel-daros
Copy link

It need at least remember the current folder of each tab.

@zadjii-msft
Copy link
Member Author

@MrSparklle It'll do that, so long as you configure your shell to tell the Terminal about the current working directory, with OSC9;9 (#8166, etc.)

@rf-0
Copy link

rf-0 commented Oct 29, 2021

@Rosefield using preview 1.12.2931.0 it doesn't seem that the state is properly saved. Tabs names and directories are lost. The only thing it is doing is opening the same number of tabs i had previously open at the default directory of C:\Users*

@xsnowemployee
Copy link

"state": {
        "windows": [
            {
                "maximized": false,
                "positionX": 500,
                "positionY": 200,
                "width": 1000,
                "height": 2000,
                "tabs": [
                    {
                        "title": "Project X",
                        "panes": [
                            {
                                "profile": "EF3E32A7-5FF6-42B4-B6E2-96CD7D033F00",
                                "directory": "C:/...",
                                // position/dimensions
                            }
                        ]
                    }
                ]
            }
        ]
    }

Even this one not working for me, I tried and reopen terminal but is not working.

@zadjii-msft
Copy link
Member Author

@RehanHashmi did you write that json by hand? That doesn't look like the actual schema we ended up going with for the state file.

Make sure to enable "firstWindowPreference": "persistedWindowLayout" in the settings, and the Terminal will auto-generate state that looks more like:
image

in state.json.

Tabs names ... are lost

That's a known bug, #11878

directories are lost

You'll need to make sure to configure your shell to emit the CWD to the Terminal for us to be able to restore those

@xsnowemployee
Copy link

xsnowemployee commented Dec 15, 2021

@zadjii-msft Here is my settings.json file from windows shell, My requirement is like each time mostly I need to move to some directory and run few commands. Each time I open terminal I make few tabs and move each tab in different directory to run those commands, I want it like when I open windows shell, It automatically open 4 tabs in different file location, Please guide me what Should I do, I tried to use "state" but I guess I applied wrongly or did something wrong. it is not working,

{
  "$schema": "https://aka.ms/terminal-profiles-schema",
  "actions": [
    {
      "command": "unbound",
      "keys": "ctrl+shift+t"
    },
    {
      "command": "unbound",
      "keys": "ctrl+shift+w"
    },
    {
      "command": "unbound",
      "keys": "alt+f4"
    },
    {
      "command": {
        "action": "copy",
        "singleLine": false
      },
      "keys": "ctrl+c"
    },
    {
      "command": {
        "action": "newTab"
      },
      "keys": "ctrl+t"
    },
    {
      "command": "paste",
      "keys": "ctrl+v"
    },
    {
      "command": "closeWindow"
    },
    {
      "command": "find",
      "keys": "ctrl+shift+f"
    },
    {
      "command": {
        "action": "splitPane",
        "split": "auto",
        "splitMode": "duplicate"
      },
      "keys": "alt+shift+d"
    },
    {
      "command": "closePane",
      "keys": "ctrl+w"
    }
  ],
  "copyFormatting": "none",
  "copyOnSelect": false,
  "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
  "launchMode": "default",
  "profiles": {
    "defaults": {
      "backgroundImage": null,
      "backgroundImageStretchMode": "uniform",
      "colorScheme": "Solarized Dark",
      "cursorShape": "vintage",
      "font": {
        "face": "Cascadia Code",
        "size": 10,
        "weight": "bold"
      },
      "icon": "C:\\Users\\Azam\\Pictures\\Camera Roll\\extra\\0fMGlH3.jpg",
      "padding": "9",
      "tabTitle": "Welcome Rehan"
    },
    "list": [
      {
        "commandline": "powershell.exe",
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "hidden": false,
        "name": "Windows PowerShell",
        "colorScheme": "Campbell",
        "cursorColor": "#C19C00",
        "cursorShape": "filledBox",
        "fontSize": 9,
        "padding": "5, 5, 5, 5"
      },
      {
        "commandline": "cmd.exe",
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "hidden": false,
        "name": "Command Prompt"
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "hidden": false,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      },
      {
        "guid": "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}",
        "hidden": false,
        "name": "Git Bash",
        "source": "Git"
      }
    ]
  },
  "schemes": [
    {
      "background": "#0C0C0C",
      "black": "#000000",
      "blue": "#0037DA",
      "brightBlack": "#767676",
      "brightBlue": "#3B78FF",
      "brightCyan": "#61D6D6",
      "brightGreen": "#16C60C",
      "brightPurple": "#B4009E",
      "brightRed": "#E74856",
      "brightWhite": "#F2F2F2",
      "brightYellow": "#F9F1A5",
      "cursorColor": "#FFFFFF",
      "cyan": "#3A96DD",
      "foreground": "#CCCCCC",
      "green": "#13A10E",
      "name": "Campbell",
      "purple": "#881798",
      "red": "#C50F1F",
      "selectionBackground": "#FFFFFF",
      "white": "#CCCCCC",
      "yellow": "#C19C00"
    }
  ],
  "theme": "system",
  "state": {
    "windows": [
      {
        "maximized": false,
        "positionX": 500,
        "positionY": 200,
        "width": 1000,
        "height": 2000,
        "tabs": [
          {
            "title": "Project X",
            "panes": [
              {
                "profile": "61c54bbd-c2c6-5271-96e7-009a87ff44bf",
                "directory": "C:/Data/Tools/"
              }
            ]
          }
        ]
      }
    ]
  }
}

@zadjii-msft
Copy link
Member Author

state is not a member in settings.json. When I'm talking about state.json, I'm talking about a totally different file that the Terminal uses to persist instance data. Setting the global setting "firstWindowPreference": "persistedWindowLayout" in 1.12 will cause the Terminal to automatically populate your current window state into that file, and re-use it on startup.

Otherwise, what I used to do before that feature landed, was add an action like the following: (copypasta from a while back)

{ "command": { "action": "wt", "commandline": "new-tab --title OpenConsole cmd.exe /k #work 15 ; split-pane -s .30 --title OpenConsole cmd.exe /k #work 15 ; split-pane  -s .25 -H cmd.exe /k media ; new-tab --title \"Symbols Script\" powershell dev\\symbols.ps1 ; new-tab -p \"Ubuntu 18.04\" ; new-tab -p \"microsoft/Terminal\" ; sp -V -p \"microsoft/Terminal\" ; sp -H -p \"microsoft/Terminal\" ; focus-tab -t 0" }, "name": "Good Morning" },

(you could probably make it shorter by replacing new-tab/split-pane with nt/sp, respectively). (EDIT: You could also probably make this clearer now with the multipleActions action)

I add that to the keybindings/actions. This creates a new command in the Command Palette named "Good Morning". That opens up a few tabs & panes, running various build environments. I like having it in a command rather than startupActions, because I only really want this in one terminal window, not every single one I launch. It's personal taste.

You could repeat this for multiple different "session"s if you wanted. That way you could have layouts pre-defined for various different dev environments.

@theperiscope
Copy link

theperiscope commented Aug 24, 2022

I'm using Terminal 1.14.2281.0 and would really like the window position to stay persistent but it doesn't.

I have a Open windows from a previous session selected. With that mode, closing my last window with ALT+F4, X, or CTRL-SHIFT-W and then restarting Terminal works as expected and on restart position is restored. However, if you type exit (Command Prompt) to close last shell, position does not get restored.

@Poopooracoocoo

This comment was marked as off-topic.

@zadjii-msft
Copy link
Member Author

Necro'ing an old thread is not typically the best place for bug reports 😉 However, we definitely do intentionally treat exit different than closing the window. If the connection itself initiated the close, then we treat that as a signal that the connection is all done, and doesn't need to come back. Closing from the UI side though does persist the terminal state. We discussed this more in one of #9800, #10972 or #11083, but I can't find the specific comment chain at the moment.


@Poopooracoocoo your issue is already tracked in #12633, no need to pollute discussion here.

@simontom
Copy link

Well, without preserving the last opened path in each tab it seems pretty useless.
I'm on v1.15.2874
After a few years, this is still no working

@Kolobamanacas
Copy link

I apologize if the naming is confusing. The issue, I think, is that a "session" is not well defined yet anywhere, and the wording was based on other applications (such as Edge) to be forward looking for when things like #961 are also implemented.

The exact behavior of this feature when the setting is enabled is, from the docs (https://docs.microsoft.com/en-us/windows/terminal/customize-settings/startup#behavior-when-starting-a-new-terminal-session-preview)

Note: [when enabled] Currently, Windows Terminal will save the following information:
- Each window's position, size, and name
- The tab layout of each window, including the layout and profile of each pane, but not any contents of those panes

@Rosefield

Sorry for disturbing you in such a manner, but it's been a year and it seems like you have some idea of what's going on with the roadmap. Could you please shed some light on the implementing saving session feature plans?

By saving session I mean the Terminal's ability when launched to reopen tabs with their names, positions and (missed now) the opened paths. Currently I have lets say three projects. I open three PowerShell tabs for these projects and navigate to corresponding path for each of them. But when I reopen Terminal, each tab's path is reset to a user's home directory, which seems like not expecting behavior.

@zadjii-msft
Copy link
Member Author

Well, without preserving the last opened path in each tab it seems pretty useless. I'm on v1.15.2874 After a few years, this is still no working

You know, that's actually in the docs: Tutorial: Opening a tab or pane in the same directory in Windows Terminal. You need to set up your shell for it to cooperate with the Terminal - that doesn't work magically on Windows, unfortunately.


@Kolobamanacas This feature was implemented like, a year ago. Did you enable the following setting:
image

Or do you want a more generic "I want to save this set of tabs as a session, but not necessarily reopen the Terminal with that session every time"? In which case, you might be interested in #11526

@Kolobamanacas
Copy link

@Kolobamanacas This feature was implemented like, a year ago.

zadjii-msft, thanks a lot for your reply. The funny thing is that while answering simontom's question, you have accidentally answered mine.

I personally think that most users agree, that "restore session" feature means that when one launches the application, one continues exactly where one left. Who could possibly know (RTM), that some additional settings need to be set up in order to reach such a behavior. Here is an example:

  • Let's say I have brand new Windows with the latest stable version of the Terminal and PowerShell installed.
  • I open Terminal, go to Settings and set Open windows from a previous session in When Terminal starts menu.
  • I open two PowerShell tabs and rename the first one to "Desktop" and the second one to "Downloads".
  • I navigate to ~\Desktop in the first tab (cd ~\Desktop) and in the second tab I navigate to ~\Downloads (cd ~\Downloads).
  • I close Terminal.
  • I open Terminal.

My expectations:

  1. Terminal has two tabs (works).
  2. The first tab is called "Desktop" and the second one is called "Downloads" (works).
  3. Commands history is separated and presented for both tabs (works).
  4. Finally, my current location for the first tab is ~\Desktop and for the second tab is ~\Downloads (does not work).

Current behavior is all tabs are opened in the user's home directory.

Thanks to your post and the link you've provided I'm now aware that I need to set up my shell for it to cooperate with the Terminal by appending the following line to the prompt (at least for PowerShell profiles) for it to work as I expect.

$([char]27)]9;9;`"$executionContext.SessionState.Path.CurrentLocation`"$([char]27)\

So my problem is solved. The only thing I wish to mention is that if ordinary user doesn't spend some time on intense research he would be completely unaware of the existence of such a setting. So maybe some kind of hint within the GUI could help, stating for a shell need to be set up accordingly in order to Open windows from a previous session feature to work as intended.

@zadjii-msft
Copy link
Member Author

ryan-point

Okay, this isn't great, but I'm gonna put a link to the docs in the settings UI, right by that setting. That'll at least possibly clue someone in that there's more config needed.

@simontom
Copy link

Well, yes :)
Restore session means restore session. Does not work as the name says. Or we all has just got used to all the stuff been saved when restarting apps ;-)

@ngocbae98
Copy link

Well, without preserving the last opened path in each tab it seems pretty useless. I'm on v1.15.2874 After a few years, this is still no working

You know, that's actually in the docs: Tutorial: Opening a tab or pane in the same directory in Windows Terminal. You need to set up your shell for it to cooperate with the Terminal - that doesn't work magically on Windows, unfortunately.

@Kolobamanacas This feature was implemented like, a year ago. Did you enable the following setting: image

Or do you want a more generic "I want to save this set of tabs as a session, but not necessarily reopen the Terminal with that session every time"? In which case, you might be interested in #11526

I tried setting it like that, but it only restores the previous number of sessions (number of tabs). But the command content of each tab is completely gone.

@lhecker
Copy link
Member

lhecker commented Jul 29, 2024

@ngocbae98 This is the wrong issue. What you want is #961.
Content restoration is available in v1.21.1272.0 and later which is currently in Windows Terminal Preview. You can find the preview version in the app store or in our release page: https://github.com/microsoft/terminal/releases

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-User Interface Issues pertaining to the user interface of the Console or Terminal Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet