Telling your VS Code windows apart with colour

This makes things pretty obvious…

I often have two copies of Visual Studio Code open on the same project. Both are on my desktop. One runs locally. The other is connected to a different platform through a Remote connection. I do this a lot when I'm writing code for a Raspberry Pi and it can be rather confusing. The two windows look exactly the same, and more than once I've typed into the wrong editor, or built and run my code on the wrong machine.

What I wanted was to make the editor pages a different colour in each window, so I can see at a glance which one I'm in. My first thought was to write an extension. Before I did, I checked whether one already existed. It turns out you don't need an extension at all. VS Code can do this with its own settings.

The trick: User settings and Remote settings

VS Code keeps more than one level of settings, and later levels override earlier ones:

  1. Default: built into VS Code.
  2. User: your local installation. Applies to every local window.
  3. Remote: stored on the remote machine. Applies to every window connected to that machine, whatever project is open.
  4. Workspace: the project's own .vscode/settings.json.

If you put one colour scheme in your User settings and a different one in the Remote settings, every window shows where it is running. You set it once per machine and it works for every project, with nothing added to the projects themselves.

Colouring the remote window

Open a remote window. The box in the bottom-left corner shows the connection, for example SSH: myserver. Press Ctrl+Shift+P and run Preferences: Open Remote Settings (JSON). This command only appears in a remote window.

Add a colour scheme like this:

{
    "workbench.colorCustomizations": {
        "editor.background": "#0d1b2a",
        "editor.foreground": "#e0e6ed"
    }
}

Save the file and the editor changes colour straight away. The settings are stored on the remote machine itself, so that machine shows the same colours whichever computer you connect from.

Colouring the local window (optional)

You only need this step if you want to change your local colours as well. Colouring the remote window is usually enough to tell them apart.

In a local window, run Preferences: Open User Settings (JSON) and add a workbench.colorCustomizations entry in the same way. If the file already has one, add your colours to it rather than creating a second entry, because only the last one counts.

Going further

editor.background only changes the editor page itself. The line-number gutter, the current-line highlight and the terminal keep their theme colours. These settings colour the rest of the window too:

"workbench.colorCustomizations": {
    "editor.background": "#0d1b2a",
    "editor.foreground": "#e0e6ed",
    "editorGutter.background": "#0d1b2a",
    "editor.lineHighlightBackground": "#1b2d42",
    "terminal.background": "#0d1b2a",
    "titleBar.activeBackground": "#1f4e79",
    "statusBar.background": "#1f4e79"
}

The title bar and status bar are the most useful, because you can see them even when no file is open.

Some other things worth knowing:

  • Each remote machine has its own settings, so you can give each one its own colour. Each WSL distribution counts as a separate remote too.
  • Dev containers lose their settings when rebuilt, because the settings are stored inside the container. To keep the colours, put them in the project's devcontainer.json under customizations.vscode.settings.
  • Project settings win. If a project's .vscode/settings.json sets workbench.colorCustomizations, it overrides your colours in both windows. Colour extensions write their settings there, so check that file if your colours don't appear.

What about extensions?

There are extensions that do something similar. Peacock is the best known. It has a peacock.remoteColor setting that gives remote windows a different colour, but it colours the title bar, activity bar and status bar rather than the editor page. Others, such as Color Stamp and Unique Window Colors, colour windows by project. That doesn't help here, because the same project would get the same colour on both sides.

For my purposes the built-in settings do the job, and there's nothing to install.

Speed up Visual Studio Code Remote Development

These are the two you need to disable.

I’m writing all the code for the red telephone project in JavaScript as a node application. This is because it makes it very easy to host a website on the phone. Also I need a lot of multi-threading and events stuff and JavaScript’s await and async stuff makes this so easy to write. Also, I like writing JavaScript.

I’m writing the code on the Raspberry Pi W that is powering the phone. This is a really useful thing to be able to do. I can set breakpoints which are triggered when hardware events occur. Then I can step through the code. I’m using Visual Studio Code, but the environment is running on my PC and I’m using the amazing Remote Development feature to develop on the Pi. But it is not without its problems. When I started doing this, things would work fine for a while and then suddenly the Raspberry Pi would lock up and progress would grind to a halt.

I did some searching and thanks to this wonderful post I discovered how to fix it. It turns out that some of the language plugins for Visual Studio Code consume humungous amounts of computer power. And when they wake up on the Raspberry Pi it is pretty much game over for development on the machine. If you search the extensions for “@builtin type” and then disable the two shown above you’ll find that things work swimmingly. You miss out on a few useful goodies, and you’ll have to re-enable these to get them back, but in the meantime at least you’ll be able to do useful work on a device costing less than a couple of burgers and fries.

Visual Studio Code on Raspberry Pi is awesome

Visual Studio Code Raspberry Pi.png

I’ve been using Visual Studio Code remote development on my Raspberry Pi devices for a while, but what I’d really like to do is just run Visual Studio Code on the Raspberry Pi itself. And now I can.

sudo apt update 
sudo apt install code -y

All I had to do was open up a command prompt and type the two commands above. And then I had an extra option for programming and using all the lovely Visual Studio Code plugins.

This is wonderful, wonderful, news. I’ve installed the Platform IO plugin and I now have a completely self-contained system for development on the Pi. I’m using the Pi 400 device which has a fair amount of processing power. It’s not as fast as my desktop - but then it cost a tiny fraction of the price. And it does get there in the end. Such good news.

Writing MicroPython using Visual Studio Code and Pymakr

I’m starting to really, really, like MicroPython. I particularly like the way that you can use the REPL command prompt to test out code before dropping it into your programs. I’ve been using an editor called Thonny which is nice enough but of course what I really want is to be able to use Visual Studio Code. It turns out that there’s a plugin for this. It’s called Pymakr. It lets you transfer Python files between your PC and your MicroPython device and provides a REPL prompt too. If you want to use it you have to install Node.js first. I did this and then found that it didn’t work. My MicroPython board was not detected.

I was using one of my Doit boards which usually works fine. (I’ve just bought another one….) It turns out that Pymakr maintains a list of USB devices that it is willing to connect with. You need to make sure that your device is on the list in the configuration file Pymakr.json. Use the command Pymakr>Global Setting to open this file and then add your device manufacture to the list, like I’ve done below.

Note that this is not the the manufacturer of your device, it is the manufacturer of the USB interface that the device uses to connect to the PC.

You can find the name that you need by opening Device Manager, right-clicking the com port where your board is connected and then getting the manufacturer name from the properties page:

I did this and everything started working. The Pymakr plugin has a nifty feature where it will copy all the Python files in your project into the device, or only copy the ones that you’ve changed. Very nifty.

If you’ve not played with MicroPython before I’ve written a tiny guide that you might find useful. You can find it here.

Visual Studio Code is growing on me

You learn something new every day. Today I learned that if you don’t enable the C++ extension in Visual Studio Code you can’t use any of the Arduino extension commands.

Visual Studio Code is really growing on me. It’s like a software Swiss Army knife. It wraps around your code development and there are some awesome plugins. Including an amazing one for working with MarkDown (Markdown All in One). And the GitHub integration is lovely useful.