Save terminals state in VS Code [duplicate] - visual-studio-code

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.

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.

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."

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

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.

Related

home directory is unpreferable location in msys2 shell in integrated teriminal of vscode

I want to use msys2 as a integrated terminal in vscode, so I added this configulation to my user setting.json in vscode.
"terminal.integrated.profiles.windows": {
"MSYS2 UCRT64": {
"path": [
"C:\\msys64\\usr\\bin\\bash.exe",
],
"args": [
"--login",
"-i"
],
"env": {
"MSYSTEM": "UCRT64",
"CHERE_INVOKING": "1",
"MSYS2_PATH_TYPE": "inherit"
}
},
},
"terminal.integrated.defaultProfile.windows": "MSYS2 UCRT64",
And when I open the vscode and toggle terminal window, the msys2 launch, but its start folder is not the same with the one when I launch it from installed location(c/msys64/ucrt64.exe).
shumbow#DESKTOP-STEUAV4 UCRT64 /c/Users/shumbow
$ pwd
/c/Users/shumbow
This is when I launch it from install location.
shumbow#DESKTOP-STEUAV4 UCRT64 ~
$ pwd
/home/shumbow
Could anyone teach me the way I can make these two same?
#HolyBlackCat Removing the CHERE_INVOKING env variable did the trick!(answer by HolyBlackCat). Thanks.

VSCode terminal task not using zsh profile

I'm trying to run a task on window load in VSCode where a terminal opens and nvm use && yarn dev is run by default. However, running this shell tasks seems to not load my zsh profile.
The output I get from running my task is:
The terminal process "zsh '-c', 'nvm use && yarn dev'" terminated with exit code: 127.
Terminal will be reused by tasks, press any key to close it.
But if I then manually start a new terminal and run the same command (ie: by pressing plus, opening a new integrated terminal), it will work as intended.
Suspecting that VSCode isn't loading my profile for some reason, I tried adding the following to my task, it resulted in the error /bin/zsh: can't open input file: nvm use && yarn dev The terminal process "zsh '-l', 'nvm use && yarn dev'" terminated with exit code: 127..
// in dev task
"options": {
"shell": {
"executable": "zsh",
"args": ["-l"]
}
},
.vscode/tasks.json
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Create terminals",
"dependsOn": [
"Dev",
],
// Mark as the default build task so cmd/ctrl+shift+b will create them
"group": {
"kind": "build",
"isDefault": true
},
// Try start the task on folder open
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Dev",
"type": "shell",
"command":
["nvm use && yarn dev"],
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "dev-group"
}
},
]
}
This worked for me-
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"args": ["-l", "-i"]
}
},
github.com/microsoft/vscode/issues/143061
try adding this to your settings.json
"terminal.integrated.profiles.osx": {
[...]
"zsh": {
"path": "/bin/zsh -l",
"args": [
"-l"
]
},
[...]
},
Note that the important part is
"path": "/bin/zsh -l",
I had the same problem and I found that for some reason VScode does not take into consideration the -l flag passed in args. So you can just include it with path.
If you do not have terminal.integrated.profiles.osx in your settings, you can copy it from the default settings (open the Command Palette and search for 'default settings').
I did not need to do this, but you can make sure that zsh is the default terminal profile for VScode by setting terminal.integrated.defaultProfile.osx to zsh
Try running echo $SHELL from VSCode's integrated terminal. If you're on a Mac or Linux machine, you can compare that output to the output from the terminal app (outside VSCode). It's possible your default shell in VSCode is set incorrectly or using a copy of zsh at another location. If so, set VSCode's default shell through the command palette (Terminal: Select Default Shell).
Also check out your shell's default profile (Terminal: Select Default Profile) from the command palette and make sure it's set to zsh -l... using the -c argument (non-login non-interactive) will prevent ~/.zshrc from being executed, which sounds like what's going on here given your error output.
Finally, confirm your profile is located correctly (at ~/.zshrc) and that both nvm and yarn PATHs are exported. Alternatively, if you're trying to reference yarn locally (if for some reason you only installed it locally), you'll need to run yarn via npx...
You may need to add an automation profile as well
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh -l",
"args": ["-l"]
}
},
"terminal.integrated.automationProfile.osx": {
"path": "/bin/zsh"
}
macOS 12.6.1 | vscode 1.74.0
I did not manage to do any if it since none of this worked, so I have just removed warning...
"terminal.integrated.showExitAlert": false
Or via GUI
I hope that will not get minus points here...

