Presenting with VS Code and Running Commands on Remote Hosts

Page content

In this blog post, I’m going to show you how to use VS Code and Azure Data Studio for presentations where you need to execute local code on remote systems over SSH.

The Challenge

When using tools like VS Code and PowerShell ISE these tools give you the ability to highlight code and execute that highlighted code. This generally means the highlighted code you want to execute will execute against the local machine. When presenting, this technique is very useful and impactful for your audience because both the code being executed and its output will be on the screen at the same time as you step through your demos. I do a lot of presenting on topics like Linux, SQL Server on Linux, Kubernetes, and OpenSSH when I’m logged into remote hosts over SSH. I’d like to have the same functionality, so I can highlight the code in VS Code locally and execute that code on a remote host over SSH. Let’s explore how I do that.

The Solution

You can create a custom keyboard shortcut in VS Code (And Azure Data Studio too) that gives you this functionality. Highlight code, press a button and execute that code in the active terminal, which just so happens to be SSH’d into a remote host.

Head over to Preferences->Keyboard Shortcuts (Picture 1) and in there you’ll find a shortcut called “Terminal: Run Selected Text In Active Terminal” (Picture 2). This is exactly what I want. Now, when I’m presenting…I can highlight the code…and what I highlighted gets copied into the terminal below and executed on whatever system is active in the terminal below. This could be either my local computer or a remote system over SSH.


Picture 1: Preferences -> Keyboard Shortcuts. This is where you’ll find the Shortcut.


Picture 2:  The Run Selected Text in Active Terminal is bound to keys SHIFT+F8

The Output

So, let’s see this in action. In Picture 3, you can see I have VS Code open, the first thing I do is SSH into a remote host, in this case c1-master1 which is a Master Node in a Kubernetes Cluster. Once Logged in, I highlight the command “kubectl get nodes” then I hit SHIFT+F8 which copies my selected text into the active terminal, which is the SSH session on the remote host, and executes the code. This makes me so happy. Being able to do this when presenting provides a lot of value to the attendee/viewer so they can see both the code and its output on the screen at the same time.  Another quick tip, VS Code and Azure Data Studio have excellent font scaling, crank that font up…your attendees/viewers will be happier with you.


Picture 3:  Here you can see the shortcut in action in an SSH session to a Kubernetes Cluster Master.