VS Code SFTP

repository·develop·Indexed 19 days ago

https://github.com/natizyskunk/vscode-sftp

A VS Code extension for file synchronization between local directories and remote servers using FTP or SSH protocols. Version 1.16.3 supports upload on save, directory syncing, multiple connection profiles, SSH connection hopping, and a Remote Explorer for browsing remote files. Configuration is managed via a .vscode/sftp.json file.

Tokens
14.6K
Snippets
61
Records
77
Agent score
67%

What's inside vscode-sftp

  1. Use SFTP profiles for different environments

    develop

    You can define multiple environments (like dev and prod) within a single sftp.json file using the profiles key. This allows you to switch between different remote hosts and paths easily.

    Important: The context and watcher keys are only available at the root level of the configuration and do not apply to individual profiles.

    To switch between profiles, use the command SFTP: Set Profile from the Command Palette.

    {
      "username": "username",
      "password": "password",
      "remotePath": "/remote/workspace/a",
      "watcher": {
        "files": "dist/*.{js,css}",
        "autoUpload": false,
        "autoDelete": false
      },
      "profiles": {
        "dev": {
          "host": "dev-host",
          "remotePath": "/dev",
          "uploadOnSave": true
        },
        "prod": {
          "host": "prod-host",
          "remotePath": "/prod"
        }
      },
      "defaultProfile": "dev"
    }
  2. Set up SSH connection hopping

    develop

    You can connect to a target server through one or more proxy (hop) servers using the SSH protocol.

    Note: Variable substitution is not supported in hop configurations.

    Single Hop (local -> hop -> target)

    Define the primary connection details (the hop) at the root, and the target server details inside the hop object.

    Multiple Hop (local -> hopa -> hopb -> target)

    Use an array for the hop key to define a chain of servers.

    // Single Hop Example
    {
      "name": "target",
      "remotePath": "/path/in/target",
      "host": "hopHost",
      "username": "hopUsername",
      "privateKeyPath": "/Users/localUser/.ssh/id_rsa",
      "hop": {
        "host": "targetHost",
        "username": "targetUsername",
        "privateKeyPath": "/Users/hopUser/.ssh/id_rsa"
      }
    }
  3. Upload files as root using sshCustomParams

    develop

    To attempt uploading files with root privileges, you can add the following parameter to your sftp.json configuration. Note that this is a workaround and may not work for all environments.

    "sshCustomParams": "sudo su -;"
  4. Configure automatic two-way synchronization

    develop

    You can achieve automatic synchronization (including deleting extraneous files from the destination) by using the watcher and syncOption properties. This is useful for keeping a server in sync with local Git branch changes.

    Note: If watcher.autoUpload is set to true and watcher.files is set to **/*, you should set uploadOnSave to false to avoid conflicts.

    {
      "name": "My Server",
      "host": "<host_ip_address>",
      "protocol": "sftp",
      "port": 22,
      "username": "user1",
      "remotePath": "/folder1/folder2/folder3",
      "uploadOnSave": false,
      "watcher": {
        "files": "**/*",
        "autoUpload": true,
        "autoDelete": true
      },
      "syncOption": {
        "delete": true
      }
    }
  5. Configure SFTP sync with sftp.json

    develop

    To set up a sync connection, follow these steps:

    1. Open a local directory in VS Code.
    2. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and run SFTP: config.
    3. This creates a .vscode/sftp.json file. Edit this file with your remote server details.

    Note: If you omit the password field, you will be prompted for it during sync operations. Special characters like backslashes must be escaped with a backslash.

    {
        "name": "Profile Name",
        "host": "name_of_remote_host",
        "protocol": "ftp",
        "port": 21,
        "secure": true,
        "username": "username",
        "remotePath": "/public_html/project",
        "password": "password",
        "uploadOnSave": false
    }
  6. Use the Remote Explorer

    develop

    The Remote Explorer allows you to browse files on the remote server directly within VS Code.

    • To open: Run the command View: Show SFTP or click the SFTP icon in the Activity Bar.
    • Viewing files: You can view file contents directly in the explorer. To edit a remote file locally, run the command SFTP: Edit in Local.
    • Bulk actions: You can select multiple files or folders using Ctrl or Shift to perform bulk upload or download operations.
    • Ordering: You can control the position of the Remote Explorer in your sidebar by setting the remoteExplorer.order parameter in sftp.json (default is 0).
    {
      "remoteExplorer": {
        "order": 1
      }
    }
  7. Show dotfiles/hidden files in ProFTPD remote explorer

    develop

    If you are using ProFTPD and cannot see hidden files (dotfiles) in the VS Code remote explorer, you must modify your server's proftpd.conf file.

    Locate the ListOptions parameter and change its value from "-l" to "-la" within the <Global> section.

    #Global settings
    <Global>
    [...]
    ListOptions  "-la"
    [...]
    </Global>
  8. Install the VS Code SFTP extension

    develop

    You can install the extension using two methods. It is highly recommended to uninstall any existing version from @liximomo before proceeding to avoid conflicts.

    1. Open the Extensions view (Ctrl + Shift + X).
    2. Uninstall the existing sftp extension by @liximomo.
    3. Search for and install Natizyskunk.sftp from the Marketplace.

    Method 2: Manual VSIX Installation

    1. Open the Extensions view (Ctrl + Shift + X).
    2. Uninstall the existing sftp extension by @liximomo.
    3. Click the ellipsis (...) in the Extensions view and select Install from VSIX....
    4. Select your downloaded .vsix file and reload VS Code.
  9. Upload folder contents without the parent folder

    develop

    To upload the contents of a local directory to a remote path without creating the local directory itself on the remote, set the context property to ./[your-folder-name] (e.g., ./build).

    {
      "name": "My Server",
      "host": "<host_ip_address>",
      "protocol": "sftp",
      "port": 22,
      "username": "user1",
      "remotePath": "/folder1/folder2/folder3",
      "context": "./build",
      "uploadOnSave": false,
      "watcher": {
        "files": "*.{js,html}",
        "autoUpload": true,
        "autoDelete": false
      }
    }