1 Synchronize remote code using sftp
Open vscode, search for sftp in the extension, and click Install.
Press and hold the shortcut key shift+ctrl+p to open the command line at the top of the interface, enter sftp, and click the config option as shown in the figure below: a file named sftp.json will be automatically created
in the .vscode directory, and the content is as shown in the figure below:
According to your own situation, you can modify it according to the following content:
{
"name": "My Server",
"host": "远程ip地址",
"protocol": "sftp",
"port": 远程端口号,
"username": "用户名",
"password": "密码",
"remotePath": "远程存放代码的路径",
"ignore": [
"**/.vscode/**",
"**/.git/**",
"**/logs/**",
"**/__pycache__/**"
],
"watcher": {
"files": "*",
"autoUpload": true,
"autoDelete": true
},
"uploadOnSave": true,
"downloadOnOpen":false,
"useTempFile": false,
"openSsh": false
}
After completion, we can find that there is an SFTP icon with a cloud above the paper on the left column. At this point, we can right-click the mouse in the blank space of the resource manager to see Sync Local -> Remote
the option, click it, we can synchronize the code folder opened in the current vscode window to the remote server, and the synchronization path is filled in the above json configuration file.
When synchronizing, if we are careful, we can observe that the status bar in the lower left corner of vscode will keep beating (as shown in the figure below), showing which file in the local is being synchronized to the remote. Of course, this process was completed very quickly.
After the synchronization is complete, we can refresh and open the remote code path, and then we can see the synchronized code.
2 Use remote-ssh to run remote terminals and modify content
Similarly, search for remote-ssh on the extension interface and install it.
After the installation is complete, you can see an additional remote resource manager icon (a computer with two facing angle brackets) on the left menu bar. Click the icon, select remote in the upper right corner:
and then click the plus sign on the right side of the SSH bar, a bar will pop up at the top of vscode, just fill in according to the prompts, the content is, ssh 用户名@远程ip:端口号
among them, :端口号
non-essential options, if there are, you must fill in. After filling, press Enter to save by default.
After completion, you can see the newly added remote connection option in the remote window. You can right-click and choose to perform remote connection in the current window or a new window.
For example, if we choose to establish a connection in a new window, a new vscode window will pop up. First, we need to select the type of remote platform. The server is generally linux. Next, we will continue to ask us to enter a password, enter it and press Enter.
After successfully connecting, we will enter the remote terminal, and we can see that the green rectangle in the lower left corner shows the current connection status, which is the SSH remote server. All modifications in the current vscode are for remote. We can choose to create a new file to create a new file, or we can choose to open a remote file or folder.
The use of ssh-remote is not only to modify the remote code, I think a more important function is that in the remote environment, we can open the remote terminal in vscode, so that we can use the terminal to run the remote code.
3 used together
Based on these two plug-ins, we can realize the synchronization of local code and remote code, and add, delete and modify remote code or files. But whether we modify the code locally and then synchronize to the remote, or directly modify the remote code and then synchronize back, this needs to be clarified. Otherwise, if both sides have made different modifications, there may be conflicts. One of my usage strategies is to modify the code locally, synchronize the local code to the remote through sftp after completion, and then use the remote terminal of ssh-remote to run the code remotely. Then the code modified locally can also be synchronized to the cloud in time using git, which is more secure.
Pay attention when running remote code. It is possible that we cannot directly use python, pip, conda and other commands because the environment variables have not been configured. We need to specify the bin directory of conda and then execute it, that is:
/opt/conda/bin/python3
/opt/conda/bin/pip
/opt/conda/bin/conda
export PATH=$PATH:/usr/bin/
export PATH=$PATH:/opt/conda/bin/
For example, if you want to install numpy with pip, the command is:
/opt/conda/bin/pip install numpy
Reference:
vscode local and server remote synchronization code
vscode connects to the remote server (fool teaching)
VSCode uses ssh to remotely link to the server and reports an error Downloading VS Code Server failed