40

Is it possible to save Terminal settings to a workspace? For most of my projects I always have two terminal tabs open. One where I do all of my git work and one where I run gulp tasks. These are two different folders and neither are the project root. When I open a saved workspace it always just opens one tab to the project root.

PattyOK
  • 821
  • 1
  • 8
  • 9
  • Won't meet all of your requirements, but be sure to check out the `terminal.integrated.cwd` setting. Use it to create a custom "current working directory" and add it to your workspace settings. – jabacchetta Jul 12 '18 at 20:58

5 Answers5

21

Look at the Restore Terminals extension. For example, in your settings.json:

 "restoreTerminals.runOnStartup": false,   // true is the default
          // set to false if using a keybinding or command palette

  "restoreTerminals.terminals": [
    {
      "splitTerminals": [
        {
          "name": "git",
          "commands": [
            "cd <your directory>",
            "npm run test"         // your git command(s)
          ]
        }
      ]
    },
    {
      "splitTerminals": [
        {
          "name": "gulp",
          "commands": [
            "cd zip",
            "gulp sass"
          ]
        }
      ]
    }
  ]

will open two terminals, one for your git work and one for the gulp work. They can each take multiple commands.

Example keybinding:

{
  "key": "shift+alt+t",    // whatever keybinding if you wish
  "command": "restore-terminals.restoreTerminals",
},

or you can it run at start-up.

Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78
Mark
  • 143,421
  • 24
  • 428
  • 436
10

The Tasks feature is the current recommended way to handle this. There is no need for an extension. See Automating launching of terminals in the VS Code documentation.

Then make sure that tasks are run automatically when the folder is opened by running command Tasks: Manage Automatic Tasks in Folder and choosing Allow Automatic Tasks in Folder (see Run behavior).

Please also see the note here that:

Task support is only available when working on a workspace folder. It is not available when editing single files."

kkaja123
  • 135
  • 1
  • 7
Welt Fahr
  • 492
  • 5
  • 8
  • 3
    this does not work when i close vs code and start it over – Eliav Louski Nov 07 '21 at 11:43
  • Updated the answer with information on how to allow automatic tasks in folder. Hope this helps. – Welt Fahr Nov 25 '21 at 09:21
  • 1
    @WeltFahr Do you know how to prevent VSCode from closing these terminals and leaving them to work in? – virtual98 Jan 26 '22 at 07:26
  • @virtual98 The terminals remain open as long as you keep VSCode open. Are you asking whether they can be left open when VSCode is closed? Just to make sure I understand you question. The latter would not be possible. – Welt Fahr Jan 27 '22 at 08:31
  • I've been looking at this for 15 minutes and still don't know what to do. I think that's the need for an extension. – xr280xr Jul 18 '23 at 14:51
  • @xr280xr: you have to follow the linked documentation, that is creating a `.vscode/tasks.json` file with your configuration and running the **Tasks: Manage Automatic Tasks in Folder** command as stated above. Where are you stuck in this process? – Welt Fahr Jul 19 '23 at 16:07
5

I use this User setting:

Restoring terminal session in VSCode

It has the nice benefit of working across all directories/projects and of not requiring to save anything to any particular project.

WhyNotTryCalmer
  • 357
  • 5
  • 15
  • thanks! it remembers my terminals layout, even though it didn't remember the working directory of each of the terminals, any way to persist the working directory ? – Lydon Ch Jun 11 '23 at 09:31
2

For a Linux not-so-officially-recommended way that works. The xdotool key emulator for linux can be used. I'm sure any Windows key emulator could be used to accomplish the same thing.

First, set the Terminal: Change Color to a keyboard shortcut. Here I'm using Ctrl+Shift+C. This is done by bringing up the command pallet (Cntrl+Shift+P) and, typing Terminal: Change Color

enter image description here

Here is my Restore Terminals json from VS Code settings. The lengthy cli command to update the colors needs to be activated on the last tab, as it is the tab that is focused when Restore Tabs is finished restoring tabs. We need a bit of a timeout between the setting of each tab color, as VS Code takes just about a 10th of a second to complete the command. The built in keyboard shortcut cntrl+Page_Up is used to bring the previous tabs into focus. This script is for a WorkSpace that includes a Docker-Compose enviroment that contains 3 repos. Be sure to save this in your Workspace settings, not your user settings. For more info, visit: https://code.visualstudio.com/docs/getstarted/settings

      "restoreTerminals.terminals": [
    {
      "splitTerminals": [
        {
          "name": "docker-compose",
          "commands": ["cd ..", "cd docker-compose", "clear"]
        },
      ]
    },
    {
      "splitTerminals": [
        {
          "name": "api",
          "commands": ["cd ..", "cd api", "clear"]
        },
      ]
    },
    {
      "splitTerminals": [
        {
          "name": "ui",
          "commands": [
                "cd ..",
                "cd ui",
                "xdotool key ctrl+shift+c && xdotool type 'cyan' && xdotool key Return && sleep .2",
                "xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'green' && xdotool key Return && sleep .2",
                "xdotool key ctrl+Page_Up && xdotool key ctrl+shift+c && xdotool type 'yellow' && xdotool key Return",
                "clear"
            ]
        },
      ]
    }
  ],

And the end result... Is that all tab colors are updated after they are restored. Of course, anyone who has ever used keyboard emulation for automating tasks should know you can not be entering text or clicking elsewhere until the task is complete.

enter image description here

0

If you're on windows you can use powershell to create a similar effect to @CodeBloodedChris's solution.

Create a powershell script in your workspace root directory (or where ever the last terminal's final path is) and name it something like 'restore-terminal-customization.ps1' or something like that. Then add the following code to it:

# Uses windows forms to send keystrokes to customize vscode Terminals
Add-Type -AssemblyName System.Windows.Forms
$tabDelay = .6

# Last Terminal 
[System.Windows.Forms.SendKeys]::SendWait("^+cRed~")

# Second to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cYellow~")

# Third to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cMagenta~")

In this script the '^+c' is ctrl+shift+c and '~' is the enter key. Similarly I had also set up a keybinding for the icons which uses ctrl+shift+i '^+i'. Here's an example of setting both the color and the icon of a terminal:

# Fourth to last Terminal
[System.Windows.Forms.SendKeys]::SendWait("^{PGUP}"); Start-Sleep -s $tabDelay
[System.Windows.Forms.SendKeys]::SendWait("^+cGreen~^+iorganization~")

In your restoreTerminal's config call the script you created as a command in the last terminal on your list.

"restoreTerminals.terminals": [
    {
      "splitTerminals": [
        {
          "name": "docker-compose",
          "commands": ["cd ..", "cd docker-compose", "clear"]
        },
      ]
    },
    {
      "splitTerminals": [
        {
          "name": "api",
          "commands": ["cd ..", "cd api", "clear"]
        },
      ]
    },
    {
      "splitTerminals": [
        {
          "name": "ui",
          "commands": [
                "cd ..",
                "cd ui",
                ".\restore-terminal-customization.ps1",
                "clear"
            ]
        },
      ]
    }
  ],

Don't forget to set the keybindings to the Terminal: Change Color and Terminal: Change Icon commands in VsCode.