How to run a lua file in cmd from Sublime Text 3?

独自空忆成欢 提交于 2019-12-08 03:06:27
r-stein

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!