Sublime Text 2 - Open CMD prompt at current or pro

2020-02-16 06:29发布

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

{ "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.

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

5条回答
兄弟一词,经得起流年.
2楼-- · 2020-02-16 06:36
  1. Click the menu preference > Browser Packages in Sublime Text 2.
  2. Create a folder Cmd in the directory opened in step 1.
  3. Create a python file named cmd.py with the following code in the Cmd folder created in step 2.
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-menu with the following code in the Cmd folder created in step 2.
[
     { "command": "cmd" }
]
  1. Restart Sublime Text.

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

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-16 06:37

A non-python method.

  1. Go to Menu > Preferences > Browser Packages.
  2. Open the user directory.
  3. Create a new file cmdRunFromDIR.sublime-build and 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 START 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 an ERROR 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.
    
  5. For the keyboard shortcut go to Menu > Preferences > Key Bindings and 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+B will run the command also.


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

Build System - 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

How to Add a Shortcut for Any Command in Sublime Text

Build Systems – Configuration

查看更多
▲ chillily
4楼-- · 2020-02-16 06:38

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:

  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)
    
查看更多
家丑人穷心不美
5楼-- · 2020-02-16 06:45

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

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
查看更多
forever°为你锁心
6楼-- · 2020-02-16 06:57

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

查看更多
登录 后发表回答