Sublime Text 2 - 在当前或项目目录中打开 CMD 提示 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12103028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:49:55  来源:igfitidea点击:

Sublime Text 2 - Open CMD prompt at current or project directory (Windows)

windowscmdsublimetext2

提问by captainclam

I have found the following Sublime command to be really useful since it opens an explorer window at the location of the current file:

我发现以下 Sublime 命令非常有用,因为它会在当前文件的位置打开一个资源管理器窗口:

{ "keys": ["ctrl+alt+o"], "command": "open_dir", "args": {"dir": "$file_path", "file": "$file_name"} },

What I would love is a similar command that will open a cmd window instead. Ideally at the root project folder, but current file directory would also be fine.

我想要的是一个类似的命令,它将打开一个 cmd 窗口。理想情况下在根项目文件夹中,但当前文件目录也可以。

Have read the following question, but can't quite figure out how to use this in a sublime plugin/command: BAT file to open CMD in current directory

已阅读以下问题,但无法弄清楚如何在 sublime 插件/命令中使用它: BAT 文件以在当前目录中打开 CMD

回答by TomCaps

  1. Click the menu preference> Browser Packagesin Sublime Text 2.
  2. Create a folder Cmdin the directory opened in step 1.
  3. Create a python file named cmd.pywith the following code in the Cmdfolder created in step 2.
  1. 在 Sublime Text 2 中单击菜单preference> Browser Packages
  2. Cmd在步骤 1 中打开的目录中创建一个文件夹。
  3. 在步骤 2 中创建cmd.pyCmd文件夹中创建一个以以下代码命名的 python 文件。
