Editing Remote Files on a Raspberry Pi with Sublime Text 2/3
Lately, I've been doing a lot of work with a Raspberry Pi. Until recently, I would ssh into the Raspberry Pi, and use Vim on the pi to edit files on the pi. I really hate doing this, not because I don't like Vim, but because I love Sublime Text. I can't reasonably install Sublime Text on the Raspberry Pi, so I sought the next best thing: use my local instance of Sublime Text to edit the remote files on the Raspberry Pi. To do this, I had to setup a few things, but now that I'm done, it's hard to imagine doing it any other way.
Steps We'll Perform
rsubfor Sublime Text on your local computer
~/.ssh/configon your local computer
rmateshell script on the Raspberry Pi
Package Control for Sublime Text
The first you thing you'll need to do is to add the rsub package to Sublime Text. The best way to install the package for Sublime Text is to use the excellent Package Control Sublime Package Manager tool.
Installing the RSUB Package
Once you've installed Package Control, open Sublime Text and press
ctrl+shift-p on Windows) to bring up the package manager dialog in Sublime Text. The dialog will show a number of options and a textbox in which you can type. Type
install, and the visible list of options will be automatically filtered down to a subset of options, and you should see an option
Package Control: Install Package. Select this option, and the dialog content will refresh with a list of packages that you can install into Sublime Text. You will see a textbox in which you can type. Type
rsub in the textbox, which will filter the list, and automatically highlight the
rsub package name. Press
Enter (or click the
rsub option with you mouse) to install the package.
The next step is to modify the
~/.ssh/config file on your local machine. You need to do this so that the remote port on the Raspberry Pi will be forwarded to a local port on your local computer.
52698, is used by
rsubfor the secure communication (via ssh) between your local computer (Sublime Text) and the remote Raspberry Pi.
Go ahead and open the `~/.ssh/config file (there's no file extension) in your favorite text editor (Sublime, of course). I like to use the terminal to open the file:
If you don't have an alias configured for Sublime Text, it's no big deal. Simply navigate your file system from your user directory to locate the
.ssh folder, and open the
config file located in the
Once you have the
config file open, add the following lines to the bottom of the file:
Host raspberry_pi_ip_address_or_hostname.com RemoteForward 52698 127.0.0.1:52698
In the above example, replace
raspberry_pi_ip_address_or_hostname.com with the IP Address or hostname of your Raspberry Pi. Save and close the
Setting up the Raspberry Pi
We're almost done. You've made it this far. Don't give up.
Now you need to ssh into the Raspberry Pi (ssh your_username@your_raspberry_pi_address). Once you've ssh-ed into the pi, run the following in the terminal:
sudo wget -O /usr/local/bin/rsub
This command downloads the rmate repository from github into a newly created
rsub folder in the
/usr/local/bin/ folder on the Raspberry Pi.
Make the Script Executable
You can't do anything with the
rmate script you just downloaded until you make it executable. To do that, run the following command from your terminal while still ssh-ed into the Raspberry Pi:
sudo chmod +x /usr/local/bin/rsub
This command will make the
rmate file an executable file.
Ok, whew! Take a breath. Now that you've done all of the above, let's see it in action. While still ssh-ed into the Raspberry Pi, use the terminal to navigate to a directory that contains a text file on the Raspberry Pi. Once you're at the directory, type the following at the terminal command prompt:
Presto! The file should open in Sublime Text on your local computer. You can edit the file and save your changes, and the file will be updated on the Raspberry Pi! You no longer need to ssh into the Raspberry Pi to edit files on the pi. Shazam!
Here's me opening a file called
index.html on the Raspberry Pi, with Sublime Text on my local Macbook Air:
I hope this improves your workflow, and inspires you to look for other creative ways to make everyday tasks easier for you and others.
Curious to read another one of my posts? Check out this post on things to consider for designing modal windows.
Interested in learning more or joining the team? Let's talk.