Sublime Text

How to Run Code in Sublime Text in Steps

How to Run Code in Sublime Text in Steps

Sublime Text does not run code out of the box, and that surprises a lot of developers the first time they press Ctrl+B and nothing happens.

Learning how to run code in Sublime Text comes down to one thing: understanding the build system. Once you get that, everything else follows.

This guide covers:

  • How the .sublime-build file and output panel work
  • Running Python, JavaScript, C++, and Java with exact configurations
  • Handling user input using the Terminus package
  • Fixing the most common build errors, including PATH issues on Windows and Mac

No fluff. Just the steps that work.

What Is a Build System in Sublime Text?

maxresdefault How to Run Code in Sublime Text in Steps

A build system in Sublime Text is a JSON configuration file that tells the editor which external command to run, which shell to use, and where to display output. It is not a compiler or runtime. It is a bridge between the editor and whatever tool is already installed on your machine.

Build systems are stored as .sublime-build files. Each file is a plain JSON object with fields like cmd, selector, working_dir, and encoding.

When you press Ctrl+B (Windows/Linux) or Cmd+B (Mac), Sublime Text reads the active .sublime-build file, fires the specified shell command, and pipes the standard output into the built-in output panel at the bottom of the editor window.

This is a key distinction from a full web development IDE: Sublime Text provides the trigger mechanism, not the execution environment. Python, Node.js, GCC, and every other runtime must be installed separately and accessible via the system PATH.

What the Output Panel Shows

The output panel captures stdout and stderr from the shell command. It appends a line like [Finished in 1.3s] after execution completes. The panel is read-only. It does not accept keyboard input, which matters a lot when running scripts that use input() or similar stdin calls.

Where Build System Files Live

Sublime Text scans the Packages/User directory to populate the Tools > Build System menu. Default build systems ship with the editor and live in Packages/Default. Custom ones created via Tools > Build System > New Build System save directly to Packages/User.

FieldRequiredPurpose
cmdYesCommand array to execute (e.g., ["python", "-u", "$file"])
selectorNoScope selector for auto-detection (e.g., source.python)
working_dirNoDirectory to run the command from
file_regexNoPerl-style regex to capture error lines for F4 navigation
encodingNoOutput encoding, defaults to UTF-8

Sublime Text Build 4 (released 2021, latest stable as of 2025) ships with default build systems for Python, Ruby, Make, and a handful of others. JavaScript, Java, and C++ require either custom files or PATH adjustments.

What Are the Ways to Run Code in Sublime Text?

maxresdefault How to Run Code in Sublime Text in Steps

There are 3 distinct methods to execute code inside Sublime Text. Each one fits a different scenario, and picking the wrong one wastes time.

MethodBest ForSupports stdin?
Built-in build systemQuick scripts, interpreted languagesNo
Custom .sublime-build fileCompiled languages, custom workflowsNo
Terminus packageInteractive programs, long-running processesYes

The built-in build system covers most scripting use cases. The moment your script asks for user input at runtime, you need Terminus. That is a hard wall, not a preference.

No debugger ships with any of these methods. Sublime Text is a source code editor, not a full IDE. If you need breakpoints and variable inspection, tools like PyCharm handle that workflow more directly. Worth knowing upfront rather than spending an hour looking for a debug panel that does not exist.

How to Run Python Code in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

Sublime Text includes a Python build system by default. No installation required. The selector field is already set to source.python, so Automatic mode works correctly for .py files.

Steps to Run a Python File

Process:

  1. Save the file with a .py extension
  2. Go to Tools > Build System > Python
  3. Press Ctrl+B (Windows/Linux) or Cmd+B (Mac)
  4. Output appears in the panel at the bottom

Sublime Text runs the last saved version of the file, not the current buffer. If the output does not reflect your latest change, the file was not saved. Ctrl+S first, then Ctrl+B.

How to Fix the Python PATH Error on Windows

The most common error on Windows looks like this:

'python' is not recognized as an internal or external command

This appears when Python is installed but not added to the system PATH during setup. 3 ways to fix it:

  • Reinstall Python and check “Add Python to PATH” during setup
  • Manually add the Python installation directory to the system PATH via Environment Variables
  • Use an absolute path in the cmd field of the build system: ["C:\Python312\python.exe", "-u", "$file"]

