Skip to content

Instantly share code, notes, and snippets.

@Diogo-Rossi
Last active October 25, 2024 03:03
Show Gist options
  • Save Diogo-Rossi/b0abcd9e52ce00e1bb2a12bc0d6beafc to your computer and use it in GitHub Desktop.
Save Diogo-Rossi/b0abcd9e52ce00e1bb2a12bc0d6beafc to your computer and use it in GitHub Desktop.
Workaround to make a VSCode settings sync to a regular GitHub repository

Requirements:

  • git
  • python
  • code (cli of VSCode)

All reachable from the PATH of the system

Steps: (9 steps)

  1. Using the portable version of VS Code, there is a /data/ folder in the software's directory

  2. This /data/ folder will be the repository. So, initialize it with git init

    $ git init	
    

    OBS:
    A normal installation (not portable) may work as well. But then, the folder with settings is located in %APPDATA%\Code\User\ (Windows system). The advantage in using the /data/ folder of a portable installation is the access to the extensions folder in the same directory. This folder is in a different default location in a normal installation (%USERPROFILE%\.vscode\extensions\ in Windows system), which can be changed with the --extensions-dir command line option (See the docs)

  3. Add manually any files you want. Normally, we want to sync files from the user-data/User/ folder:

    $ git add user-data/User/keybindings.json
    $ git add user-data/User/settings.json
    $ git add user-data/User/snippets/javascript.json
    ...
    

    But it could be also any files from the /extensions/ folder, or the root folder (/data/) as well.

  4. Create an empty text file extensions.txt and add it to git

    $ touch extensions.txt
    $ git add extensions.txt
    
  5. Put this python script on the repository's root folder (i.e. the /data/ folder) and add it to git

    $ git add syncextensions.py
    

    Obs:
    In the line 3 of the script, you can configure the list ignoreextensions with extensions IDs you don't want to sync.

  6. Ignore everything else and add the .gitignore file to git

    $ echo "*" > .gitignore
    $ git add -f .gitignore
    
  7. Now, create the remote repository on GitHub, add the remote, commit and push to it.

    $ git remote add origin <remote-url>
    $ git commit -m "first commit"
    $ git push --set-upstream origin master
    
  8. Configure these two keybindings on keybindings.json

    {
        "key": "ctrl+shift+alt+d",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "python -c \"import os; os.chdir(os.path.dirname(r'${execPath}') + r'\\data'); os.system('git pull'); os.system('python syncextensions.py')\" \u000D"
        }
    },
    {
        "key": "ctrl+shift+alt+u",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "python -c \"import os; os.chdir(os.path.dirname(r'${execPath}') + r'\\data'); os.system('python syncextensions.py & git status & git commit -am sync_settings & git push')\"\u000D"
        }
    }
  9. Done. Now, the keybinding ctrl+shift+alt+d will download the synced settings from the remote repository and the keybinding ctrl+shift+alt+u will upload the settings to the remote repository.

Notes:

  1. The first time you execute the command, it will prompt for uninstall every extension, since the extensions.txt file is empty. Just answer "no" to all of them. From the second time on, it will have the extension list.

  2. You can change the commit message for push or leave it empty (removing the -m argument) to insert a different commit message in every push. You can also edit the local repository by adding new files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment