microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
94.96k stars 8.22k forks source link

Enable Terminal to persist / restore instance settings #766

Closed zadjii-msft closed 3 years ago

zadjii-msft commented 5 years ago

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

Thoughts:

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

This also includes restoring things like previous window positions.

glen-84 commented 5 years ago
  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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

The thing that makes me hate YAML is that the spec doesn’t support tabs.

I personally prefer HOCON over YAML and JSON.

The former because of tabs, and the latter because of comments.

JCKodel commented 5 years ago

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 commented 4 years ago

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

DHowett-MSFT commented 4 years ago

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. :smile:

getify commented 4 years ago

Still feeling this pain every time I re-open. I don't even mind it not remembering size, since I set that in my config. I just really hate it not remembering position, because on my system, every time it opens, the top-left corner is about 30px to the right/down from the last time it opened, so it shifts little by little until something resets it back to the top-left and the cycle repeats. That is endlessly annoying.

Would love even a minimal interim on window size being remembered quicker, even if this larger feature is still being worked out. Please!? :)

zadjii-msft commented 4 years ago

@getify You know you can use the initialPosition to set a launch position for the Terminal window, right?

getify commented 4 years ago

@zadjii-msft No, I was not aware of that. Maybe it was added after the last time I had looked at the docs.

Just tried it, and with some fiddling it seems fine for now as an interim. I use a left-side taskbar, so it was a little annoying that the "X" position isn't "available desktop coordinates" but "absolute screen position". I figured out with some trial and error that 93 was the right magic number for my current setup.

smykhailov commented 4 years ago

I found some workaround for this, which works for me and maybe will be helpful for others. I created cmd file with the following command: wt.exe -p PowerShell -d "D:\Projects" ; split-pane -p PowerShell -d "D:\Projects\foo" ; split-pane -H -p PowerShell -d "D:\Projects\bar" ; -p cmd -d "D:\Projects" ; -p Ubuntu ; focus-tab -t 0 which opens the tabs and panes I need to have opened.

also in settings.json I added the position and size:

  "initialPosition": "-1760, 20",
  "initialRows": 46,
  "initialCols": 170,

with x: -1760 I open terminal on my left monitor :)

mws-rmain commented 4 years ago

When restoring 'last position', please remember to handle which monitor (assuming more than one) the position refers to.

igorakkerman commented 4 years ago

Do not only remember the last monitor that was used, but also remember the last position used on other monitors.

When I have the Terminal open on my big monitor and disconnect my notebook, it will appear on my notebook's screen, of course. Now, it would be great if Terminal remembered the last position used when it was open on my big monitor's and my notebook's screens. Then, when I would re-connect my big monitor, the Terminal should appear on my big monitor, at the last position on that screen. If I disconnect my big monitor again, the Terminal should come back to the last position on my notebook's screen.

That is, the application should store the last monitor used and a table mapping each monitor to the position and dimensions the Terminal occupied on that monitor.

radutomy commented 4 years ago

Also remember position if opened on another virtual desktop.

clounie commented 3 years ago

Do not only remember the last monitor that was used, but also remember the last position used on other monitors.

When I have the Terminal open on my big monitor and disconnect my notebook, it will appear on my notebook screen, of course. Now, it would be great if Terminal remembered the last position used when it was open on my big monitor's and my notebook's screens. Then, when I would re-connect my big monitor, the Terminal should appear on my big monitor, at the last position on that screen. If I disconnect my big monitor again, the Terminal should come back to the last position on my notebook's screen.

That is, the application should store the last monitor used and a table mapping each monitor to the position and dimensions the Terminal occupied on that monitor.

^ This would be great. But would rather have basic functionality than wait extra time for macOS tricks.

@glen-84 Pretty much nailed behavior with his comment about "do as browsers do". It keeps things fairly simple, and those are patterns that, while fairly powerful, anyone who uses a computer is familiar with.

NeilMacMullen commented 3 years ago

As described in #8989, if the state can be persisted to file as suggested in https://github.com/microsoft/terminal/issues/766#issuecomment-494689538 then a desirable feature would be to be able to invoke a new terminal instance and supply that particular configuration as the startup state (via argument). That would help alleviate some of the ambiguity around what to do in the case of multiple open instances.

brantPTS commented 3 years ago

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

@zadjii-msft

