How to run programs in an external console with Sublime Text in Windows system? How to run programs in an external console with Sublime Text in Windows system? windows windows

How to run programs in an external console with Sublime Text in Windows system?


Try with this:

{    "cmd": ["C:\\Dev-Cpp\\bin\\mingw32-g++.exe", "-static", "-Wall", "-time", "$file", "-o", "$file_base_name.exe", "&&", "start", "$file_base_name"],    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",    "working_dir": "${project_path:${folder}}",    "selector": "source.c",    "shell": true,    "encoding": "latin1"}

The trick is to execute the generated file using the "start" command, which forces the file to open in a new cmd window, and to put everything inside the first cmd definition (using the && in order to execute the two commands), because if you define more than one cmd array, they overwrite.


@FacundoJ: On Mac, you can use Terminal to run the app externally. In C++.sublime-build, change the cmd so that instead of just running the compiled output, it launches it in Terminal.

Change from:

"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]

To:

"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]


The accepted answer allows you to execute the program in Windows CMD and accept input. But once the program execution is over, the cmd window will be closed immediately before you can see clearly the output.

I think you should try the following solution, which keeps the CMD window open after executing the program, so that you can examine your result.

{    "shell_cmd": "g++ -Wall -std=c++11 \"${file}\" -o \"${file_path}/${file_base_name}.exe\"",    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",    "working_dir": "${file_path}",    "selector": "source.c, source.c++",    "variants":    [   // run in the sublime text console        {            "name": "Run",            "shell_cmd":"\"${file_path}/${file_base_name}\""        },        // run in the Windows cmd        {            "name": "Run in cmd",             // the following two settings are all valid, choose one you prefer            "shell_cmd":   "start cmd /k  $file_base_name "            // "shell_cmd":   "start \"$file_base_name\" call $file_base_name"        }    ]}

First press Ctrl+B to compile the program, then press Ctrl+Shift+B and choose run in cmd option, which will run the program in system CMD instead of the Sublime Text console.

The above settings are tested on Windows 8.1, and should also work on Windows 7 and Windows 10 out of the box.