Editing Your Domain Directly in VS Code Using SFTP
Managing your website’s files directly from your code editor can greatly boost productivity, especially when working on remote servers. Visual Studio Code (VS Code) offers a powerful tool for this with the SFTP extension, letting you open, edit, and save files on your domain easily. This article will show you how to set up and use the SFTP extension in VS Code to manage and edit your domain’s files effortlessly.
Introduction to VS Code and SFTP
Managing and editing files on your web server can be tough, especially with many files and directories. Web developers have often used FTP clients for this, but it can be inconvenient and separate from the coding environment. With Visual Studio Code (VS Code) and the SFTP extension, you can connect to your web server, open files, and edit them in the same editor.
VS Code is a lightweight but powerful code editor developed by Microsoft. It's popular among developers because it has many features and a large library of extensions that add more functions. One useful extension is SFTP (Secure File Transfer Protocol), which lets you connect to your remote server and manage files directly in VS Code.
Requirements
Before starting the setup, make sure you have these things:
Make sure you have the latest version of VS Code installed on your computer.
Make sure you have installed the SFTP extension by Natizyskunk from the VS Code marketplace.
You need access to your SFTP credentials, which usually include the server address, username, password, and port.
Setting Up the SFTP Extension in VS Code
Before you can start editing your domain's files, you need to install the SFTP extension in VS Code and set it up to connect to your server.
Installing the SFTP Extension:
Open VS Code and navigate to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
In the search bar, type "SFTP" and look for the extension named "SFTP" by Natizyskunk.
Click "Install" to add the extension to your VS Code.
Configuring SFTP in Your Workspace:
Once installed, open the Command Palette by pressing
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(macOS).Type
SFTP: Config
and select it to create a newsftp.json
configuration file in your project.In the
sftp.json
file, you’ll need to specify the connection details for your domain. Here’s a basic example:{ "name": "connectionname", "host": "yourhost", "protocol": "sftp", "port" : 22, "username": "yourusername", "password": "yourpassword", "remotePath": "/path/to/your/domain/files", "uploadOnSave": true, "useTempFile": true }
Enter the following details:
name
: A name for your connection.host
: The domain name or IP address of your server.protocol
: The protocol to use, which is "sftp" in this case.port
: The port number (22 is default for SFTP).username
: Your server’s username.password
: Your server’s password.remotePath
: The path to your domain’s root directory on your server where you want to open your filesuploadOnSave
: Set this totrue
to automatically upload files and any changes when you save them.useTempFile
: Set this tofalse
to upload the file directly to the remote server as soon as you save it in VS Code. If set totrue
, the extension first creates a temporary file on the remote server when you save a file in VS Code. After the temporary file is uploaded, it renames it to the original file name. This helps prevent incomplete files if the connection is interrupted, but the server must support this feature.
Connecting to Your Domain:
After configuring the
sftp.json
file, you can connect to your server by clicking on the SFTP icon in the Activity Bar.Select the "Remote Explorer" view, and you should see your domain listed there.
Click on the domain to expand it, and you’ll see all the files and directories on your server. You can now open, edit, and save these files directly from VS Code.
Editing Files on Your Domain
Once connected, editing files on your domain is as simple as working with local files. You can:
Right-click on the file in the Remote Explorer and select "Edit in Local".
Make changes to the file using VS Code’s powerful editing features, including IntelliSense, syntax highlighting, and more.
Simply press
Ctrl + S
(Windows/Linux) orCmd + S
(macOS) to save your changes. If the "Auto Save" feature is turned on in your VS Code, it will make the process seamless. The SFTP extension will automatically upload the updated file to your server.
Additional Features
The SFTP extension offers additional features that can enhance your workflow:
You can upload files by right-clicking on any file or folder in your local project and selecting "Upload File" or "Upload Folder" to transfer it to your remote server.
You can download files by right-clicking on any file or folder in the remote explorer and selecting "Download File" or "Download Folder" to transfer it to your local project.
You can sync entire folders and directories between your local machine and the server.
The extension can watch for changes in specific files or directories and automatically upload them.
You can use SSH Key Authentication for added security.
Additional Tips for Using SFTP with VS Code
If you manage multiple domains, you can create separate
sftp.json
files for each one. Just make sure to place them in the correct workspace directory.For large projects, consider excluding certain files or directories from syncing by adding them to the
ignore
array in yoursftp.json
file.Avoid storing your password in plain text by using SSH keys for authentication. You can specify the path to your private key in the
sftp.json
file.
Conclusion
Using the SFTP extension in VS Code makes it easier to manage and edit your domain files. With these simple steps, you can connect to your server, make changes directly from your favourite code editor, and work more efficiently. Whether you're maintaining a website or managing multiple domains, this method provides a smooth and effective way to handle remote files.
Before, managing and editing files on my server was such a hassle. I’d have to jump between different tools just to make a simple change, and it was slowing me down. But now, with just a few quick steps, I can connect directly to my server right from VS Code. I make my edits, hit save, and the changes are instantly live on the site. It's like everything's just there, in one place, and it’s made such a difference in how I work.
Subscribe to my newsletter
Read articles from Victor Pianwi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Victor Pianwi
Victor Pianwi
I am a passionate full-stack developer with two years of hands-on experience crafting and maintaining dynamic and responsive solutions, with expertise in web, app and smart contract development, a technical writer and a community manager focused on empowering the community and individuals. I write well-detailed technical articles on self-growth, development(website, application, decentralized application and smart contract) and blockchain.