vscode allow only wsl terminal in a workspace

I have these settings in my workspace:
"terminal.integrated.defaultProfile.windows": "Ubuntu-20.04 (WSL)",
"terminal.integrated.profiles.windows": {
"Ubuntu-20.04 (WSL)": {
"path": [
"C:\\WINDOWS\\System32\\wsl.exe"
],
"args": [],
}
},
I want to either disable all others or set it wsl as default.
I still have all the rest in my list:
When I set wsl as default, its working only when I create a new terminal...
How can I remove all the others and stay only with wsl?
Thanks

visual studio code multiple commands attached to keyboard shortcut

I'm tried each of the following keybindings.json ctrl+enter "command" settings to enable execution of two commands when the .py[thon] script editor extension is active. The single commands work but none of the attempts to associate multiple commands or a macros reference containing multiple commands, work. The later generates a "command 'macros.<macro defined in settings.json>' not found."
Any pointers on how I get multiple commands attached to a single keyboard shortcut definition, specifically what's wrong with how i'm setting up macros approach?
// %appdata%\Code\User\keybindings.json
[
{
"key": "ctrl+enter",
//"command": "python.execSelectionInTerminal",
//"command": "cursorDown",
//"command": [ "python.execSelectionInTerminal", "cursorDown" ],
//"command": "python.execSelectionInTerminal, cursorDown",
//"command": "python.execSelectionInTerminal && cursorDown",
"command": "macros.pythonExecuteLineAndMoveToNextOne",
"when": "editorTextFocus && editorLangId == 'python'"
}
]
// %appdata%\Code\User\settings.json
{
"macros": {
"pythonExecuteLineAndMoveToNextOne": [
"python.execSelectionInTerminal",
"cursorDown"
]
},
.
. .
. . .
}
Adding duplicate of answer raised in comments so it can be accepted to change state of this question to answered.
The fix was to add the #name:macros #publisher:geddski vscode extension which was mentioned in the article i used to arrive at the multi-step macro definitions. It wasn't initially clear that it was a requirement.

missing items from Command palette in ST2

I'm trying to create a custom package and one thing I would like to do is bring up the Build Systems(located in the Tools menu item) in the command palette(show_overlay). So I have tried to create a Default.sublime-commands file in my package and type in...
[
{ "caption": "Build System", "command": "build_system" }
]
...to enable that menu item for the command palette(I also tried set_build_system) and then I created a Default.sublime-keymap file in my package so that I can access the Build System list from via shortcut...
[
{
"keys": ["f9"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Build System"}
}
]
I am not having any luck exposing the Build System menu item to the command palette. Can I get some help on this? I also noticed the Tools menu item is not available in the command palette as well. What am I missing?
Ok I figured it out. The command palette can only be populated by existing commands that run in sublime. The way you can view which commands that are being run in sublime is to open up the console(CTR + ~) and type in sublime.log_command(True)
Now whenever you do anything that makes sublime trigger a command, it will log that action in the console. Armed with this knowledge, we we go to Tools > Build System and click on the build system type we want, say, C++, we get:
command: set_build_system {"file": "Packages/C++/C++.sublime-build"}
Sweet! Knowing this we can go to our .sublime-commands file(you can call it Default.sublime-commands) and type in the below code:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
}
]
Tip: pay close attention to the "caption" it's what we will use to tie our .sublime-command file with our .sublime-keymap file. Let's add another one build system:
[
{
"caption": "Set Build System: C++", "command": "set_build_system",
"args": { "file":"Packages/C++/C++.sublime-build" }
},
{
"caption": "Set Build System: Python", "command": "set_build_system",
"args": { "file":"Packages/Python/Python.sublime-build" }
}
]
Now that we have exposed these two commands in our .sublime-commands file. We can create a shortcut for it in our .sublime-keymap file. I called mine Default.sublime-keymap:
[
{
"keys": ["f8"], "command": "show_overlay",
"args": {"overlay": "command_palette", "text": "Set Build System:"}
}
]
Notice the "text" key. Look familiar? This is how you connect your key binding to your command. Save press F8 and boom! You have our own custom command palette menu. Enjoy!
PS: you can put your .sublime-commands/.sublime-keymap files in your User package or add to any existing ones if you have them there if you just want to customize your sublime text 2 without making a custom package.