How to simultaneously create a new folder and multiple files in VSCode? - visual-studio-code

I discovered recently in Visual Studio Code that I can create a new folder and a new file simultaneously by using the following patten: Test/Test.jsx
eg.
1: Right click and select 'New File'.
2: Enter desired folder and file name.
3: The result from step 1 & 2.
Anyone know if it's possible to create a folder with multiple files using a similar pattern? This is what I'd like to be able to do.

I don't think you can do it the way you showed, but it is pretty easy to do it with a task. In your tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "new react folder and files",
"command": "mkdir ${input:dirName} && touch '${input:dirName}/${input:dirName}.component.jsx' '${input:dirName}/${input:dirName}.styles.jsx'",
"type": "shell",
"problemMatcher": [],
"presentation": {
"echo": false,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
}
],
// ........................................................................................
"inputs": [
{
"type": "promptString",
"id": "dirName",
"description": "Complete my folder name",
"default": "jsx folder to create"
}
]
}
And some keybinding to trigger the task (in your keybindings.json):
[
{
"key": "alt+j",
"command": "workbench.action.tasks.runTask",
"args": "new react folder and files",
}
]
This will prompt for the directory name and then create the folder and two files within it.
[I used bash commands mkdir and touch to create the folder and files, if you are using a shell without those commands swap out the ones you have.]

Related

Visual Studio Code User Individual Configuration for Project

I have a project created with defined tasks, for other users to use in my institution.
But now one of my tasks needs a parameter that should be a user defined directory.
How can each user have a configuration file in their project that my tasks can read and get the directory they want the out put to go?
This is a example of my task :
{
"label": "Generate File",
"type": "shell",
"command": "cscript.exe",
"args": [
"//NoLogo",
"${workspaceFolder}/scripts/build.vbs",
"0",
"${output}",
],
"options": {
"cwd": "${workspaceFolder}"
},
The output should be a folder define by each of the users in a configuration or setting file they can put in their project.
Edit :
As Requested i tried to do this
inside my .vscode folder i created a file named settings.json
with this as content
{
"env": {
"dirLoc": "C:\\output\\"
}
}
and tried to use this variable in my tasks.json file
{
"label": "Generate File",
"type": "shell",
"command": "cscript.exe",
"args": [
"//NoLogo",
"${workspaceFolder}/scripts/build.vbs",
"0",
"${env:dirLoc}",
],
"options": {
"cwd": "${workspaceFolder}"
},
but still i did not get anything in the value, it came blank
You can use the extension Command Variable.
It has the command extension.commandvariable.file.content where you can read file content and use it in a variable in the task or launch.
The file can be plain text, key-value, json and yaml.

How to open zsh terminals using vs code tasks in wsl?

My aim is to open zsh terminals in the current working directory of wsl whenever I open a workspace. For example, if I open a workspace with three folders, viz. test1, test2, and test3, I want one terminal in each directory to open in vs code.
For that purpose, I created .vscode/tasks.json file in each directory and input the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "test1",
"type": "shell",
"command": "",
"isBackground": true,
"problemMatcher": [],
"options": {
"shell": {
"executable": "zsh",
"args": []
}
},
"runOptions": {
"runOn": "folderOpen"
},
"presentation": {
"reveal": "always",
"panel": "dedicated"
}
}
]
}
But in the terminal, I get this error:
* Executing task in folder illuminate:
/usr/bin/zsh: can't open input file:
* The terminal process "zsh ''" failed to launch (exit code: 127).
* Terminal will be reused by tasks, press any key to close it.
I checked the docs, but it includes settings only useful for windows, not linux. Is there any way to achieve this?

Is it possible to specify user specific pre commands in VS Code?

We have a slew of folks doing development through the same GitLab repo. We are using VS Code tasks to execute internal commands. The main command is the same for everyone: internal_command on Windows and internalCommand on Linux.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
}
}
]
}
This works as expected.
Some users need/want to run a specific command before the main command. For example, one use wants to rename a file, another user wants to change an environment variable, etc...
We don't want to have multiple versions of .vscode/tasks.json cause that is a mess when pushing things to GitLab.
So I am wondering if there is a way to specify user specific tasks in the project's .vscode/tasks.json file?
You can with the help of the extension Command Variable it allows you to use the content of a file as a command in the terminal. The file can also contain Key-Value pairs or be a JSON file.
Say you store this userTask.txt or userTask.json file in the .vscode folder and add the file to the .gitignore file.
With the current version of the extension the file userTask.txt has to exist, I will add an option to supply alternative text in case the file does not exist. You can fill the file with a dummy command like echo No User Task
Set up your task.json like
{
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
},
"dependsOrder": "sequence",
"dependsOn": ["userTask"]
},
{
"label" : "userTask",
"type" : "shell",
"command": "${input:getUserTask}"
}
],
"inputs": [
{
"id": "getUserTask",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/userTask.txt"
}
}
]
}

