How To Backup Your VS Code Extensions And Settings
Backup your Visual Studio Code extension list and configuration files using Linux terminal
Having the same working environment across multiple machines is always a good thing. This might be a case for your system configuration files (dotfiles) and your development tools alike. What I'm going to show you this time around is a simple script that can help with the task of backing up your Visual Studio Code settings and extensions for use on your secondary PC, virtual environment or even your computer at work.
Setting up the backup
For this article, I'm going to suppose that you have VS Code already installed on your system and want to backup the settings and extensions you've spent some of your precious time setting up. We'll be using the old trusty Linux terminal magic and VS Code's command line utility code.
Our basic command for exporting the list of extensions is going to look like this:
code --list-extensions | xargs -L 1 echo code --install-extension
The code --list-extensions command exports the list of installed VS Code extensions which we then pipe into xargs, this transforms each extension's name into an extension install command. The output of this command looks like this:
code --install-extension anseki.vscode-color
code --install-extension autsenc.laravel-blade-spacer
code --install-extension Dart-Code.flutter
As you see we've successfully transformed the extension names to installation commands via xargs. You can output these into a file like this:
mkdir /home/$USER/code-backup && cd /home/$USER/code-backup
code --list-extensions | xargs -L 1 echo code --install-extension > ext
Now we also need to export the configuration files. These usually reside in your .config directory. The config files are saved in JSON format, so to back these up, you would simply tar them and save them somewhere in the cloud. Alternatively, you could push these into a Git repo.
cd /home/$USER/.config/Code/User
tar -czf code-config.tar.gz *.json
mv code-config.tar.gz /home/$USER/code-backup
In your code-backup folder, you should have your config files in a tar archive and your extension install script as ext. If you want to restore these on another machine, you'd just copy the code-backup directory's contents over and reverse the process shown above (that is, untar the config files into VS Code config directory and run the ext script)
Wrapping up
This was just a basic example of backing up your files. If you don't fancy writing backup scripts, you can check out my own full and enhanced implementation of this backup workflow over here. There is also a VS Code extension for synchronizing your settings called Settings Sync that synchronizes everything for you into a GitHub Gist.