How to Open Command Line Prompt From Sublime Editor in Windows?

How to Open Command Line Prompt From Sublime Editor in Windows?

How to Open Command Line Prompt From Sublime Editor in Windows?

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.

Why should you use the Sublime editor, too?

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.

How to Open Command Line Prompt From Sublime Editor?

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.

Step by step procedure:

Follow the procedure…

  • Open Sublime editor.
  • Click on the Preferences and then select Browser Packages…
  • It will open a new window. Create a new folder CMD (all uppercase letters).
  • Create a file 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.

  • Create another file with a name 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.

  • Open any program file in the sublime editor and Right-click (context menu).
  • You can see the Cmd menu in the Context menu options.
  • Click on it.
  • The command prompt will be opened through which you can compile and run your program.

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.

Keyboard shortcut to Open Command Prompt:

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!

12 Comments

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

  2. 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!

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

      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]
              file = path.pop()
              current_directory="\\".join(path)
              command= "cd "+current_directory+" & "+current_driver+" & start cmd /k python " + file
              os.system(command)
      
  3. 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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *