Sublime Forum

Variables in command palette (.sublime-commands)

#1

Hi out there,

I hope I will not annoy anyone, as I feel this must be stupid in some way: I feel to not be able to use substitutable variables in sublime-commands file for the command palette.

Example:


	{
		"caption": "Markdown: Preview",
		"command": "exec",
		"args": { "cmd": "multimarkdown-preview", "$file"] }
	}
]

Here, multimarkdown-preview is a shell script invoking another command, and it receives $file, literally, instead of being substituted the current file path and name as it is with build systems.

Or is this on purpose? May be that’s just not possible to pass the current file path and name to a command launched from the command palette? I would be surprised, while still prefer to ask instead of erroneously guessing.

Is $packages the only available variable in sublime-commands files?

0 Likes

Issue with the forum: memory exhausted, cannot edit or post
Wish for review: a tiny plugin to invoke external programs
#2

Indeed, according to the unofficial (while very good) documentations, the command palette is handled at a low level in the application, which may explains why $packages in the only available variable:

Command Palette (docs.sublimetext.info)

As near to all the custom commands I used in any editor are implemented as external commands getting either a file name as an argument or reading text from standard-input, I needed to be able to get the same in Sublime Text. For people with the same need, here is how to do this in Sublime Text 3 ; here, for external command expecting the active file name as an argument, via a simple plug-in:

Plugin /…/Packages/User/external_command.py:

""" Run an external command on the current file.

Usage: run command `external_command` and giving it the executable name as the
`executable` argument. The external command with be invoked with the current
file path and name, as its first and single argument.

"""

import sublime_plugin

class ExternalCommandCommand(sublime_plugin.WindowCommand):

	def run(self, executable):
		variables = self.window.extract_variables()
		if "file" in variables:
			file = variables"file"]
			self.window.run_command("exec", {"cmd": [executable, file]})
		else:
			sublime.status_message("Error: no file")

	def description(self):
		return (
			"Run an external command with the current file path and name as "
			"a single argument.")

	def description(self):
		return (
			"Run an external command "
			"with the current file path "
			"and name as a single argument.")

Example /…/Packages/User/MyCommands.sublime-commands making use of it:


	{
		"caption": "Markdown: Preview",
		"command": "external_command",
		"args": { "executable": "multimarkdown-preview" }
	}
]

References used:

Now Ctrl+Shift+P the “Markdown preview” then Enter, works as expected.

This simple plug-in will allow me to integrate all of my external command I used with another editor, into Sublime Text 3.

0 Likes

#3

Although this is discussion is old, the problem may be there for some, so here is a more generic solution, that will allow you to use any Sublime variables:

import sublime_plugin

class ExternalCommandCommand(sublime_plugin.WindowCommand):
  def run(self, cmd):
    parsedCmd = []
    variables = self.window.extract_variables()

    for arg in cmd:
      if arg[0] == '$' and arg[1:] in variables:
        parsedCmd.append(variables[arg[1:]])
      else:
        parsedCmd.append(arg)

    self.window.run_command("exec", {"cmd": parsedCmd})

You can use like so:

  { 
    "caption": "Open cmder in current project", 
    "command": "external_command",
    "args" : { 
      "cmd" : [
        "C:\\work\\tools\\cmder\\cmder.exe",  
        "$project_path"
      ] 
    } 
  },
0 Likes