The PATH variable that Sublime Text reads can differ from the PATH in your terminal. Even if python works in Command Prompt, Sublime may not see it. The absolute path approach in the build file is the most reliable fix on Windows machines with non-standard setups.

How to Handle User Input in Python Scripts

The default output panel does not accept keyboard input. Scripts using input() will hang at the prompt with no way to type a response.

2 practical solutions:

  • Install Terminus and use "target": "terminus_exec" in the build file (covered in Section 5)
  • Run the script directly from the system terminal when stdin is needed

There is no workaround that makes the built-in panel interactive. This is a known architectural limitation, not a bug.

How to Run JavaScript and Node.js Code in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

No JavaScript build system ships with Sublime Text. Running .js files requires Node.js installed locally and a custom .sublime-build file pointing to the node executable.

According to the 2024 Stack Overflow Developer Survey, JavaScript remains the most-used programming language for the 12th consecutive year (used by 62.3% of respondents), making this one of the more common Sublime Text setup requests.

Creating the Node.js Build System

Steps:

  1. Install Node.js from nodejs.org and confirm it is in PATH by running node -v in a terminal
  2. In Sublime Text, go to Tools > Build System > New Build System
  3. Paste the following JSON and save as Node.sublime-build
{
  "cmd": ["node", "$file"],
  "selector": "source.js",
  "working_dir": "$file_path"
}

After saving, select Tools > Build System > Node and press Ctrl+B. Test with a file containing console.log("hello") before moving to more complex scripts.

Why the Build Silently Fails

Root cause in most cases: the node executable is not in the PATH that Sublime Text reads at launch time. The fix is the same as Python on Windows. Use the absolute path to node.exe if the relative name does not resolve:

"cmd": ["C:\Program Files\nodejs\node.exe", "$file"]

On Mac and Linux, Node installed via nvm does not automatically appear in Sublime’s PATH. Adding the nvm shim path to the "path" field in the build file resolves this cleanly.

How to Run C and C++ Code in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

C and C++ execution is a 2-step process: compile the source file first, then run the output binary. The default C++ build system in Sublime Text only compiles. It does not run the resulting executable automatically.

Setting Up GCC on Windows for Sublime Text

GCC is available on Windows via MinGW-w64 or the MSYS2 toolchain. After installation, confirm the compiler is accessible by running gcc --version in Command Prompt.

If gcc is not found:

  • Add the MinGW bin directory (e.g., C:\mingw64\bin) to the system PATH
  • Restart Sublime Text after updating PATH
  • Test by opening View > Show Console and running import subprocess; subprocess.run(["gcc", "--version"])

Custom Build System to Compile and Run C++

The default build system stops at compilation. Use this custom file to compile and execute in one step:

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

On Windows using cmd.exe, replace the bash command with:

"cmd": ["cmd", "/c", "g++ \"$file\" -o \"$file_path\\$file_base_name\" && \"$file_path\\$file_base_name\""]

How to Keep the Output Window Open After Execution

On Windows, the binary runs and the console window closes before you can read the output. 2 fixes:

  • Add system("pause"); before the final return 0; in your source file (quick, but modifies code)
  • Use Terminus as the build target so the output stays in an interactive panel

The Terminus approach is cleaner for any project you plan to keep long-term. Modifying source code just to read output gets old quickly.

How to Run Java Code in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

Java requires 2 separate commands: javac to compile the .java file into a .class file, and java to run the compiled class. A single cmd field can chain both using && in a shell command.

Prerequisites

Required before any build system will work:

  • Java Development Kit (JDK) installed, not just the JRE
  • JAVA_HOME environment variable set to the JDK root directory
  • Both javac and java accessible via PATH

If JAVA_HOME is missing or points to the JRE instead of the JDK, the build fails silently or throws a javac: command not found error. This is the single most common Java setup problem on both Windows and Mac.

Build System with Compile and Run Variants

Build variants let you assign separate shortcuts to the compile step and the run step inside one .sublime-build file. Ctrl+Shift+B opens the variant selector.

