VSCode open terminal with directory does not appear to work - visual-studio-code

Having found the VSCode command workbench.action.terminal.newWithCwd
{
"key": "cmd+shift+alt+h",
"command": "workbench.action.terminal.newWithCwd",
"args": {
"cwd": "${fileDirname}"
}
}
I cannot get it to work.
I have inserted the JSON above into the ~\Code\User\keybindings.json file, but how do I actually get it to execute?

This works as a keybinding, using the macro extension multi-command:
{
"key": "alt+k", // whatever you want here
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "workbench.action.terminal.newWithProfile",
"args": {
"profileName": "Git Bash",
"shouldForwardArgs": true,
}
}
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "cd '${fileDirname}'\u000D"
}
}
]
},
"when": "editorTextFocus"
},
What I think should work is this profile in settings.json:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash",
"path": "C:\\Program Files\\Git\\git-bash.exe"
},
"Git Bash at fileDirname": {
"source": "Git Bash",
"path": "C:/Program Files/Git/git-bash.exe",
"args": [
"cd ${fileDirname}" // variables are supported here according to the docs
]
}
},
and then this associated keybinding:
{
"key": "alt+k",
"command": "workbench.action.terminal.newWithProfile",
"args": {
"profileName": "Git Bash at fileDirname",
"shouldForwardArgs": true
}
},
But it doesn't quite work for me. It almost works, but it says no such file exists. I see a couple of issues filed on variable resolution in profiles.

Related

VSCODE running conda activate script upon opening cmd terminal

Whenever I open a new cmd or powershell instegrated terminal the terminal runs the following:
D:\..\..\Programming> D:/miniconda3/Scripts/activate
(base) D:\Archive\Bachelor\Programming>conda activate optim
with output:
(optim) D:\Archive\Bachelor\Programming>
I have been searching for the setting to do this for days and can not turn it of.
There seem to be no setting specifing this behavior nor I ever added a configuration to do so.
Can somebody explain where this is coming from?
User Settings
{
"workbench.colorTheme": "Monokai Pro",
"[perl]": {
"editor.defaultFormatter": "cfgweb.vscode-perl"
},
"window.restoreWindows": "none",
"C_Cpp.autocompleteAddParentheses": true,
"editor.accessibilitySupport": "off",
"workbench.iconTheme": "Monokai Pro Icons",
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"grunt.autoDetect": "off",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"cmd": {
"path": "C:\\WINDOWS\\System32\\cmd.exe",
"args": []
}
},
}
.vscode\settings.json
{
"python.analysis.extraPaths": [
"./Bachelor_Thesis_HOTRG"
]
}
.code-workspace
{
"folders": [
{
"path": "."
},
{
"path": "../writing"
}
],
"settings": {
"terminal.integrated.defaultProfile.windows": "Command Prompt",
}
}

How to Debugger in VSCode?

I added new extension in VSCode. I've added syntax and snippets. It's time for debugging. I searched a lot and couldn't get exactly what I wanted. I want to debug without using tasks.json.
My json files:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run",
"request": "attach",
"windows": {
"preLaunchTask": "windows"
},
"linux": {
"preLaunchTask": "linux"
},
"osx": {
"preLaunchTask": "osx"
}
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "windows",
"command": "echo",
"args": ["hello windows"],
"type": "shell"
},
{
"label": "linux",
"command": "echo",
"args": ["hello linux"],
"type": "shell"
},
{
"label": "osx",
"command": "echo",
"args": ["hello macOS"],
"type": "shell"
}
]
}
Thanks for all your answers.

How to pass input variable from launch.json to tasks.json in vscode

