Automate VSCode Extension Setup: The Quick & Easy Way
code --install-extension <extension_id>. For a "magic command" to install Python extensions, try: bash <(curl -s https://raw.githubusercontent.com/StatPan/vscode-extension-install/refs/heads/master/scripts/python.sh)
            🤔 Problem: Manually Installing VSCode Extensions is Cumbersome, Isn't It?
Each time you start a new project or reset your development environment, the process of manually installing VSCode extensions one by one can be tedious and time-consuming. This is especially true for developers who work with various projects like Python, JavaScript, and Git, where the number of required extensions increases, making the process even more cumbersome. Moreover, there's a high chance of accidentally missing a necessary extension or installing the wrong version.
💡 Solution: Automate Extension Installation with a Single Command!
To solve this inconvenience, we introduce a method to automate VSCode extension installation with a single command! No more manually searching for and installing extensions. By using this method, you can predefine the extensions required for a project and install them all at once with a simple command.
📁 Project Structure Overview
This automation tool consists of a simple structure:
- all.sh: This script is the core! Running this script automatically installs all extensions.
- readme.md: A document containing a description of the project.
- scripts/: This directory contains- .shfiles with extension lists. For example, you can create and manage files by category, such as- git.shand- python.sh.
⚙️ How Does It Work?
This automation tool works through the following steps:
- First, the all.shscript checks if VSCode is installed by looking for thecodecommand in your system's PATH. If VSCode is not found, the script attempts to install it automatically.
- If VSCode is not already installed, the script will attempt to install it. It detects the operating system (Linux, macOS) and uses the appropriate package manager (apt-get, yum, dnf, brew) to install VSCode. For Windows, it provides a link to download VSCode manually.
- The script then navigates to the scripts/directory and identifies all files ending with the.shextension. It excludes itself (all.sh) from this process.
- For each .shfile found, the script "sources" the file, which means it executes the script in the current shell environment. This makes the variables defined in the script, especially theextensionsarray, available to theall.shscript.
- The script iterates through the extensionsarray in each sourced.shfile. This array contains the IDs of the VSCode extensions to be installed.
- The script adds each extension ID to a master list, ensuring that duplicate IDs are removed to prevent redundant installation attempts.
- Finally, the script uses the code --install-extensioncommand to install each extension in the master list. Thecodecommand is VSCode's command-line interface, and--install-extensionis an option that tells VSCode to install the specified extension.
- The script handles potential errors during the installation process, such as when an extension ID is invalid or the extension cannot be found.
📝 Example Script Files (scripts/*.sh)
            
                The scripts/ directory contains .sh files that serve as categorized lists of VSCode extensions. This structure allows you to organize extensions based on your project types or development needs. Each .sh file defines an extensions array containing the IDs of the extensions to be installed. Using extension IDs ensures that the correct extensions are installed quickly and accurately.
            
scripts/git.sh
            
#!/bin/bash
# file: scripts/git.sh
extensions=(
    eamodio.gitlens
    GitHub.vscode-pull-request-github
    mhutchie.git-graph
    # ... Add the necessary Git-related extension IDs here.
)
            
                This git.sh file contains extension IDs related to Git functionality, such as GitLens and GitHub Pull Requests.
            
scripts/python.sh
            
#!/bin/bash
# file: scripts/python.sh
extensions=(
  "ms-python.python"
  "ms-python.isort"
  # ... Add the necessary Python-related extension IDs here.
)
            
                This python.sh file includes extension IDs for Python development, such as the official Python extension and the isort extension for sorting imports.
            
scripts/js.sh
            
#!/bin/bash
# file: scripts/js.sh
extensions=(
    sidthesloth.html5-boilerplate
    # ... Add the necessary JavaScript-related extension IDs here.
)
            
                This js.sh file contains extension IDs for JavaScript development.
            
🚀 How to Use: It Can't Be Simpler!
Now, let's see how to actually use it. It's really simple!
- Clone the repository: Use the command git cloneto download the automation tool to your local machine. Replace- Navigate to the directory: Use the command cd vscode-extension-installto enter the directory where you cloned the repository.
- Make the script executable: Use the command chmod +x all.shto give theall.shscript execute permissions. This is necessary to run the script.
- Run the script: Execute the ./all.shcommand. This will automatically install all the extensions listed in thescripts/*.shfiles.
➕ Adding/Modifying Extensions: Customize to Your Taste!
Do you want to add necessary extensions or remove existing ones? Don't worry! It's very easy to do.
- Open the scripts/*.shfile: Use a text editor or VSCode itself to open the.shfile that corresponds to the category of extensions you want to modify (e.g.,git.sh,python.sh). These files are located in thescripts/directory.
- Modify the extensionsarray: Inside the file, you'll find an array namedextensions. To add an extension, simply add its ID to the array. To remove an extension, delete its ID from the array. Ensure that the extension IDs are enclosed in double quotes.
- Save the file: After making the necessary changes, save the .shfile.
- Run the all.shscript again: Execute the./all.shcommand in the terminal. This will update your VSCode installation with the modified extension list.
