This extension allows you to remotely control instances of Visual Studio Code by exposing a REST endpoint that you can use to invoke vscode commands. In the background it launches a HTTP server that listen on the localhost
interface for requests of commands to execute.
The demo below shows how we can open a terminal, run some commands in it, then close all terminals, open a file with the cursor in a specified location and finally start a debug session on the same file that is orchestrating it all!
DISCLAIMER: This extension was forked from Remote Control.
The main motivation behind it was that while Remote Control
uses websockets
, for my use case I can only rely on HTTP REST calls. In addition, some commands I need to use require non-primitive JavaScript types which are not handled except for a couple of cases in the original extension (e.g. the Uri
for vscode.open
command).
The extension has the following settings which you can use to configure it:
restRemoteControl.enable
: enable/disable this extensionrestRemoteControl.port
: set the port number on which the HTTP server will listenrestRemoteControl.fallbacks
: an array of port numbers to fallback to if the port is already in use.
When you install this extension, it will automatically try to start a HTTP server on port 37100
. This port can be changed on in the VSCode settings. When you are going to use multiple VSCode sessions at the same time, it is best to configure it on workspace level or use the restRemoteControl.fallbacks
setting to specify fallback ports when the previous one is already in use. VSCode terminals opened will have environment variable REMOTE_CONTROL_PORT
set with the port the server is currently listening to.
Once installed, you can execute vscode commands
by making HTTP requests. Here are few examples using curl
:
# Create a new terminal
curl -X POST http://localhost:37100 -d '{"command":"workbench.action.terminal.new"}'
# Run `pwd` in the currently active terminal
curl -X POST http://localhost:37100 -d '{"command":"custom.runInTerminal", "args": ["pwd"]}'
# Kill all terminals
curl -X POST http://localhost:37100 -d '{"command":"workbench.action.terminal.killAll"}'
All requests are expected to be in a JSON HTTP request body in the form:
{
"command": "<command-id>",
"args": ["<arg1>", "<arg2>", "...", "<argN>"]
}
Some VSCode commands expect VSCode's defined types such as Range, Uri, Position and Location. To accommodate for those, such arguments can be passed as a special types, see the example below which effectively invokes editor.action.goToLocations
with Uri
, Position
and an array of Location
s:
{
"command": "editor.action.goToLocations",
"args": [
{
"__type__": "Uri",
"args": ["/path/to/file.py"]
},
{
"__type__": "Position",
"args": [4, 0]
},
[
{
"__type__": "Location",
"args": [
{
"__type__": "Uri",
"args": ["/path/to/file.py"]
},
{
"__type__": "Position",
"args": [11, 5]
}
]
}
]
]
}
As the extension progresses, I plan to add more special commands (i.e. commands that require some use of the VSCode API). For now, we have defined the following commands:
custom.goToFileLineCharacter
: allows you to nagivate to a specific position in a file by passing the file path, line and column number as argumentscustom.startDebugSession
: allows you to invokevscode.debug.startDebugging()
API by passing the workspace folder and a name or definition of a debug configurationcustom.runInTerminal
: allows you to invoke commands the currently active integrated terminalcustom.showQuickPick
: show quick pick dialog to collect selection from the usercustom.showInformationMessage
,custom.showWarningMessage
andcustom.showErrorMessage
: show message dialogs to the user and let them click on a buttoncustom.listInstalledExtensions
: get the list of installed extension IDs
- Add the ability to set a breakpoint at the specified file/line combination
To get the command ID, open the Command Palette
and type Show all commands
. This will give you a list with all the available commands. VScode's built-in commands can be found here.
Behind each command, there is a gear button. When you click on it, it brings you to the shortcut configuration. Where you can right-click on the command and copy its ID.
Please submit your feedback/issues/ideas by creating an issue in the project repository: issue list.