How to Remote Edit Files on Raspberry Pi Using VS Code
Set up an remote file editing on your Raspberry Pi
If you have a headless (no screen, no keyboard, no mouse) Raspberry Pi, you can use an SSH session and a command line text editor like Nano to edit your code files. However, using a real code editor like VS Code always works better. A good solution is to use rmate and a plugin in VS Code to open remote files in VS code and edit them using your preferred setup and plugins.
Install rmate on your Raspberry Pi
First step is to install (rmate)[https://github.com/aurora/rmate]. Follow the instructions in the read me there, it is a quick two line install.
Install and set up “Remote VSCode” in VS Code
To use rmate on the Raspberry Pi from another machine, you need the “Remote VSCode” extension in VS Code (id: rafaelmaiolla.remote-vscode). Install it, and then set it up. Open the user settings for the installed extension and set the port to use to 52698
. You can also enable the extension to always start on start up if you like.
Using the rmate setup
On the device that you want to use VS Code on, you need to set up a tunnel to the Raspberry Pi. I like to use the VS Code terminal for this, as you will also be using this session to open up files to edit.
ssh -R 52698:localhost:52698 pi@<ip address>
This forwards port 52698 from your Raspberry Pi to port 52698 on your local machine, the port we just set up in the “Remote VS Code” settings.
In the tunnel session you can now open the file you want to edit using rmate:
rmate <file name>
This should open a new tab in VS Code with the file. You can now edit this file directly in VS Code, changes will be saved to the Raspberry Pi.