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

后端 未结 5 846
庸人自扰
庸人自扰 2020-12-22 23:46

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         


        
相关标签:
5条回答
  • 2020-12-23 00:23

    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

    0 讨论(0)
  • 2020-12-23 00:30

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

    • Shell Turtlestein package

    But I ended up using the

    • Sublime Terminal package

    for the following reasons:

    • Shell Turtulestein's main purpose is another
    • Sublime Terminal lets me use iTerm instead of built in terminal
    0 讨论(0)
  • 2020-12-23 00:35
    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.

    0 讨论(0)
  • 2020-12-23 00:36

    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.

    0 讨论(0)
  • 2020-12-23 00:39

    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)
      
    0 讨论(0)
提交回复
热议问题