Stata + VS Code Workflow on Windows

Run Stata do-files and selections from VS Code via Code Runner and rundo/rundolines — two extensions, two INI files, five settings, zero custom scripts.

Beginner Verified Working Updated 6 min read Tested on Stata/MP 17 (StataMP-64.exe), VS Code + Code Runner 0.12.2 on Windows 11 Pro 25H2 (build 26200.8655)

This guide wires VS Code to the Stata GUI so you can execute an entire do-file or just the selected lines with a keyboard shortcut, exactly like Stata’s built-in do-file editor — but with VS Code’s editing power (multi-cursor, LSP-grade highlighting, Git integration). The whole setup is two extensions, two INI files, and five VS Code settings. No PowerShell scripts, no task runners, no custom keybindings.

The same workflow installs cleanly on the ReviOS base from Tiny 11 vs Atlas OS vs ReviOS and has an LMDE counterpart described in the dual-boot guide.


How It Works

Two small utilities by Friedrich Huebler — rundo (run a whole do-file) and rundolines (run the selected lines) — do all the heavy lifting:

ToolWhat it doesInvoked by
rundo.exeCopies the do-file path to the clipboard, focuses Stata, types do "path" into the Command window and presses EnterCode Runner’s Run command (Ctrl+Alt+N)
rundolines.exeGrabs the selected text from the focused editor, writes it to a temp do-file, and executes it the same wayCode Runner’s Custom Command (Ctrl+Alt+K)

Both are compiled AutoIt v3 scripts, and the .au3 sources ship next to the executables — you can read exactly what they do before trusting them. They launch Stata if it isn’t running, and reuse the running instance if it is.


Prerequisites

  • Stata installed at C:\Program Files\Stata17\ (this guide uses Stata/MP 17; any 64-bit Stata works — adjust paths and the window title for your version).
  • VS Code with two extensions:
code --install-extension formulahendry.code-runner
code --install-extension kylebarron.stata-enhanced
  • Code Runner executes files and custom commands with default shortcuts.
  • Stata Enhanced gives .do files proper syntax highlighting and code outlining. (No Stata MCP server or automation library is needed for this workflow.)

Step 1: Install rundo and rundolines

  1. Download both programs from Friedrich Huebler’s guide to integrating Stata and external text editors (version 5.1 — the links were refreshed in October 2024; v5.1 supports Stata up to 17).
  2. Extract the two folders into Stata’s personal ado directory:
C:\Program Files\Stata17\ado\personal\
├── rundo51\
│   ├── rundo.exe
│   ├── rundo.au3        ← AutoIt source, auditable
│   ├── rundo.ini        ← you will edit this
│   └── rundo_readme.txt
└── rundolines51\
    ├── rundolines.exe
    ├── rundolines.au3
    ├── rundolines.ini   ← you will edit this
    └── rundolines_readme.txt

Writing to C:\Program Files requires administrator rights — approve the UAC prompt when extracting.


Step 2: Configure the INI Files

Open rundo.ini and set three values (shown here with the tested Stata 17 values):

[Stata]
statapath = "C:\Program Files\Stata17\StataMP-64.exe"
statawin = "Stata/MP 17.0"
statacmd = "^1"

[Delays]
clippause = 200
winpause = 300
keypause = 1

rundolines.ini gets identical values.

KeyMeaningNotes
statapathFull path to your Stata executableMust match your edition (StataMP-64.exe, StataSE-64.exe, StataBE-64.exe) and version folder
statawinExact title of the Stata main windowMust match character-for-character — this is how the tool finds the window. Check your exact title in Stata’s title bar (e.g. Stata/MP 17.0)
statacmdShortcut that focuses Stata’s Command window^1 is AutoIt syntax for Ctrl+1, the Stata default. Change only if you remapped it inside Stata
clippauseWait after copying to clipboard (ms)Raise toward 200 if runs silently fail; lower toward 0 to speed up
winpauseWait between window operations (ms)Lower to speed up, raise if window activation is flaky
keypauseWait between keystrokes (ms)Leave at 1

Step 3: Configure VS Code

Add to settings.json (Ctrl+Shift+P → Preferences: Open User Settings (JSON)):

{
  // Hand whole .do files to rundo when Code Runner runs them
  "code-runner.executorMapByFileExtension": {
    ".do": "\"C:\\Program Files\\Stata17\\ado\\personal\\rundo51\\rundo.exe\"",
  },

  // Custom Command (Ctrl+Alt+K) = rundolines = run the selection
  "code-runner.customCommand": "\"C:\\Program Files\\Stata17\\ado\\personal\\rundolines51\\rundolines.exe\"",
  "workbench.settings.applyToAllProfiles": ["code-runner.customCommand"],

  // The three settings below are NOT optional for this workflow — see why:
  "code-runner.runInTerminal": false,
  "code-runner.preserveFocus": true,
  "code-runner.saveFileBeforeRun": true,

  // Run each file from its own folder, so do-files find data beside them
  "code-runner.fileDirectoryAsCwd": true,
}

Why the three flagged settings matter — each one silently breaks the workflow if flipped:

  • runInTerminal: false — Code Runner must use the Output panel, not the integrated terminal. Terminal mode moves focus to the terminal, and rundolines copies the selection by sending Ctrl+C to whatever window has focus — so it would copy from the terminal and silently run nothing.
  • preserveFocus: true — same reason, opposite direction: focus must stay in the editor when the Output panel appears, so Ctrl+C copies your Stata selection.
  • saveFileBeforeRun: true — otherwise Ctrl+Alt+N runs the last saved version of the file, which may disagree with what you see in the editor.

Usage

No custom keybindings needed — Code Runner’s defaults cover everything:

ActionShortcut
Run the entire do-file in StataCtrl+Alt+N
Run the selected lines in StataSelect lines, then Ctrl+Alt+K
Stop a Code Runner processCtrl+Alt+M

Both tools launch Stata automatically if it isn’t running, then reuse the running instance. Results appear in Stata’s Results window exactly as if you had typed them.


Troubleshooting

Nothing happens when I run. Almost always the window title: statawin in the INI must match Stata’s title bar exactly. Stata/MP 17.0 ≠ Stata/MP 17 ≠ StataNow/MP 19.5. Open Stata, read the title bar, copy it verbatim.

Runs work but selections don’t (or vice versa). Check the focus rules: runInTerminal must be false and preserveFocus must be true. If focus lands anywhere other than your VS Code editor when you trigger the command, rundolines copies the wrong thing.

Runs are flaky or paste garbage. Raise clippause toward 200 and winpause toward 300 in both INI files. Slow machines and busy CPUs occasionally need the extra breathing room.

“Cannot find Stata window” on first run after changing the title. Fully close and reopen Stata so the new window title takes effect, then retry.

Stata opens but nothing executes. Verify statacmd matches your Stata shortcut for focusing the Command window (Ctrl+1 by default). If you changed it in Stata’s preferences, update ^1 in the INI to match (AutoIt sends Ctrl as ^).


What Didn’t Work (and Why)

ApproachWhy It Failed
Custom PowerShell scripts that copy to clipboard and SendKeys into Stata~160 lines of clipboard juggling, window-focus P/Invoke, and timing races — rundo/rundolines do the same job in two compiled utilities with tunable INI delays
code-runner.runInTerminal: trueMoves focus to the terminal; rundolines then copies the terminal content instead of your selection and silently runs nothing
code-runner.preserveFocus: falseThe Output panel steals focus; same silent failure as above
code-runner.saveFileBeforeRun: falseCtrl+Alt+N executes the last saved file, which quietly disagrees with the unsaved editor content

Official References