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.
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:
| Tool | What it does | Invoked by |
|---|---|---|
rundo.exe | Copies the do-file path to the clipboard, focuses Stata, types do "path" into the Command window and presses Enter | Code Runner’s Run command (Ctrl+Alt+N) |
rundolines.exe | Grabs the selected text from the focused editor, writes it to a temp do-file, and executes it the same way | Code 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
.dofiles proper syntax highlighting and code outlining. (No Stata MCP server or automation library is needed for this workflow.)
Step 1: Install rundo and rundolines
- 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).
- 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.
| Key | Meaning | Notes |
|---|---|---|
statapath | Full path to your Stata executable | Must match your edition (StataMP-64.exe, StataSE-64.exe, StataBE-64.exe) and version folder |
statawin | Exact title of the Stata main window | Must 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) |
statacmd | Shortcut that focuses Stata’s Command window | ^1 is AutoIt syntax for Ctrl+1, the Stata default. Change only if you remapped it inside Stata |
clippause | Wait after copying to clipboard (ms) | Raise toward 200 if runs silently fail; lower toward 0 to speed up |
winpause | Wait between window operations (ms) | Lower to speed up, raise if window activation is flaky |
keypause | Wait 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 sendingCtrl+Cto 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, soCtrl+Ccopies your Stata selection.saveFileBeforeRun: true— otherwiseCtrl+Alt+Nruns 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:
| Action | Shortcut |
|---|---|
| Run the entire do-file in Stata | Ctrl+Alt+N |
| Run the selected lines in Stata | Select lines, then Ctrl+Alt+K |
| Stop a Code Runner process | Ctrl+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)
| Approach | Why 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: true | Moves focus to the terminal; rundolines then copies the terminal content instead of your selection and silently runs nothing |
code-runner.preserveFocus: false | The Output panel steals focus; same silent failure as above |
code-runner.saveFileBeforeRun: false | Ctrl+Alt+N executes the last saved file, which quietly disagrees with the unsaved editor content |
Official References
- Huebler’s guide to integrating Stata and external text editors — rundo/rundolines download, official user guide, and the original source of the Code Runner + rundo pattern
- Code Runner extension
- Stata Enhanced extension
Related Guides
- Power User Software Inventory — Install VS Code and the rest of the toolchain via winget
- Debloated Windows: Tiny 11 vs Atlas OS vs ReviOS — The Windows base this workflow runs on