flyeris
flyeris

Reputation: 129

Sublime Text 3 - ?send commands to sublime console

I would like to extend Sublime functionality which would allow me to send text from any application to active Sublime Text window. I realized it is possible to add text from Sublime Console with the following command:

view.run_command("insert", {"characters": "Hello World!"})

Now I am wondering if there is any built-in solution to execute sublime console commands from external applications, ex. command-line interface to accept external commands? Else, I am considering it should be possible to set-up Python to run Socket server to listen for commands and accept input this way?

Any ideas?

Thanks!

Upvotes: 3

Views: 702

Answers (1)

OdatNurd
OdatNurd

Reputation: 22791

The subl command that ships with Sublime allows you to execute arbitrary commands from the command line:

tmartin:dart:~> subl --help
Sublime Text build 3211

Usage: subl [arguments] [files]         Edit the given files
   or: subl [arguments] [directories]   Open the given directories

Arguments:
  --project <project>: Load the given project
  --command <command>: Run the given command
  -n or --new-window:  Open a new window
  -a or --add:         Add folders to the current window
  -w or --wait:        Wait for the files to be closed before returning
  -b or --background:  Don't activate the application
  -h or --help:        Show help (this message) and exit
  -v or --version:     Show version and exit

Filenames may be given a :line or :line:column suffix to open at a specific
location.

The --command argument expects you to provide the command and any arguments required all in a single argument; for example:

tmartin:dart:~> subl --command "insert {\"characters\": \"Hello, World\"}"

This will work for any command, and just like the Sublime console, TextCommand commands target the current view and WindowCommand commands target the current window.

The subl command is a helper that gives it's command line arguments to the running instance of Sublime and terminates; in the case that Sublime is not already running, it starts Sublime first and then passes the arguments along.

In the case that Sublime isn't already running, the --command argument won't work because subl provides the argument as soon as Sublime starts, but it takes the plugin host some time to become available to commands (in Sublime you see this happening as a plugins loaded message in the console).

So, it's important that you either ensure Sublime is running before you do it, or you launch Sublime first, wait a second or so, and then provide the command.

Upvotes: 5

Related Questions