You should have this restore just like a browser, i.e., either restore last window closed, or if they select "Exit" command, restore all windows. What would be really awesome would be to save command history in each tab so you can just up arrow to bring back recent commands. That will make this indispensable.

Thank you.

zadjii-msft commented 3 years ago

What would be really awesome would be to save command history in each tab so you can just up arrow to bring back recent commands.

FYI this has been discussed to death elsewhere in this repo - it's not the terminal's responsibility to remember what the last commands entered were. Most modern shells (e.g. pwsh, bash, zsh) will already do this. cmd can't do this, because cmd has never done that, and cmd is in maintenance-only mode.

The rest though - I'm on board with all that ☺️

Poopooracoocoo commented 3 years ago

hey @zadjii-msft (sorry if you're not back from your vacay) this issue doesn't sound like remembering the size and position of a Terminal window to many users, including myself. The discussion below it and the closed issues do make it look like it, but not everyone will have the time and patience to do that. Mind updating the issue to say something about it independent of there being an option to restore the last session? :)

zadjii-msft commented 3 years ago

Okay, sure. I'll update the OP. To clarify:

igorakkerman commented 3 years ago

@zadjii-msft thanks for clarifying. In your first item, you refer to restoring the window state across terminal launches. Should we have another issue for restoring the window state while it is running, but after an external monitor is disconnected and re-connected? (https://github.com/microsoft/terminal/issues/766#issuecomment-653671768)

zadjii-msft commented 3 years ago

That's totally something that should have it's own issue. FYI I don't think any other app does this currently - I'm constantly repositioning windows when I dock/undock my laptop.

That ask is now #9188

marcelo-mason commented 3 years ago

This issue is opened for two years. Unusable terminal for any poweruser for two years. How about just get something done, then figure out how to make it perfect later.

yodaldevoid commented 3 years ago

@marcelo-mason If you feel so strongly about it, as it is an open source project you are always free to submit a proposed spec for the feature. While I don't know for certain, I have the feeling that the obvious movement on this feature is not due to people not thinking it is important but because other features have taken precedence because people have decided that they are more important. This feature might see more movement if it had a champion to push it forward; maybe you fit that roll?

zadjii commented 3 years ago

That's absolutely true. This is a big project, with a lot of high priority features in development all the time. A lot of the groundwork for this particular feature is in progress over at #8771. There's just only so much bandwidth for designing so many features at a time, and the team is currently busy with the Settings UI (#1564), quake mode (#653), tab tear out (#1256), setting a default terminal application (#7489), so there's not really any other bandwidth at the moment. It's still high up on the priority list though, and if anyone would like to make progress faster, I'm sure the team would be happy to review a spec proposal or a PR ☺️

ghost commented 2 years ago

:tada:This issue was addressed in #11083, which has now been successfully released as Windows Terminal Preview v1.12.2922.0.:tada:

Handy links:

ghost commented 2 years ago

:tada:This issue was addressed in #10972, which has now been successfully released as Windows Terminal Preview v1.12.2922.0.:tada:

Handy links:

Poopooracoocoo commented 2 years ago

I got my party poppers out too early there. I thought this was fixed... Terminal STILL doesn't persist its window size and position!

5858 was closed in favour of this issue...

Tested on Terminal Preview v1.12.2922.0

And strangely it doesn't seem to use WinUI 2.7 with the new styles.

Something that's present in the release build is a strange window that is opened briefly before Terminal opens.

zadjii-msft commented 2 years ago

Terminal STILL doesn't persist its window size and position!

Did you enable the setting? image It won't work for elevated windows at the moment.

And strangely it doesn't seem to use WinUI 2.7 with the new styles.

That's by design, see #11231

Something that's present in the release build is a strange window that is opened briefly before Terminal opens.

File a new issue for that, let's not take this thread off topic.

alrz commented 2 years ago

Any reason to not set this by default?

Also, I think "confirmCloseAllTabs": false should be the default too considering we preserve the state.

Poopooracoocoo commented 2 years ago

Terminal STILL doesn't persist its window size and position!

Did you enable the setting? image It won't work for elevated windows at the moment.

That's not what I want. That's session restore, right? It looks like what should just be the normal behaviour was tied to that feature.

Rosefield commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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

zadjii-msft commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago
"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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

I only want the window position to be persistent just like every other app. I don't want to restore my previous session at all.

zadjii-msft commented 2 years ago

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 commented 1 year ago

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