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
Sublime Text 2 - Open CMD prompt at current or project directory (Windows)
提问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
- Click the menu
preference
>Browser Packages
in Sublime Text 2. - Create a folder
Cmd
in the directory opened in step 1. - Create a python file named
cmd.py
with the following code in theCmd
folder created in step 2.
- 在 Sublime Text 2 中单击菜单
preference
>Browser Packages
。 Cmd
在步骤 1 中打开的目录中创建一个文件夹。- 在步骤 2 中创建
cmd.py
的Cmd
文件夹中创建一个以以下代码命名的 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)
- Create a file named
Context.sublime-menu
with the following code in theCmd
folder created in step 2.
- 在步骤 2 中创建
Context.sublime-menu
的Cmd
文件夹中创建一个使用以下代码命名的文件。
[
{ "command": "cmd" }
]
- Restart Sublime Text.
- 重新启动 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 更改为在根项目文件夹中打开命令提示符(如问题中所要求):
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)
在步骤 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 上。我也试过
- Shell Turtlesteinpackage
- 壳牌海龟包
But I ended up using the
但我最终使用了
- Sublime Terminalpackage
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方法。
- Go to
Menu
>Preferences
>Browser Packages
. - Open the
user
directory. - Create a new file
cmdRunFromDIR.sublime-build
and open in Sublime Text. 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
&
afterSTART
will then pass on the$file_path variable
which 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 anERROR
if 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.
For the keyboard shortcut go to
Menu
>Preferences
>Key Bindings
and paste the following code. This shortcut isCTRL+C
,D
.{ "keys": ["alt+c,alt+d"], "command": "build", "args": { "file": "{packages}/User/cmdRunFromDIR.sublime-build" } }
Add
,
at the end of this line if it's not the last line in that file.There's no requirement to restart Sublime Text.
You also access this via
Menu
>Tools
>Build System
>cmdRunFromDIR
.
Once this is doneCTRL+B
will run the command also.
- 转到
Menu
>Preferences
>Browser Packages
。 - 打开
user
目录。 - 创建一个新文件
cmdRunFromDIR.sublime-build
并在 Sublime Text 中打开。 粘贴以下...
{ "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.
对于键盘快捷键,请转到
Menu
>Preferences
>Key Bindings
并粘贴以下代码。这个快捷方式是CTRL+C
,D
。{ "keys": ["alt+c,alt+d"], "command": "build", "args": { "file": "{packages}/User/cmdRunFromDIR.sublime-build" } }
,
如果它不是该文件中的最后一行,则在该行的末尾添加。不需要重新启动 Sublime Text。
您也可以通过
Menu
>Tools
>Build System
>访问它cmdRunFromDIR
。
完成此操作后,CTRL+B
还将运行该命令。
Useful links:See 1st link below for how to run .bat
files directly from Sublime Text. Very little modification of the code above.
有用的链接:请参阅下面的第一个链接,了解如何.bat
直接从 Sublime Text运行文件。对上面的代码修改很少。
在 Sublime Text 3 中,如何像在 Sublime Text 2 中一样分别为“构建和运行”和“仅构建”设置快捷方式