Is there a VS Code shortcut for creating a new file from selected or clipboard code?

I work with large html files that I would like to fragment into separate files. The process of doing this is quite tedious as it requires copying the code, creating a new file, pasting it in the new file, and then selecting a folder and a new name to save it under.
Is there a built-in shortcut, macro or extension for VS Code for making this easier?
Just a note for others that since this is an html file, the new refactor Move to a new file is not available. It does what you want and will work in many other languages, but not html. You can access it by selecting the text to move and in the context menu choose Refactor.. - it may even add import statements to the old file if supported.
Here is a macro which does what you want. I am using the macro extension multi-command but there are others.
In settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.newFileWithContent",
"sequence": [
// choose which one you want
"editor.action.clipboardCutAction",
// "editor.action.clipboardCopyAction",
"workbench.action.files.newUntitledFile",
"editor.action.clipboardPasteAction",
// prompt for save immediately?
"workbench.action.files.saveAs",
]
},
Then trigger it either through the command palette (search for "multi") or with a keybinding (keybindings.json):
{
"key": "strl+alt+b", // your keybinding choice
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.newFileWithContent" }
},
I don't know how to automate the "selecting a folder and a new name to save it under" part of your question. I think you are still going to have to do that manually, but there is some handy "intellisense" for that in the "saveAs" dialog.
Update in 2020
After I came up with this answer, see in vscode how can I quickly generate a new file with datetime in the name?
I thought there might be a better way to handle creating the file with a task and prompting for a folder and filename in one go. You lose the saveAs intellisense on your folder structure, but it is a pretty good technique to know in any case. And a macro isn't needed. In a bash shell:
{
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
// assuming your folder name isn't static
"command": "echo '${selectedText}' > ${input:folderName}/${input:fileName}.html",
"type": "shell",
"problemMatcher": [],
"presentation": { // terminal handling which you may not care about and could delete
"echo": false,
"reveal": "silent",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
"promptOnClose": false
}
],
"inputs": [
{
"type": "promptString",
"id": "folderName",
"description": "Complete my folder name.",
"default": "folder"
},
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
Some keybinding to run that task (or just run it from the command palette Run Task command):
{
"key": "alt+r", // whatever you choose
"command": "workbench.action.tasks.runTask",
"args": "newFile"
},
That's it, select your task, and run the task Alt+R.

VSCode: Open new terminal as part of task?

Visual Studio Code was just updated to allow running a task and having them open in a split terminal. This is great, however I'm looking for one more thing to make this perfect.
I would like to be able to open a total of 3 terminals via a task. One for my NPM build, one for my backend MAVEN build, and a third that is just a blank new terminal I can use for git commands when needed.
I can't seem to find a way to tell VSC to run a task that just opens a new terminal ready to use without providing it a command. I would even settle with giving it a simple command like "node -v" just to start it out, as long as that panel is still usable after. Right now it wants to close it after it has ran.
Here is my task setup: I have one task setup as the build task that depends on two others. I envision adding a third one to that which would just open the new terminal:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Maven and NPM",
"dependsOn": [ "maven", "npm" ],
"group": {
"kind": "build",
"isDefault": true,
},
},
{
"label": "maven",
"command": "...",
"type": "shell",
"presentation": {
"reveal": "always",
"group": "build"
},
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "npm",
"type": "shell",
"command": "ng serve --port 4203 --proxy-config proxy.conf.json",
"presentation": {
"reveal": "always",
"group": "build"
},
"options": {
"cwd": "${workspaceRoot}/client-APS"
}
}
]
}
The following should work:
{
"type": "process",
"label": "terminal",
"command": "/bin/bash", // <-- your shell here
"args": [
"-l" // login shell for bash
],
"problemMatcher": [],
"presentation": {
"echo": false, // silence "Executing task ..."
"focus": true,
"group": "build", // some arbitrary name for the group
"panel": "dedicated"
},
"runOptions": {
"runOn": "folderOpen"
}
}
I was trying to achieve something very similar when I stumbled my way into this solution: Here, I'm auto-launching (and setting the focus on) the terminal when the folder is opened in vscode -- and further tasks that share the same presentation.group gets placed in split terminals when they're run (with new vs. reused splits depending on their presentation.panel)
(The runOptions bit is superfluous for your case, but I'm keeping it in case it is helpful for someone)
Note: For this example, you may or may not need the -l option depending on your settings for terminal.integrated.shell*, terminal.integrated.automationShell* and terminal.integrated.inheritEnv -- this issue has some discussion on what is involved in setting up the shell environment.