{
  "working_dir": "$file_path",
  "selector": "source.java",
  "variants": [
    { "name": "Compile", "cmd": ["javac", "$file_name"] },
    { "name": "Run", "cmd": ["java", "$file_base_name"] },
    { "name": "Compile and Run", "shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"" }
  ]
}

Save this as Java.sublime-build in the Packages/User directory. Use “Compile and Run” for quick iteration and the individual variants when you need to inspect the .class file before running.

How to Create a Custom Build System for Any Language

maxresdefault How to Run Code in Sublime Text in Steps

Any language with a command-line runtime or compiler can have a Sublime Text build system. The process is the same regardless of language: write a JSON file, save it to Packages/User, and select it from the Tools menu.

Build System Variables Reference

These variables expand at runtime inside the cmd, shell_cmd, and working_dir fields:

VariableResolves ToExample
$fileFull path to current file/home/user/project/main.py
$file_pathDirectory of current file/home/user/project
$file_nameFile name with extensionmain.py
$file_base_nameFile name without extensionmain
$project_pathDirectory of the open project/home/user/project

A literal $ in a JSON string must be escaped as \$ because JSON uses backslashes for its own escaping. This trips up a lot of people writing their first custom build file.

How to Add Build Variants for Compile and Run Steps

The variants key accepts an array of sub-build configurations. Each variant appears in the Ctrl+Shift+B picker and can override any top-level field.

Minimal variant structure:

{
  "working_dir": "$file_path",
  "selector": "source.go",
  "cmd": ["go", "build", "$file_name"],
  "variants": [
    { "name": "Run", "cmd": ["go", "run", "$file_name"] }
  ]
}

The name field in each variant is what appears in the command palette. If the name is exactly "Run", Sublime Text automatically binds that variant to Ctrl+Shift+B. Any other name requires manual selection from the picker each time.

This naming convention is documented in the official Sublime Text build system reference, but it is easy to miss. Took me longer than I’d like to admit to figure out why only “Run” got an automatic shortcut while every other variant I named did not.

How to Run Code with User Input in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

The built-in output panel does not accept keyboard input. Scripts that call input() in Python, readline() in Ruby, or Scanner in Java will hang at the prompt with no way to respond.

This is not a configuration issue. The panel is a read-only text buffer. Stdin support requires a real terminal.

Why the Default Panel Does Not Support stdin

The default build target, exec (implemented in Packages/Default/exec.py), runs the subprocess asynchronously and pipes stdout and stderr into the output panel. It does not open a pseudo-terminal (pty), so there is no mechanism for keystroke input to reach the running process.

In practical terms: the process starts, reaches the input() call, waits for data that never arrives, and either hangs indefinitely or exits with a broken pipe error.

Practical Workarounds for Interactive Scripts

3 options, each with a different tradeoff:

  • Terminus package: adds a real terminal inside Sublime Text with full stdin support (covered in the next section)
  • System terminal: run the file directly from Command Prompt, Terminal, or bash when input is needed
  • Refactor inputs: replace input() calls with hardcoded test values during development, then restore them before release

The refactor approach only works for short scripts. Anything with multiple interactive steps needs Terminus or the system terminal. There is no other path here.

How to Run Code Using the Terminus Package

maxresdefault How to Run Code in Sublime Text in Steps

Terminus is the first cross-platform terminal emulator for Sublime Text. It opens a real shell (bash, zsh, PowerShell, cmd.exe) inside a panel or a tab, with full stdin support, colored output, and support for long-running processes.

The Terminus package on GitHub (by randy3k) has over 2,000 stars and is one of the most actively maintained packages in the Sublime Text ecosystem as of 2025.

How to Install Terminus via Package Control

Step-by-step:

  1. Open the Command Palette: Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac)
  2. Type “Install Package Control” if not already installed, then restart
  3. Open Command Palette again, type “Package Control: Install Package”
  4. Search for “Terminus” and press Enter
  5. Restart Sublime Text after installation

If “Terminus” does not appear in the install list, check that Package Control itself is not listed in ignored_packages inside your user preferences file. This is a common setup issue that blocks all package installs silently.

How to Configure a Build System to Open in Terminus

