问题
I want to run lua files in cmd (for a reason I can't use build system).
How can I do this?
回答1:
Since you just want to call a cmd command you can easily write your own plugin.
Just open your user directory and create a python file (e.g. run_lua.py
).
Or just go with Tools >> New Plugin.
This plugin runs the command lua $file
and afterwards pauses until the user pressed a key:
import subprocess
import sublime_plugin
class RunLuaCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
subprocess.Popen(["cmd", "/c", "lua", view.file_name(), "&", "pause"])
Add the key binding:
{
"keys": ["alt+b"],
"command": "run_lua",
},
For a more general approach:
import subprocess
import shlex
import sublime
import sublime_plugin
class RunCmdCommand(sublime_plugin.WindowCommand):
def run(self, command):
variables = self.window.extract_variables()
command_expanded = sublime.expand_variables(command, variables)
# run in cmd
command_arr = ["cmd", "/c"]
# run the command
command_arr.extend(shlex.split(command_expanded, posix=False))
# afterwards wait for a key
command_arr.extend(["&", "pause"])
# execute the command
subprocess.Popen(command_arr)
With the key binding:
{
"keys": ["alt+b"],
"command": "run_cmd",
"args": {
"command": "lua $file"
}
},
PS. You can also use cmd /k
instead of cmd /c
and omit the & pause
at the end to create a cmd, which will stay open after executing the command. With pause it will wait for a keypress and close.
Thanks to @EgorSkriptunoff for the hint.
回答2:
According to the features of the plugin I don't think you are able to do that:
Opens a terminal in the folder containing the currently edited file
Opens a terminal in the project folder containing the currently edited file
The plugin will only open the terminal to your project/file directory.
I think the parameters that the documentation highlights is about configuring your terminal and not to run any kind of commands.
The plugin makes your life easier anyway. You are able to open your command prompt/terminal in your working directory and then just run your command you need. After that you can use the command prompt's history (Arrow Up or Arrow Down)to navigate to your previous commands.
回答3:
You might consider the sublimeREPL plugin. It opens a command prompt as a new "code tab". I needed to add my lua install to my path. It opens an interactive shell and supports a variety of shells including Lua.
来源:https://stackoverflow.com/questions/35813778/how-to-run-a-lua-file-in-cmd-from-sublime-text-3