35

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

Community
  • 1
  • 1
Simon Lang
  • 40,171
  • 9
  • 49
  • 58

5 Answers5

61
  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.

Community
  • 1
  • 1
TomCaps
  • 2,497
  • 3
  • 22
  • 25
  • this looks good thanks mate. I'll test it out on my windows machine this arvy and let you know how it goes – Simon Lang Aug 29 '12 at 23:38
  • I'd also love to be able to get this working. Where does this code snippet go? And captaincalm, did you get this to work? – Dale C. Anderson Sep 26 '12 at 17:10
  • sorry for delayed response. yes pretty sure this worked for me (not on windows anymore so can't test at the moment) – Simon Lang Oct 12 '12 at 02:27
  • where would I look to see if this process was successful? I'm new to sublime and am not sure where to look to verify this? right click context menu on what part of the ui? – Maslow Jan 29 '13 at 15:05
  • 1
    you should also add the imports for the two packages you are using: `import os, sublime_plugin` at the begining of the file. once done that, it works for me. thanks – javigzz May 21 '13 at 13:36
  • 1
    Thanks @TomCaps. The instructions are pretty detailed. If you right click the editor area in any file you should be able to see a _Cmd_ menu item. – Batandwa Jul 05 '13 at 14:56
  • Thanks, but it's not working for me. Getting [this error](http://pastebin.com/t8542UVE) in the log after ST restart. I have ST3 beta. – FredyC Sep 15 '13 at 07:43
  • Is it possible to get it as a window inside Sublime Text 2? So you don't have to open CMD and go outside ST2 to execute CMD-commands? – Kebman May 07 '14 at 15:34
  • 16
    In Sublime Text 3 following the above instructions places a `Cmd` item in the context menu, but it is always disabled. – Phrogz Oct 09 '14 at 15:11
  • 4
    @Phrogz the name of the package "cmd" should be in upper-case: "CMD" – caub Feb 03 '15 at 15:27
  • 2
    @Phrogz : I renamed the folder to "Cmd" with an upper C and if fixed it. – BenC May 19 '16 at 12:13
  • The terminal seems to blink once before persisting. I'm guessing it's because python uses os.system to run the terminal to run the terminal then closes the first one. Is there a way to just keep one open from the get-go? – Unknow0059 Sep 06 '20 at 08:24
18

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.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Cruinh
  • 3,611
  • 4
  • 38
  • 45
4

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)
    
Kevin Blake
  • 434
  • 2
  • 5
4

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
duongel
  • 493
  • 5
  • 7
1

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

Ste
  • 1,729
  • 1
  • 17
  • 27