Reputation: 4979
I'm working with sublime text 2 build 2221, on Windows 8; python 2.7.
I want to pass the name of the file I'm currently working on in st2 to a command by calling a key binding like this:
{ "keys": ["ctrl+shift+2"],"command": "run_me", "args":{"cmd":"$1"} }
where "$1"
gets replaced with the name of the file in the current view, i.e. the file I'm looking at when hitting the keys. How would I go about this?
My script for run_me
looks like this:
class runMeCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
cmd_string = kwargs["cmd"]
os.system("start "+cmd_string)
I found the following references here and here which seem to talk about this but I couldn't get it to work.
Relevant quotes from the links:
Link 1:
...
"args": {
"contents": "console.log('=== HEARTBEAT $TM_FILENAME [$TM_LINE_NUMBER] ===');${0}"
...
--------------------------------------------------------------------------------------------
Link 2:
$TM_FILENAME Filename of the file being edited including extension.
Upvotes: 0
Views: 1888
Reputation: 4979
This seems to be the way to do it (HT @skuroda):
class runMeCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
try:
if "$file_name" in kwargs["cmd"]:
cur_view = self.window.active_view()
cmd_string=kwargs["cmd"].replace("$file_name",cur_view.file_name())
os.system("start "+cmd_string)
else:
os.system("start "+cmd_string)
except TypeError:
sublime.message_dialog("Something went wrong with the command given... \n\n\n"+traceback.format_exc())
And the key binding looks like this:
{ "keys": ["alt+shift+3"],"command": "run_me", "args":{"cmd":"echo $file_name"} }
If the runMe class will always only get one argument only there is no need to use **kwargs
:
def run(self, cmd):
try:
if "$file_name" in cmd:
cur_view = self.window.active_view()
cmd_string=cmd.replace("$file_name",cur_view.file_name())
os.system("start "+cmd_string)
else:
cmd_string=cmd
os.system("start "+cmd_string)
except TypeError:
sublime.message_dialog("Something went wrong with the command given... \n\n\n"+traceback.format_exc())
The key binding would be the same as above.
Upvotes: 1