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
- 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 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)
- Create a file named
Context.sublime-menu
with the following code in the Cmd
folder created in step 2.
[
{ "command": "cmd" }
]
- Restart Sublime Text.
Now you can open the Cmd prompt at the current directory in the right-click context menu.
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.
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:
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)
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
for the following reasons:
- Shell Turtulestein's main purpose is another
- Sublime Terminal lets me use iTerm instead of built in terminal
A non-python method.
- 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 &
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.
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" } }
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 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