I can use input variables from launch.json in launch.json.
"configurations": [
{
...
"args": [${input:file_no}]
"preLanuchTask": "runPreTasks"
...
}
],
"inputs": [
{
"id": "file_no",
"type": "promptString"
}
]
Now, I want to get access to the same variable without entering input a second time in tasks.json.
{
"version": "2.0.0",
"tasks":[
{
"label": "runPreTasks",
"type": "shell",
"command": sh,
"args": [
"/path2script/scriptName.sh",
"${input:file_no}" // This does not work, without defining input again
]
}
]
}
Is there a way to pass input variables from launch.json to tasks.json in vscode?
You can use the extension Command Variable v1.21.0
It has a command extension.commandvariable.promptStringRemember that behaves the same as an ${input:name} promptString variable. By adding a key property the result is saved under this key and you can retreive it with the extension.commandvariable.rememberPick command.
The extension.commandvariable.rememberPick command can be used in a different task/launch than the extension.commandvariable.promptStringRemember command
{
"version": "2.0.0",
"tasks": [
{
"label": "Task 1",
"type": "shell",
"command": "dostuff1",
"args": ["-p", "${input:promptPath}"]
},
{
"label": "Task 2",
"type": "shell",
"command": "dostuff2",
"args": ["-p", "${input:rememberPath}"]
},
{
"label": "Do Task 1 and 2",
"dependsOrder": "sequence",
"dependsOn": ["Task 1", "Task 2"],
"problemMatcher": []
}
],
"inputs": [
{
"id": "promptPath",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "path",
"description": "Enter a path"
}
},
{
"id": "rememberPath",
"type": "command",
"command": "extension.commandvariable.rememberPick",
"args": { "key": "path" }
}
]
}
Following #rioV8 answer, I edited my json files as shown below:
launch.json:
"configurations": [
{
...
"args": [${input:file_no}]
"preLanuchTask": "runPreTasks"
...
}
],
"inputs": [
{
"id": "file_no",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "lastnumber",
"description": "Enter the number"
}
}
]
tasks.json:
{
"version": "2.0.0",
"tasks":[
{
"label": "runPreTasks",
"type": "shell",
"command": sh,
"args": [
"/path2script/scriptName.sh",
"${input:file_no}"
]
}
]
"inputs": [
{
"id": "file_no",
"type": "command",
"command": "extension.commandvariable.rememberPick",
"args": { "key": "lastnumber" }
}
]
}

add a save item to vscode editor context menu

Vscode noob here, trying to get a save item in the editor context menu. I've got as far as this:
{
"name": "vsContextSave",
"displayName": "vsContextSave",
"description": "add save item to editor context menu",
"version": "0.0.0",
"publisher": "njamescouk",
"repository": {
"type": "git",
"url": ""
},
"license": "MIT License",
"engines": {
"vscode": "^1.40.0"
},
"contributes": {
"menus": {
"editor/context": [
{
"command": "workbench.action.files.save",
"group": "9_cutcopypaste",
"when": "editorTextFocus"
}
]
}
},
"__metadata": {}
}
When wrapped up in a directory in the .vscode\extensions directory vscode sees it and vsContextSave shows up in the extension list and is enabled. However I don't see a save item in the context menu.
edit:
I've inserted an activation event just before the metadata statement, but apparently I need a main module with an activate() function in it.
"activation Events": ["onStartupFinished"],
these two files get you find, find file and save as context menu items in the editor.
extension.js:
no idea what this does but seems to be necessary
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
function activate(context) {
};
package.json:
{
"name": "mainContext",
"displayName": "mainContext",
"description": "add various items to editor context menu",
"version": "0.0.0",
"publisher": "njamescouk",
"repository": {
"type": "git",
"url": ""
},
"license": "MIT License",
"engines": {
"vscode": "^1.40.0"
},
"main": "./extension.js",
"contributes": {
"menus": {
"editor/context": [
{
"command": "workbench.action.files.save",
"group": "9_cutcopypaste",
"when": "editorTextFocus"
},
{
"command": "actions.find",
"group": "9_cutcopypaste",
"when": "editorTextFocus"
},
{
"command": "workbench.action.quickOpen",
"group": "9_cutcopypaste",
"when": "editorTextFocus"
}
]
},
"commands": [
{
"title": "Save",
"command": "workbench.action.files.save"
},
{
"title": "Find",
"command": "actions.find"
},
{
"title": "Find file",
"command": "workbench.action.quickOpen"
}
]
},
"activationEvents": [
"onStartupFinished"
],
"__metadata": {}
}

