collapse

Welcome!



Welcome to Robot Rebels, an online robot maker community.

Register today to post your projects, ask questions, share knowledge and meet like-minded people from around the world.


The RobotRebel.org Community

Author Topic: Editing Raspberry Pi Code Remotely from Visual Studio Code  (Read 590 times)

Ladvien

  • Alabtu-ian Refugee
  • Member
  • *
  • Posts: 57
Editing Raspberry Pi Code Remotely from Visual Studio Code

I'm spoiled.  I love the pretty colors of modern text IDEs.  My favorite among them being Visual Studio Code.

Visual Studio Code

I know it'll engender a lot of bad rep with the old-timers, but I prefer the one on the right.



However, when working on a headless (no monitor) Raspberry Pi it felt like I was pretty much stuck with the nano. 

Until! I discovered Visual Studio Code's remote extension.


This allowed me to edit my Raspberry Pi files from within Visual Studio Code.  So, I get all the joys of writing code directly on my Raspberry Pi, but with all the bells-and-whistles of Visual Studio Code (VSC).

For the most part, setup is pretty straightforward.  But the Pi side can get tricky, so I'm going to walk us through the process.

1. Get Visual Studio Code
Download the version of VSC for your PC.  Note, you aren't running this from the Raspberry Pi--instead, you'll be running it from the PC and connecting it to the Raspberry Pi.

 Visual Studio Code Download

After it's downloaded and installed open it up.


Once open, click here


Ok, now search for the extension called

Code: [Select]
Remote VSCodeAnd hit the Install button.  Once it finishes hit the reload button.

The extension works by creating a server which listens for incoming calls from the Raspberry Pi.  Once we finished setting up the Raspberry Pi we will use a special command which sends the file on the Raspberry Pi to Visual Studio Code.  However, when it's all done, it'll look pretty seamless.

Back to setup.

In Visual Studio Code type F1 and type Preferences: Open Workspace Settings



Find the section labeled
Code: [Select]
remote.onStartup: falseWe need to change it to true by clicking on the pencil next to its name.  This sets the listening server to start every time you open Visual Studio Code.



Almost there.  Now to setup the Raspberry Pi.  We need to install a program on the Pi which will send a file of our choosing to Visual Studio Code to be edited.  RMate was my choice.

Start by SSH'ing into your Raspberry Pi.

Then type
Code: [Select]
sudo pacman -S ruby
cd ~
gem install rmate
PATH="$PATH:$(ruby -e 'print Gem.user_dir')/bin"
echo 'PATH="$PATH:$(ruby -e 'print Gem.user_dir')/bin"' >> /etc/profile
echo 'export GEM_HOME=$HOME/.gem' >> /etc/profile
The above commands install Ruby, moves to to the user's directory, uses the Ruby package manager to install rmate, then adds Ruby and it's Gems (packages) executables to the environment variables.  All of this is necessary to get Rmate working on Arch Linux.

Ok, let's test it.  Stop SSH'ing into your Pi by typing exit until it brings you back to your PC's prompt.  Now we are going to SSH into the Pi while listening for incoming files to be displayed in Visual Studio Code.

Open Visual Studio Code and open the integrated terminal (if it's not showing hit CTRL + `).

At the terminal type
Code: [Select]
ssh -R 52698:localhost:52698 alarm@192.168.1.xReplace the x with your Pi's ip address.

This should SSH into the Pi while listening for files.

At the pi command prompt, type
Code: [Select]
rmate test.js
This should open a new file called test.js in your Visual Studio Code.

Now you get all the goodness of the VSC IDE, such as syntax highlighting, linting, etc!

A few notes.  File permissions still apply, so if you want to be able to save a file the user you logged into on the Raspberry Pi and rmated the file must have write permission on the file.

However, if you do have write permissions, then the "File Save" function in the VSC editor will update the Raspberry Pi file with your modifications.  Booyah!


One last annoyance to address.  Whenever you want to use VSC to edit your file you have to log into the Pi using
Code: [Select]
ssh -R 52698:localhost:52698 alarm@192.168.1.xThis annoyed me a bit.  I could never remember all that.  Instead, I created a small bash script to help.

On my PC (for Mac and Linux, Windows, you're on your own) I created in my home user directory called
Code: [Select]
vsAnd added the following to the file.
Code: [Select]
echo $1
ssh -R 52698:localhost:52698 "$1"
Essentially, this script takes your Pi's login information and logs in to your Pi using the VSC Remote Extension listening.

To get it to work you've got to make the file executable
Code: [Select]
sudo +x chmod vsThen login in your Pi like this
Code: [Select]
./vs alarm@192.168.1.xHope you enjoy.

Oh, and for you web-devs, this also works for remote servers.  Just replace the Pi with the server.
« Last Edit: April 14, 2018, 05:24:57 AM by Ladvien »

 

* Search


* Recent Topics

Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]


API Directory by Gareth
[December 06, 2018, 03:34:16 PM]


The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 02:49:52 PM]


Email notifications by CraigCox
[November 28, 2018, 10:26:06 PM]


And another one. by CraigCox
[November 28, 2018, 10:17:54 PM]


MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]


This kids a freaking genius by ossipee
[November 21, 2018, 04:55:40 PM]


Time of Flight >>> Rounded Corners !!! by Gareth
[November 15, 2018, 03:21:51 PM]


Double_ing up the "Time of Flight" sensors by Gareth
[November 13, 2018, 04:05:02 AM]


Point cloud anyone !!! maybe 3D scanner by maelh
[November 11, 2018, 05:38:33 AM]


removable magnetic print surface by Gareth
[November 09, 2018, 10:53:45 AM]


Robot Cop by Valeriex
[November 07, 2018, 05:48:20 AM]


it's lukeyes by Valeriex
[November 07, 2018, 05:47:16 AM]


TOF gets a local DEBUG screen by Gareth
[November 07, 2018, 03:47:29 AM]


BlueTooth elves choose "BlueTeeth" by Gareth
[November 07, 2018, 03:40:19 AM]

* Recent Posts

Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]


API Directory by Gareth
[December 06, 2018, 03:34:16 PM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 02:49:52 PM]


Re: The Original Prusa I3 Mk3 by Bajdi
[November 30, 2018, 02:26:48 PM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 04:38:53 AM]


Re: Email notifications by CraigCox
[November 28, 2018, 10:26:06 PM]


Re: And another one. by CraigCox
[November 28, 2018, 10:17:54 PM]


Re: MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]


Re: MKS Gen L 1.0 by Prokktor
[November 27, 2018, 08:35:38 AM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 26, 2018, 02:42:26 PM]


Re: The Original Prusa I3 Mk3 by jinx
[November 26, 2018, 02:24:05 PM]


Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 07:54:21 AM]


Re: The Original Prusa I3 Mk3 by tinhead
[November 25, 2018, 06:11:22 AM]


Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 04:40:31 AM]


The Original Prusa I3 Mk3 by tinhead
[November 24, 2018, 12:02:51 PM]