import os, sublime_plugin
class CmdCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        file_name=self.view.file_name()
        path=file_name.split("\")
        current_driver=path[0]
        path.pop()
        current_directory="\".join(path)
        command= "cd "+current_directory+" & "+current_driver+" & start cmd"
        os.system(command)
  1. Create a file named Context.sublime-menuwith the following code in the Cmdfolder created in step 2.
  1. 在步骤 2 中创建Context.sublime-menuCmd文件夹中创建一个使用以下代码命名的文件。
[
     { "command": "cmd" }
]
  1. Restart Sublime Text.
  1. 重新启动 Sublime Text。

Now you can open the Cmd prompt at the current directory in the right-click context menu.

现在您可以在右键单击上下文菜单中的当前目录中打开 Cmd 提示。

回答by Cruinh

The Shell Turtlestein packagealso has a command for this.
With that package installed, you can type CTRL+SHIFT+ALT+C
(or CMD+SHIFT+ALT+Con mac) to open cmd/terminal in the current file's folder.

壳牌Turtlestein包还具有用于此的命令。
安装该软件包后,您可以键入CTRL+ SHIFT+ ALT+ C
(或mac 上的CMD+ SHIFT+ ALT+ C)以在当前文件的文件夹中打开 cmd/terminal。

回答by Kevin Blake

Just to expand on TomCaps answer, you can also open the command prompt at the root project folder (as was requested in the question), by changing step 3 to:

只是为了扩展 TomCaps 答案,您还可以通过将步骤 3 更改为在根项目文件夹中打开命令提示符(如问题中所要求):

  1. Create a python file named cmd.py with the following code in the cmd folder created in step 2.

    import os, sublime, sublime_plugin
    class CmdCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            file_name=sublime.active_window().project_file_name()
            path=file_name.split("\")
            current_driver=path[0]
            path.pop()
            current_directory="\".join(path)
            command= "cd "+current_directory+" & "+current_driver+" & start cmd"
            os.system(command)
    
  1. 在步骤 2 中创建的 cmd 文件夹中使用以下代码创建一个名为 cmd.py 的 python 文件。

    import os, sublime, sublime_plugin
    class CmdCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            file_name=sublime.active_window().project_file_name()
            path=file_name.split("\")
            current_driver=path[0]
            path.pop()
            current_directory="\".join(path)
            command= "cd "+current_directory+" & "+current_driver+" & start cmd"
            os.system(command)
    

回答by duongel

I was looking for the same thing, except on Mac OS X. I also tried the

我一直在寻找同样的东西,除了在 Mac OS X 上。我也试过

But I ended up using the

但我最终使用了

for the following reasons:

出于以下原因:

  • Shell Turtulestein's main purpose is another
  • Sublime Terminal lets me use iTerm instead of built in terminal
  • Shell Turtulestein 的主要目的是另一个
  • Sublime Terminal 让我使用 iTerm 而不是内置终端

回答by Ste

A non-python method.

一种非python方法。

  1. Go to Menu> Preferences> Browser Packages.
  2. Open the userdirectory.
  3. Create a new file cmdRunFromDIR.sublime-buildand open in Sublime Text.
  4. Paste the following...

    {
    "cmd": ["C:\\Windows\System32\cmd.exe", "/C START &"],
    "working_dir": "$file_path"
    }
    

    The above will open the current folder but if you want the project directory then there's a whole host of different methods here. Note:That the &after STARTwill then pass on the $file_path variablewhich can be changed to any of those below. I couldn't see any documentation for this. It was just trial and error on my behalf and makes sense if you think about it. So, if you try to pass "cmd": ["C:\\\\Windows\\System32\\cmd.exe", "/C START & $file_path"]to will get an ERRORif the path has any whitespaces in it.

    $file_path  The directory of the current file, e.g., C:\Files.
    $file   The full path to the current file, e.g., C:\Files\Chapter1.txt.
    $file_name  The name portion of the current file, e.g., Chapter1.txt.
    $file_extension The extension portion of the current file, e.g., txt.
    $file_base_name The name-only portion of the current file, e.g., Document.
    $folder The path to the first folder opened in the current project.
    $project    The full path to the current project file.
    $project_path   The directory of the current project file.
    $project_name   The name portion of the current project file.
    $project_extension  The extension portion of the current project file.
    $project_base_name  The name-only portion of the current project file.
    $packages   The full path to the Packages folder.
    
  5. For the keyboard shortcut go to Menu> Preferences> Key Bindingsand paste the following code. This shortcut is CTRL+C,D.

        { "keys": ["alt+c,alt+d"], "command": "build", "args": { "file": "{packages}/User/cmdRunFromDIR.sublime-build" } }
    
  6. Add ,at the end of this line if it's not the last line in that file.

  7. There's no requirement to restart Sublime Text.

  8. You also access this via Menu> Tools> Build System> cmdRunFromDIR.
    Once this is done CTRL+Bwill run the command also.

  1. 转到Menu> Preferences> Browser Packages
  2. 打开user目录。
  3. 创建一个新文件cmdRunFromDIR.sublime-build并在 Sublime Text 中打开。
  4. 粘贴以下...

    {
    "cmd": ["C:\\Windows\System32\cmd.exe", "/C START &"],
    "working_dir": "$file_path"
    }
    

    以上将打开当前文件夹,但如果您想要项目目录,那么这里有很多不同的方法。 注:&以后START会再通上$file_path variable可改为任何与下面的。我看不到任何文档。这只是代表我的反复试验,如果你考虑一下是有道理的。因此,如果您尝试传递"cmd": ["C:\\\\Windows\\System32\\cmd.exe", "/C START & $file_path"]ERROR路径中是否有任何空格,则会得到一个。

    $file_path  The directory of the current file, e.g., C:\Files.
    $file   The full path to the current file, e.g., C:\Files\Chapter1.txt.
    $file_name  The name portion of the current file, e.g., Chapter1.txt.
    $file_extension The extension portion of the current file, e.g., txt.
    $file_base_name The name-only portion of the current file, e.g., Document.
    $folder The path to the first folder opened in the current project.
    $project    The full path to the current project file.
    $project_path   The directory of the current project file.
    $project_name   The name portion of the current project file.
    $project_extension  The extension portion of the current project file.
    $project_base_name  The name-only portion of the current project file.
    $packages   The full path to the Packages folder.
    
  5. 对于键盘快捷键,请转到Menu> Preferences>Key Bindings并粘贴以下代码。这个快捷方式是CTRL+CD

        { "keys": ["alt+c,alt+d"], "command": "build", "args": { "file": "{packages}/User/cmdRunFromDIR.sublime-build" } }
    
  6. ,如果它不是该文件中的最后一行,则在该行的末尾添加。

  7. 不需要重新启动 Sublime Text。

  8. 您也可以通过Menu> Tools> Build System>访问它cmdRunFromDIR
    完成此操作后,CTRL+B还将运行该命令。



Useful links:See 1st link below for how to run .batfiles directly from Sublime Text. Very little modification of the code above.

有用的链接:请参阅下面的第一个链接,了解如何.bat直接从 Sublime Text运行文件。对上面的代码修改很少。

Build System - Sublime Text 3

构建系统 - Sublime Text 3

In Sublime Text 3, how to have shortcuts for “Build and Run” and “Build only” separately like it was in Sublime Text 2

在 Sublime Text 3 中,如何像在 Sublime Text 2 中一样分别为“构建和运行”和“仅构建”设置快捷方式

How to Add a Shortcut for Any Command in Sublime Text

如何为 Sublime Text 中的任何命令添加快捷方式

Build Systems – Configuration

构建系统 – 配置