4  SetUp Vscode to work with Stata

Abstract
Steps to configure VScode for Stata development.

4.1 Install VScode and Stata Extensions

  1. Open “Company Portal” app on your computer.

  2. Search for VS Code and install it.(Make sure Git Credential Manager is selected)

  3. Search for GitForWindows and install it.

  4. Open VS Code → Extensions (left sidebar), and install:

    • Stata Enhanced (syntax highlighting).
    • Git Graph (visualize branches).
    • GitLens (blame/insight).
    • GitHub Pull Requests and Issues.
    • GitHub Copilot (optional).
    • GitHub Copilot Chat (optional).

4.2 Configure rundo and rundolines

All the information in this section is based on Friedrich Huebler’s article. If you need more detail, see his blog.

  1. For convenience, we also mirror the files here: https://github.com/GPID-WB/Pip-Technical-Guidelines/raw/refs/heads/main/_data/rundofiles.zip

  2. Extract the ZIP into C:\ado\personal. You’ll see six files; you only need to modify two of them:

    • rundo.ini
    • rundolines.ini
  3. Open rundo.ini and rundolines.ini and set:

    • statapath → full path to your Stata EXE, e.g.: statapath = "C:\Program Files\Stata19\StataMP-64.exe"
    • statawinexact Stata window title, e.g.: statawin = "StataNow/MP 19.5"
    • statacmd → shortcut that focuses Stata’s Command window (default is ^1 = Ctrl+1).
  4. The two INIs should be identical except for the filename. Keep both updated.

4.3 Set up User Tasks (global, once) to call rundolines / rundo

We use User Tasks so you don’t have to repeat this per workspace.

  1. Open Command Palette (Ctrl+Shift+P) → type: Tasks: Open User Tasks.
  2. Paste this JSON (adjust paths to match your machine). NOTE:
  • Use double backslashes \\ in paths.
  • If "tasks" already exists, just add the two task objects inside the array.
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Stata: Run selection/line (rundolines)",
      "type": "shell",
      "command": "\"C:\\ado\\personal\\rundolines.exe\"",
      "presentation": {
        "focus": false,
        "panel": "dedicated",
        "showReuseMessage": false
      }
    },
    {
      "label": "Stata: Do current file (rundo)",
      "type": "shell",
      "command": "\"C:\\ado\\personal\\rundo.exe\"",
      "args": ["\"${file}\""],
      "presentation": {
        "focus": false,
        "panel": "dedicated",
        "showReuseMessage": false
      }
    }
  ]
}
  • rundolines sends the current selection (or current line if nothing is selected).
  • rundo runs the entire file (saves first).

4.4 Bind your keyboard shortcuts (global, once)

  1. Open Command Palette (Ctrl+Shift+P) → type: Keyboard Shortcuts → select: Open Keyboard Shortcuts (JSON).
  2. Add (or adapt keys if you already use F9/F10 or if you don’t want to use F9/F10):
  3. Houbler suggests not use any shortcut with the key Ctrl as it may get “stuck”.
[
  {
    "key": "f9",
    "command": "workbench.action.tasks.runTask",
    "args": "Stata: Run selection/line (rundolines)",
    "when": "editorTextFocus && (resourceExtname =~ /\\.(do|ado|mata)$/ || (resourceScheme == 'untitled' && editorLangId == 'stata'))"
  },
  {
    "key": "f10",
    "command": "workbench.action.tasks.runTask",
    "args": "Stata: Do current file (rundo)",
    "when": "editorTextFocus && (resourceExtname =~ /\\.(do|ado|mata)$/ || (resourceScheme == 'untitled' && editorLangId == 'stata'))"
  }
]
  • The when clauses limit these keys to Stata files.
  • If you already mapped F9/F10 to something else, either change those or pick different keys (e.g., f8 / f9).

Tip: If you still have Code Runner installed, remove any bindings that clash (search “Run Code” in Keyboard Shortcuts and delete its keybindings). You can also disable/uninstall Code Runner entirely—you don’t need it.


4.5 (Optional) Running Mata files directly

Two clean options; pick one.

4.5.2 B) Mata blocks inside a .do

Place your Mata code inside a mata: … end block in a .do, then use F9 (selection) or F10 (whole file).


4.6 Quick sanity test (30 seconds)

  1. Open a .do. Type and select:

    display "hello from VS Code"
  2. Press F9 → Stata should execute the selection.

  3. Press F10 → Stata should run the entire file.

If nothing happens, check:

  • Tasks names match your keybinding args exactly.
  • You edited User Tasks (not workspace .vscode/tasks.json).
  • No conflicting keybinding is intercepting F9/F10.

4.7 Troubleshooting & Performance

  • First run is slower (1–3 s) due to task runner warm-up and first attach. Subsequent runs are often sub-second with tuned INIs.
  • If sends are flaky, raise winpause slightly (e.g., from 120 → 140 ms).
  • Verify statawin matches the exact window title (minor versions matter).
  • Keep Stata open; cold-launches add seconds.
  • Avoid network paths for ado/working dirs when possible.
  • Consider AV exclusions for the Stata EXE, rundolines.exe, rundo.exe, and your ado/workspace.