I have a repository on my local system and am testing my changes on a remote server. I am using VSCode for development. I want that on every save, rsync should run in background and sync the changes in the current local file with remote. How can I achieve this with VSCode?

asked Dec 8, 2020 at 17:24

Abhay Gupta's user avatar

Abhay GuptaAbhay Gupta

8161 gold badge14 silver badges32 bronze badges

I am using Save and Run vscode extension for syncing code between local and remote server whenever I hit save in VSCode. Following is the configuration I am using.

"saveAndRun": {
  "commands": [
    {
      "match": ".*",
      "cmd": "rsync -av --no-owner ${file} {destination folder}${relativeFile}",
      "useShortcut": false,
      "silent": false
    },
  ]
}

The output generated when the rsync command is run can been seen in VSCode terminal. You can add the above configuration in .vscode/settings.json file. You can learn more about editing settings.json file here

user1635327's user avatar

answered Dec 9, 2020 at 16:55

Abhay Gupta's user avatar

Abhay GuptaAbhay Gupta

8161 gold badge14 silver badges32 bronze badges

The Sync-Rsync extension is designed to do this.

answered Jul 7, 2021 at 22:46

JW.'s user avatar

JW.JW.

51.4k39 gold badges120 silver badges148 bronze badges

1

Here is extension code which uses the onDidSaveTextDocument event to run code after a file is saved. The code uses the exec function of the child_process package to run a command. In this case, it launches the chrome browser.

import * as vscode from 'vscode';
import * as path from 'path';

import * as cp from 'child_process';

export function activate(context: vscode.ExtensionContext)
{
    vscode.workspace.onDidSaveTextDocument((d) =>
    {
        vscode.window.showInformationMessage("Document saved:" + path.basename(d.fileName));

        const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
        cp.exec(start + ' chrome ' );
    });
}

answered Dec 9, 2020 at 14:41

RockBoro's user avatar

RockBoroRockBoro

2,3832 gold badges23 silver badges37 bronze badges