In this article, I am sharing step by steps procedure open command line prompt from Sublime editor.
Before starting this tutorial, let me tell you, that Sublime is one of the best editors for programming. I personally use it.
It supports and highlights syntax for so many programming languages. If you dabble in many programming languages, it is not a good choice to keep one editor (IDE) specific for each programming language.
Using a single editor makes your job easy. So the sublime is the best choice for you.
If you are not using it and are serious about coding, I would recommend you to download and install Sublime. Get your own experience before I explain you further.
Now moving to our tutorial guide.
Sublime is just an editor that highlights coding syntax.
After writing any programming, you save it. Then, you have to compile and run it. Unfortunately, Sublime does not have the intelligence to compile and run you’re any program.
Whether if it is Python or C/C++ programming, you need to the compile and run it through the command prompt (cmd
).
After writing a program, you can not open the command prompt from the Sublime editor.
What do you do?
“Minimize the Sublime editor and open command prompt from Windows application stray”.
then…
“Change the current directory of the command prompt where the program is saved. “
Two things bothered me much….
Opening command prompt explicitly. And the changing current directory of the command prompt for every program I run.
So, here I am giving the solution to my both problems by which you can open a command prompt from the Sublime editor with a single click.
Follow the procedure…
cmd.py
. Write the below code and save the file.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)
This is Python coding. It is ok even if you don’t understand this code. This code is tested and verified.
Context.sublime-menu
. Copy and paste the lines given below into the file and save it.[ { "command": "cmd" } ]
That is all. Your job is done.
Verify:
Now, just test it.
Tip: If you want to excel in programming, get rid of using the mouse. And try to get the things done from keyboard shortcuts in the jiffy.
Rather than Right-click every time to open a command prompt, I prefer to use the keyboard shortcut.
In the Sublime editor, click on Preferences and select Key Bindings – User.
If you want to open a command line prompt by command (by ‘cmd’ for example), Add the following context data into the Default (Windows).sublime-keymap file. :
{ "keys": ["c", "m", "d"], "command": "cmd"}
I am habitat opening command prompt using commandcmd
through W+Run
. So I prefer using the same shortcode for Sublime. You can set any keyboard shortcuts you feel comfortable with.
It makes your programming faster. And who doesn’t like that?
Sublime is a generic IDE that supports many languages. If you are a Python programmer, you may like to use PyCharm IDE. It is built specifically for Python project development. Check out our complete PyCharm installation and configuration tutorial.
This is all from this tutorial to open command line prompt from Sublime. If you face any challenges in configuring the Sublime editor, write your query in the comment.
Happy Programming!
I did not think of this earlier. I was used to open command prompt explicitly every time running program. Thanks for these tips. It is really time-saving for me.
Welcome, Abram! I am glad as you find it useful for your coding practice.
Happy Coding!
Hi!
I know this is an old post, but are you able to tell me how to alter the python code in the cmd.py file to run the user’s open file?
Like appending the actual filename to the path somehow?
Thanks! This package is great!
Hi Taylor,
I have changed the cmd.py as per my need. After changing to the current directory (where the file is present) I am running “python filename” to run the .py file.
This is really interesting.
Thanks Kamlesh for sharing this trick. This looks very useful.
Thank You SO MUCH!
You should create a Youtube Video for this.
Thank you for your suggestion, Bibek.
I’m already planning to launch our YouTube channel.
How do I get this to work? I see the cmd in right click menu, but clicking it does nothing.
Looks like there is something wrong in cmd.py. Can you share the code from cmd.py file?
Could not make it work, the cmd does appear in the menu but it is greyed out.
I used the same code you provided in this page.
Exact same code works for me. May I know which OS you are using?
Just type your folder name CMD in uppercase…
this problem is also faced by me but now after doing this it works…
I hope it works.
thnx.