Replace the default exec target with terminus_exec in any .sublime-build file to route output into an interactive Terminus panel instead of the read-only output panel.

{
  "target": "terminus_exec",
  "cancel": "terminus_cancel_build",
  "cmd": ["python", "$file"],
  "working_dir": "$file_path"
}

The cancel field is required. Without it, the stop-build shortcut (Ctrl+Break on Windows) will not terminate the Terminus process correctly.

Key Bindings for Terminus

Add this to your key bindings file (Preferences > Key Bindings) to open a Terminus panel instantly:

[
  {
    "keys": ["ctrl+alt+t"],
    "command": "terminus_open",
    "args": { "cwd": "${file_path:${folder}}" }
  }
]

Shell support by platform:

  • Windows: cmd.exe, PowerShell, Git Bash
  • Mac: zsh, bash
  • Linux: bash, zsh, fish

Terminus can open a panel at the bottom or a full tab depending on which command you use. terminus_open with no panel_name argument opens a tab. Adding "panel_name": "Terminus" opens it as a bottom panel. Most developers prefer the panel for quick script runs and the tab for persistent sessions.

How to Debug Common Build System Errors in Sublime Text

maxresdefault How to Run Code in Sublime Text in Steps

Most Sublime Text build failures fall into 4 categories: no build system selected, runtime not found in PATH, file not saved, and encoding conflicts. Each one has a distinct fix.

ErrorCauseFix
“No build system”Automatic mode, no selector matchTools > Build System, select manually
“command not found”Runtime not in Sublime’s PATHAdd absolute path to cmd or fix PATH
Output reflects old codeFile not saved before buildCtrl+S first, then Ctrl+B
Encoding error on file with accentsNon-ASCII characters in file pathMove file to ASCII-only path

Diagnosing the PATH Problem Directly

On Mac and Linux, Sublime Text reads its PATH from the login shell at launch time, which differs from the interactive shell PATH set by .bashrc or .zshrc.

To see exactly what PATH Sublime Text sees, open View > Show Console and run:

import os; os.environ["PATH"]

If your runtime directory is missing from that output, 2 solutions:

  • Add the runtime path to .zprofile or .bash_profile (login shell config, not .bashrc)
  • Add a "path" field directly in the .sublime-build file: "path": "/usr/local/bin:$PATH"

Reading Full Error Output

The output panel shows a truncated version of some errors. Full error details, including Python tracebacks and process exit codes, appear in the console log.

Open View > Show Console (or press Ctrl+backtick). Errors from failed builds appear there with the full shell_cmd that was executed, the dir it ran from, and the complete PATH that Sublime used. This output is the fastest way to diagnose a silent failure.

One detail that catches people out: the console shows the PATH Sublime used at the time of the build, not the current system PATH. If you updated PATH after opening Sublime Text, the build still uses the old value. Restart Sublime Text to pick up the new environment.

How to Run Code Faster with Keyboard Shortcuts and Build Variants

maxresdefault How to Run Code in Sublime Text in Steps

Reducing time between writing and running code is one of Sublime Text’s real strengths. The build shortcut system is more flexible than most developers use it.

Core Build Shortcuts

Default shortcuts across platforms:

ActionWindows / LinuxMac
Run last buildCtrl+BCmd+B
Select and run buildCtrl+Shift+BCmd+Shift+B
Cancel running buildCtrl+BreakCmd+.
Jump to next errorF4F4

Ctrl+Shift+B opens the variant picker, not just an alternative build. If you have variants named “Compile” and “Run” in your build file, both appear in that picker.

Assigning Custom Key Bindings to Specific Variants

A build variant named "Run" gets Ctrl+Shift+B automatically. Any other variant name requires a manual key binding.

Add this to Preferences > Key Bindings to assign Ctrl+F5 to a variant named “Test”:

[
  { "keys": ["ctrl+f5"], "command": "build", "args": {"variant": "Test"} }
]

The "variant" argument must match the "name" field in your .sublime-build file exactly, including capitalization. A mismatch silently falls back to the default build task instead of throwing an error, which makes this tricky to diagnose.

A Practical C++ Workflow with Variants

Combining compile and run into a single build file with 2 variants removes the need to switch build systems manually:

{
  "working_dir": "$file_path",
  "selector": "source.c++",
  "cmd": ["g++", "$file", "-o", "$file_base_name"],
  "variants": [
    {
      "name": "Run",
      "shell_cmd": "g++ \"$file\" -o \"$file_base_name\" && ./$file_base_name"
    }
  ]
}

Ctrl+B compiles. Ctrl+Shift+B runs the “Run” variant, which compiles and executes in one step. This maps directly to the compile-then-run workflow that C++ and Java development requires, without leaving the editor or opening a separate terminal.

Larger projects managed through make or CMake work the same way: the cmd field calls make, and a “Run” variant calls the compiled output binary. The build file stays small and the workflow stays keyboard-driven, which is ultimately why developers who learn the full range of Sublime Text features tend to stick with it for script-heavy work even when they use a heavier IDE for larger projects.

If you are comparing editors for a broader VS Code vs Sublime Text decision, the build system and keyboard shortcut depth is one of Sublime’s genuine advantages over more feature-heavy alternatives. Startup time under half a second, 22 MB of RAM at idle (versus 300–500 MB for VS Code at idle), and a build system that requires zero extension installs for common languages make it a practical choice for developers who run code frequently from the editor.

FAQ on How To Run Code In Sublime Text

Can You Run Code Directly in Sublime Text?

Yes. Sublime Text has a built-in build system that lets you execute code without leaving the editor. Press Ctrl+B (Windows/Linux) or Cmd+B (macOS) to run the current file. The output appears in the panel at the bottom.

How Do You Set Up a Build System in Sublime Text?

Go to Tools > Build System and select your language. If nothing fits, create a custom .sublime-build file in JSON format. Place it in your Packages/User folder. Sublime Text picks it up automatically on restart.

Why Is Ctrl+B Not Running My Code?

Your build system is either not selected or misconfigured. Go to Tools > Build System and make sure the right one is active. Also check that your interpreter path is correct and added to your system’s PATH variable.

How Do You Run a Python Script in Sublime Text?

Select Tools > Build System > Python, then press Ctrl+B. Sublime Text calls the Python interpreter directly. If it fails, your Python executable is likely missing from the PATH variable. Fix that first.

What Is the Best Package for Running Code in Sublime Text?

SublimeREPL and Code Runner are the most used options. Terminus is better if you want a proper integrated terminal inside the editor. Install any of them through Package Control in under a minute.

How Do You Open the Output Panel in Sublime Text?

Press Ctrl+B to run your build and the output panel opens automatically at the bottom. You can also open it manually via View > Show Console. It shows errors, print statements, and exit codes from your script.

Can Sublime Text Run JavaScript or Node.js?

Yes, but you need a custom build system for Node.js. Create a .sublime-build file pointing to your Node.js executable. Once set up, press Ctrl+B to execute any JavaScript file directly from the editor.

How Do You Run Code on Save in Sublime Text?

Install the Terminus package or a run-on-save plugin through Package Control. Configure it to trigger your build system when a file is saved. Works well for Python, Bash scripts, and Node.js projects.

How Do You Run a Bash Script in Sublime Text?

Create a custom build system with "shell_cmd": "bash $file" in a new .sublime-build file. Save it to Packages/User. Select it under Tools > Build System and run with Ctrl+B.

What Should You Do When the Sublime Text Build System Is Not Working?

Check 3 things: the correct build system is selected, your interpreter is installed, and the PATH variable includes it. Open the Sublime Text command palette and run “Build With” to see available options and debug from there.

Conclusion

This conclusion is for an article presenting how to run code in Sublime Text through its build system, custom .sublime-build configurations, and the Terminus package for interactive script execution.

The setup takes a few minutes. After that, Ctrl+B becomes second nature.

Whether you are running Python scripts, compiling C++ with GCC, or chaining build variants for Java, the same core pattern applies: a JSON file, the right runtime in PATH, and the correct selector field.

When the default output panel falls short, Terminus fills the gap cleanly.

Sublime Text is not a full IDE. But for fast, keyboard-driven Python development and lightweight script execution, few editors match its speed and simplicity.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Run Code in Sublime Text in Steps
Latest posts by Bogdan Sandu (see all)

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.