DEV Community

Elliot DeNolf
Elliot DeNolf

Posted on • Originally published at elliotdenolf.com on

4 1

Backup and Restore Visual Studio Code Extensions from the Command Line

Originally posted on my blog at elliotdenolf.com

I maintain my own dotfiles to be able to have identical setups between my work and personal machines. My Visual Studio Code configuration is one piece of those. While there is an extension out there that does extension syncing, I didn't find them ideal for my use-case as it also did a settings sync.

Backup Extensions

Let's output our list of extensions to a file.

> code --list-extensions > extensions.txt
Enter fullscreen mode Exit fullscreen mode

The file content will be similar to the following:

donjayamanne.githistory
eamodio.gitlens
johnpapa.vscode-peacock
ms-azuretools.vscode-docker
yzhang.markdown-all-in-one
Enter fullscreen mode Exit fullscreen mode

Restore Extensions

Once we have our file listing our extensions, we can restore our extensions.

> cat extensions.txt | xargs -L 1 code --install-extension
Enter fullscreen mode Exit fullscreen mode

This line performs the code --install-extension command for every line in your file.

Use with a Script

To make these commands easier to use, we can use a script that wraps the code command. The following script will add code save-ext and code install-ext as commands. Any other standard command will simply be passed through.

#!/usr/bin/env bash

if command -v code >/dev/null 2>&1; then
  code() {
    case "$1" in
    save-ext)
      echo "Saving code extensions..."
      code --list-extensions > ~/.dotfiles/vscode/extensions.txt
      ;;
    install-ext)
      echo "Installing code extensions..."
      cat ~/.dotfiles/vscode/extensions.txt | xargs -L 1 code --install-extension
      ;;
    *)
      command code "$@"
      ;;
    esac
  }
fi
Enter fullscreen mode Exit fullscreen mode

To use this script, modify the extensions.txt file path and also add the script to your PATH.

  • code save-ext will output your extensions to file
  • code install-ext will install them from file

I'd recommend saving this file to your personal dotfiles to share between your dev environments. Here are mine.

Links

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
abelardoit profile image
abelardoit • Edited

Hi there,

I am doing something wrong since the .dotfiles directory is not created. 🤔

Collapse
 
denolfe profile image
Elliot DeNolf

You will either need to create that directory structure or change it to something that exists.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay