How to compile and run any (language) programme (with one click) from Sublime Text, allowing both input and output?
1 Answers
The following has been tested on Sublime Text 4 (build 4121) with GCC for C on Windows 7, but should be adaptable to other versions/compilers/languages/operating systems.
Sublime Text's build system is meant for building only, not for running. We can, however, run the programme with some cunning, but Sublime Text outputs everything to its own build panel, which is built for output only.
To take input from STDIN, we have to open a command (shell) window. If so, why not have everything (compiler messages, input, output) in there?
Open Sublime Text and go to Preferences→Browse Packages.... Go into the User folder and create two files as follows:-
Compile_and_run_C.sublime-build
/* This calls a Batch script with the source file as the argument. */ /* This will always execute in the directory where the source file is. */ { "shell_cmd": "start C:\\path\\to\\Compile_and_run_C.bat ${file_name}", "file_patterns": ["*.c"] }
Compile_and_run_C.bat
rem This script takes one argument, the source file, and compiles and runs it. @echo off rem Uncomment the following line if the compiler is not in `PATH` (`setlocal` unnecessary here). rem set path=C:\path\to\gcc;%path% rem Apply compiler options here. rem E.g., a reactionary as myself would write `gcc -ansi -pedantic-errors %1`. gcc %1 rem N.B.: The above line would invoke the compiler with all the default options. rem Since no output file name is specified, it would be `a.exe`! rem If there were no compilation error, only then the executable would be created. rem And, only then, run it. rem Delete it, too, if you don't want to clog up your programme folder unnecessarily. if %errorlevel%==0 ( a.exe del a.exe ) echo: pause exit
Restart Sublime Text, load up a C programme, and press Ctrl+B (or F7).
- What if we run directly, without using a Batch script, such as with
"shell_cmd": "‹compile› && ‹run›"
?
This would output to Sublime Text's build panel, which cannot accept input. You have to open a command window withstart
. - How about
"shell_cmd": "start ‹compile› && ‹run›"
then?
This would compile in a command window but then output to Sublime Text's build panel, which cannot accept input. - Okay. Then
"shell_cmd": "start ‹compile› && start ‹run›"
?
This would spawn different command windows for each command!
Think of the "shell_cmd"
as one-shot: start
one command (and don't put && ‹other commands›
). Make that command a Batch script and do everything else in there.
After this, you might not need Sublime Text's build panel. To disable it, go to Preferences→Settings and put "show_panel_on_build": false
on the right side and save it.
You could always bring up the build panel with Tools→Build Results→Show Build Results (or Shift+Esc).

- 21
- 3
-
What you're referring to here as the Console is not the console. In Sublime, `View > Show Console` shows the console, and it's a way to interact with the Python plugin system and see what plugins are doing. The item you're referring to is the `Build Output Panel` which, as the name suggests, is output only. Referring to it as a console can give the mistaken impression that it's something it's not. – OdatNurd Dec 22 '21 at 16:22
-
"*To take input from STDIN, we **have to** open a command (shell) window.*" That's not true at all. The [`Terminus`](https://packagecontrol.io/packages/Terminus) plugin is an excellent alternative to using an external shell instance, and has the added advantage that it works across all supported platforms, not just Windows. I'm just wondering what advantage this answer has over [this one](https://stackoverflow.com/a/19297613), for example. – MattDMo Dec 22 '21 at 17:22
-
This doesn't really answer the original question, which is "*How to compile and run any (language) programme*". This works for C on Windows, but Sublime already has a built-in build system for C. To make it work for other languages, you would need to customize your `.bat` file to essentially be an external build system, (poorly) duplicating a robust feature that Sublime already has. – MattDMo Dec 22 '21 at 17:29
-
I've never used the Terminus plug-in. Even if it's a way, this is another, then! ---- And, by the way, this answer, as noted in bold at the top, only uses C or GCC or Windows as an example. It can be suited appropriately to another use case. ---- The built-in build system does not work with inputs without some extra work, if I am not mistaken. – a_das Dec 22 '21 at 17:48
-
The primary goal of this technique is to have a one-stop-shop for **everything** (compiler messages, input, output). – a_das Dec 22 '21 at 17:50