Is there some way to automatically install VS Code extensions when opening a project (like package.json but for vscode?)
If not, is there some way to install extensions at the command line?
Is there some way to automatically install VS Code extensions when opening a project (like package.json but for vscode?)
If not, is there some way to install extensions at the command line?
The recommended way of doing this is through workspace recommended extensions, this is a .vscode/extensions.json in your project that will prompt the user to install them when they first open the folder, the file looks like this:
{
"recommendations": [
"eg2.tslint",
"dbaeumer.vscode-eslint",
"msjsdiag.debugger-for-chrome"
]
}
You can also try setting up a bash/bat script or some other automation to directly install the extensions using the CLI as suggested by parsley72 in the other answer. You will likely annoy users by doing this though, unless this is in a personal dotfiles project or something similar.
See nybon's answer for configuring recommendations in the UI.
Thanks to Daniel's suggestion, and I find if you want to keep a list of vscode extensions and install all of them in single click, you can try this approach:
Configure Recommended Extensions command to create the extensions.json file
You will create a file like this:{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"ms-vscode.cpptools",
"file-icons.file-icons",
"shd101wyy.markdown-preview-enhanced",
"sagebind.mathpad",
"johnpapa.vscode-peacock"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
The issue that @daniel-imms created was resolved in June 2017. You can see this in the latest version:
$ code -h
Visual Studio Code 1.16.0
Usage: code [options] [paths...]
Options:
--extensions-dir <dir> Set the root path for extensions.
--list-extensions List the installed extensions.
--show-versions Show versions of installed extensions, when using --list-extension.
--install-extension (<extension-id> | <extension-vsix-path>) Installs an extension.
--uninstall-extension <extension-id> Uninstalls an extension.
--enable-proposed-api <extension-id> Enables proposed api features for an extension.
--disable-extensions Disable all installed extensions.
codium -hSee https://code.visualstudio.com/docs/editor/extension-gallery#_command-line-extension-management
I wrote this Makefile to automatically install extensions
freeze-extensions:
code --list-extensions > extensions.txt
install-extensions:
cat extensions.txt | xargs -L 1 code --install-extension
I have developed a task that do the right job for you, gathering all the answers of this thread and my own powershell experience:
You need to create a .vscode/tasks.json file into your workspace wich contains this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Install All Recommended Extensions",
"type": "shell",
"windows": {
"command": "foreach ($ext in (Get-Content -Raw .vscode/extensions.json | ConvertFrom-Json).recommendations) { Write-Host Installing $ext; code --install-extension $ext; }"
},
"linux": {
"command": "cat .vscode/extensions.json | jq .recommendations[] | xargs -n 1 code . --install-extension"
},
"runOptions": {
"runOn": "folderOpen"
},
"presentation": {
"reveal": "silent"
},
"problemMatcher" : []
},
]
}
This will parse your .vscode/extensions.json and will install your extensions for you!
It works on linux and windows. Linux users needs to install jq before using it:
sudo apt-get update && sudo apt-get install jq
You can create a VS Code task using the .runOptions.runOn == "folderOpen" option to install them when opening the workspace.
Create (or update) file in .vscode/tasks.json with the following. This example assumes jq is installed and the code CLI command is available.
{
"version": "2.0.0",
"tasks": [
{
"label": "Install All Recommended Extensions",
"type": "shell",
"command": "jq -r '.recommendations[]' ./.vscode/extensions.json | xargs -L 1 code --install-extension",
"runOptions": {
"runOn": "folderOpen"
},
"presentation": {
"reveal": "silent"
}
}
]
}
For projects where Node.JS/npm is available, this one-liner(-ish) will install all recommended extensions for the workspace via the command line in a single step:
npx json5 .vscode/extensions.json | \
npx json-cli-tool --path=recommendations --output=newline | \
xargs -L 1 code --install-extension
You could imagine documenting this step in a README's local development setup instructions.
It uses npx, which will install the json-cli-tool and json5 packages from npm if they're not already installed.
# Compile the extensions.json file from JSON (with comments) to valid JSON
# That's because VS Code's setting files often contain comments
# If yours doesn't, you can skip this step
npx json5 .vscode/extensions.json | \
# Traverse the JSON and extract all extension names
# on a new line from the "recommendations" array
npx json-cli-tool --path=recommendations --output=newline | \
# Which should output:
# "dbaeumer.vscode-eslint"
# "stylelint.vscode-stylelint"
# Pass each line from the command above to VS Code's CLI
# as seen in [@weaming's solution][2]
xargs -L 1 code --install-extension
one-liner to install extensions.json recommendations IF you have jq installed:
cat .vscode/extensions.json | jq .recommendations[] | xargs -n 1 code --install-extension
or with --force (which will install latest version of each extension if they are already installed):
cat .vscode/extensions.json | jq .recommendations[] |xargs -I {} echo "code --install-extension {} --force" | bash