How to create a "terminal configuration macro" in VS Code? Open and split multiple terminals easily

~400 times by year 1 :
open VS Code
open one terminal
divide the terminal into 2 parts
run "npm run hot" (first split)
leave the other split terminal empty
open a second terminal (with "+" button)
split it into 3 parts
run "php artisan websockets:serve" (first part)
run "php artisan queue:words" (second part)
run "maidev --ip=localhost" (third part)
and finally I can start to work
I'm sure I am not alone with this problem.
Would anyone have a method to optimize that with one command or configuration ?
Thanks for your help !
Use this setting:
"terminal.integrated.windowsEnableConpty": false // true is the default
In tasks.json:
{
"label": "Run 2 terminals from tasks",
"dependsOrder": "sequence", // or parallel
"dependsOn": [
"OpenTerminal1",
"RunInTerminal1",
"RenameTerminal1", // if you care to rename each terminal
"SplitTerminal1",
"OpenTerminal2",
"RunInTerminal2a",
"RenameTerminal2a", // if you care to rename each terminal
"SplitTerminal2",
"RunInTerminal2b",
"RenameTerminal2b", // if you care to rename each terminal
"SplitTerminal2",
"RunInTerminal2c",
"RenameTerminal2c", // if you care to rename each terminal
],
// "runOptions": { "runOn": "folderOpen" } // or trigger with keybinding
},
That is the "master" task which calls all the constituent tasks, which are:
{
"label": "OpenTerminal1",
"command": "${command:workbench.action.terminal.new}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RunInTerminal1",
"command": "${input:runTerminal1}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RenameTerminal1",
"command": "${input:renameTerminal1}",
"type": "shell",
"problemMatcher": []
},
{
"label": "SplitTerminal1",
"command": "${command:workbench.action.terminal.split}",
"type": "shell",
"problemMatcher": []
},
// ------------------------------------------------------------------
{
"label": "OpenTerminal2",
"command": "${command:workbench.action.terminal.new}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RunInTerminal2a",
"command": "${input:runTerminal2a}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RenameTerminal2a",
"command": "${input:renameTerminal2a}",
"type": "shell",
"problemMatcher": []
},
// ------------------------------------------------------------------
{
"label": "SplitTerminal2",
"command": "${command:workbench.action.terminal.split}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RunInTerminal2b",
"command": "${input:runTerminal2b}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RenameTerminal2b",
"command": "${input:renameTerminal2b}",
"type": "shell",
"problemMatcher": []
},
// ------------------------------------------------------------------
{
"label": "RunInTerminal2c",
"command": "${input:runTerminal2c}",
"type": "shell",
"problemMatcher": []
},
{
"label": "RenameTerminal2c",
"command": "${input:renameTerminal2c}",
"type": "shell",
"problemMatcher": []
},
You might think that is enough but you can see that task require args (what to send to the terminals) so that I had to use this form:
"command": "${input:runTerminal2c}",
so that that input can provide the needed arguments and commands. Those go into the inputs section of your tasks file like so:
"inputs": [
{
"id": "openTerminal1",
"type": "command",
"command": "workbench.action.terminal.new",
},
{
"id": "runTerminal1",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "npm run test\u000D"
}
},
{
"id": "renameTerminal1",
"type": "command",
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "npm run hot"
}
},
{
"id": "openTerminal2",
"type": "command",
"command": "workbench.action.terminal.new",
},
{
"id": "runTerminal2a",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "php artisan websockets:serve"
}
},
{
"id": "renameTerminal2a",
"type": "command",
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "websockets:serve"
}
},
{
"id": "runTerminal2b",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "php artisan queue:words"
}
},
{
"id": "renameTerminal2b",
"type": "command",
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "queue:words"
}
},
{
"id": "runTerminal2c",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "maidev"
}
},
{
"id": "renameTerminal2c",
"type": "command",
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "maidev"
}
}
Again maybe you don't care about all the renaming the terminal commands and can eliminate those. Here, I just sent your command to the terminal:
{
"id": "runTerminal2c",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "maidev"
}
},
but did not automatically start them. To have them start immediately, add the return unicode to the text that is sent to the terminal like:
{
"id": "runTerminal2c",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "maidev --ip=localhost"
}
},
And finally a keybinding to trigger the master task (if you don't use the run on folder open option):
{
"key": "alt+z",
"command": "workbench.action.tasks.runTask",
"args": "Run 2 terminals from tasks"
},
I prefer the macro version - it is much shorter and less prone to set-up error. Both are very fast.
Okay, a day after answering this with other two answers, I saw this extension: Restore Terminals at this other question https://stackoverflow.com/a/62595681/836330
This extension seems to work quite well. Put this into your settings.json:
"restoreTerminals.runOnStartup": false, // true is the default
// set to false if using a keybinding or the command palette
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "server",
"commands": [
"npm run test"
]
},
{
"name": "empty",
}
]
},
{
"splitTerminals": [
{
"name": "websockets",
"commands": [
"ls -l"
]
},
{
"name": "queue",
"commands": [
"cd zip",
"gulp sass"
]
},
{
"name": "maidev",
"commands": [
"cd zip-multiple",
"gulp"
]
}
]
}
],
And a keybinding:
{
"key": "shift+alt+t", // whatever keybinding if you wish
"command": "restore-terminals.restoreTerminals",
},
If on Windows, I would still use the setting:
"terminal.integrated.windowsEnableConpty": false
as the same issue occurs when using this extension as any other method to open and write to terminals quickly - if you then try to close the terminals vscode will hang and eventually timeout and need to be reopened.
I'll make this two answers as one version is particularly long. One answer uses a macro extension to help solve this and the other uses only tasks.
For both solutions, use this setting:
"terminal.integrated.windowsEnableConpty": false // true is the default
There is a nasty unsolved bug affecting Winpty and vscode when you try to delete a terminal opened by these methods.
Using the macro extension multi-command. This goes into your tasks.json:
{
"label": "Run 2 terminals with macro",
"dependsOrder": "sequence", // or parallel
"dependsOn": [
"terminal1",
"terminal2"
],
// "runOptions": { "runOn": "folderOpen" }
},
{
"label": "terminal1",
"command": "${command:multiCommand.startFirstTerminal}"
},
{
"label": "terminal2",
"command": "${command:multiCommand.startSecondTerminal}"
},
The two tasks are run by the macro. This goes into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.startFirstTerminal",
"sequence": [
"workbench.action.terminal.newInActiveWorkspace",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "npm run test\u000D"
}
},
{
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "npm run hot"
}
},
"workbench.action.terminal.split",
]
},
{
"command": "multiCommand.startSecondTerminal",
"sequence": [
"workbench.action.terminal.newInActiveWorkspace",
{
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "websockets:serve"
}
},
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "ls -lrt\u000D"
}
},
"workbench.action.terminal.split",
{
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "queue:words"
}
},
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "ls -lrt\u000D"
}
},
"workbench.action.terminal.split",
{
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "maidev"
}
},
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "ls -lrt\u000D"
}
},
]
}
]
I renamed the terminals to match your commands - perhaps you don't care about that and can eliminate the renameWithArg sections to shorten the whole thing.
I couldn't test with your php setup, so I substituted ls -lrt\u000D running in each terminal. Just substitute your
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "php artisan websockets:serve\u000D"
}
},
for example. The \u000D is a return so the command runs immediately. Now you can either use the "runOptions": { "runOn": "folderOpen" } option so the master task runs when the workspace opens or assign a keybinding to the master task like
{
"key": "alt+z", // or watever you want
"command": "workbench.action.tasks.runTask",
"args": "Run 2 terminals with macro"
},