简体   繁体   English

将终端选项卡保存到保存的工作区 VSCode

[英]Save terminal tabs to saved workspace VSCode

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.一个是我完成所有 git 工作的地方,另一个是我运行 gulp 任务的地方。 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.当我打开一个保存的工作区时,它总是只打开一个指向项目根目录的选项卡。

Look at the Restore Terminals extension.查看恢复终端扩展。 For example, in your settings.json:例如,在您的 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.将打开两个终端,一个用于您的git工作,一个用于gulp工作。 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.或者您可以在启动时运行它。

The Tasks feature is the current recommended way to handle this.任务功能是当前推荐的处理此问题的方法。 See Automating launching of terminals in the VS Code documentation.请参阅 VS Code 文档中的自动启动终端

For a Linux not-so-officially-recommended way that works.对于非官方推​​荐的 Linux 工作方式。 The xdotool key emulator for linux can be used.可以使用适用于 linux 的 xdotool 密钥模拟器。 I'm sure any Windows key emulator could be used to accomplish the same thing.我确信任何 Windows 密钥模拟器都可以用来完成同样的事情。

First, set the Terminal: Change Color to a keyboard shortcut.首先,将Terminal: Change Color设置为键盘快捷键。 Here I'm using Ctrl+Shift+C.这里我使用 Ctrl+Shift+C。 This is done by bringing up the command pallet (Cntrl+Shift+P) and, typing Terminal: Change Color这是通过调出命令托盘 (Cntrl+Shift+P) 并输入Terminal: Change Color

在此处输入图片说明

Here is my Restore Terminals json from VS Code settings.这是我从 VS Code 设置恢复的终端 json。 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.需要在最后一个选项卡上激活用于更新颜色的冗长 cli 命令,因为它是在“还原选项卡”完成还原选项卡时聚焦的选项卡。 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.我们需要在每个选项卡颜色的设置之间有一点超时,因为 VS Code 只需大约 10 秒即可完成命令。 The built in keyboard shortcut cntrl+Page_Up is used to bring the previous tabs into focus.内置的键盘快捷键 cntrl+Page_Up 用于使之前的选项卡成为焦点。 This script is for a WorkSpace that includes a Docker-Compose enviroment that contains 3 repos.此脚本适用于包含 Docker-Compose 环境的 WorkSpace,该环境包含 3 个存储库。 Be sure to save this in your Workspace settings, not your user settings.请务必将其保存在您的工作区设置中,而不是您的用户设置中。 For more info, visit: https://code.visualstudio.com/docs/getstarted/settings有关更多信息,请访问: 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.当然,任何曾经使用过键盘模拟来自动执行任务的人都应该知道,在任务完成之前,您不能输入文本或单击其他地方。

在此处输入图片说明

If you're on windows you can use powershell to create a similar effect to @CodeBloodedChris's solution.如果您使用的是 windows,则可以使用 powershell 来创建与@CodeBloodedChris 的解决方案类似的效果。

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.在您的工作区根目录(或最后一个终端的最终路径所在的位置)中创建一个 powershell 脚本,并将其命名为“restore-terminal-customization.ps1”或类似名称。 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.在此脚本中,'^+c' 是 ctrl+shift+c,'~' 是回车键。 Similarly I had also set up a keybinding for the icons which uses ctrl+shift+i '^+i'.同样,我还为使用 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.在您的 restoreTerminal 的配置中,调用您在列表中的最后一个终端中作为命令创建的脚本。

"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.不要忘记将键绑定设置为Terminal: Change ColorTerminal: Change Icon命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM