Every time you open a new chat in Claude Code extension, it automatically attaches your currently-open file as context.
This wastes tokens, can leak sensitive files (like .env), and adds noise to every prompt.
This guide shows you how to turn off that feature.
Based on community fix from:
Important
Disable auto-update for the "Claude Code for VS Code" extension, otherwise auto-updates will silently overwrite your patched file.
Go to the Extensions tab (Ctrl+Shift+X / Cmd+Shift+X), find "Claude Code for VS Code", and uncheck Auto Update.
When you want to update:
- Disable "Claude Code for VS Code" extension
- Update to the latest version
- Re-enable the extension
- Restart Extension
- Re-run the script
-
Download or save the script to a file, e.g.
fix-auto-attach.ps1. -
Open PowerShell in the folder where you saved the script and run:
# The -Force parameter skips the user confirmation prompt to speed things up Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force .\fix-auto-attach.ps1
Or apply directly with a one-liner (no script download needed):
$f = (Get-ChildItem "$env:USERPROFILE\.vscode\extensions\anthropic.claude-code-*\webview\index.js" -Recurse | Select-Object -Last 1).FullName; $c = Get-Content $f -Raw; $c = $c -replace 'useRef\(!0\),\[(\w+),(\w+)\]=(\w+)\.useState\(!0\)', 'useRef(!0),[$1,$2]=$3.useState(!1)'; Set-Content $f $c -NoNewline
❗ Important: For Cursor replace
\.vscodewith\.cursor, and for VSCodium with\.vscode-oss. -
Reload VS Code / VSCodium / Cursor:
- Press
Ctrl+Shift+P - Type and run: Developer: Reload Window
- Press
Run the fix in one command:
WV=$(find ~/.vscode{,-server}/extensions -path '*anthropic.claude-code-*/webview/index.js' 2>/dev/null | tail -1)
cp "$WV" "$WV.bak"
perl -i -pe 's{useRef\(!0\),\[([A-Za-z_\$][\w\$]*),([A-Za-z_\$][\w\$]*)\]=([A-Za-z_\$][\w\$]*)\.useState\(!0\)}{useRef(!0),[$1,$2]=$3.useState(!1)}' "$WV"❗ Important: For Cursor replace ~/.vscode with ~/.cursor, and for VSCodium replace with ~/.vscode-oss.
Then press Cmd+Shift+P → Developer: Reload Window.
Caution
🚨 USE AT YOUR OWN RISK! 🚨
If you want to be absolutely certain that files can't be auto-injected — even if you accidentally click the toggle — use Hardcore mode. It removes the injection code entirely. The toggle button remains visible but does nothing.
PowerShell:
.\fix-auto-attach.ps1 -HardcoreBash:
WV=$(find ~/.vscode{,-server}/extensions -path '*anthropic.claude-code-*/webview/index.js' 2>/dev/null | tail -1)
cp "$WV" "$WV.bak"
perl -0 -i -pe 's{if\(([A-Za-z_\$][\w\$]*)\)if\(\1\.selectedText\)([A-Za-z_\$][\w\$]*)\.push\(\{type:"text",text:`<ide_selection>[\s\S]*?</ide_selection>`\}\);else \2\.push\(\{type:"text",text:`<ide_opened_file>[\s\S]*?</ide_opened_file>`\}\);}{}g' "$WV"❗ Important: For Cursor replace ~/.vscode with ~/.cursor, and for VSCodium replace with ~/.vscode-oss.
Then Developer: Reload Window (Ctrl+Shift+P / Cmd+Shift+P).
Want to undo the patch? Restore the backup and reload:
PowerShell:
Get-ChildItem "$env:USERPROFILE\.vscode\extensions\anthropic.claude-code-*\webview\index.js.bak" | ForEach-Object {
Copy-Item -Path $_.FullName -Destination (Join-Path $_.DirectoryName "index.js") -Force
}❗ Important: For Cursor replace \.vscode with \.cursor, and for VSCodium with \.vscode-oss.
Bash:
WV=$(find ~/.vscode{,-server}/extensions -path '*anthropic.claude-code-*/webview/index.js' 2>/dev/null | tail -1) && cp -f "$WV.bak" "$WV"❗ Important: For Cursor replace ~/.vscode with ~/.cursor, and for VSCodium replace with ~/.vscode-oss.
Then Developer: Reload Window (Ctrl+Shift+P / Cmd+Shift+P).
Note
If the extension auto-updated since you applied the patch, the .bak file may be a version behind and causing errors when the extension runs.
In that case, reinstall the extension to get a fresh index.js, or download the same version manually from Open VSX (under Resources) and copy webview/index.js into the extension's webview/ folder.
The Claude Code webview uses React state (useState(true)) to decide whether to inject the open file. The patch flips the default to useState(false). That's it. The toggle button still works — you can turn attachment ON for individual chats when you want it.
No. The patch only changes a default value. All features remain functional. The only difference: your open file won't be auto-attached to new conversations.
Auto-updates revert the patch because the extension files get replaced. After an update, just run the script again. The script tells you if the pattern wasn't found.
The extension was likely updated and the minified variable names changed. Check the original GitHub comment for updates, or file an issue linking back to it.
No. Only the automatic <ide_opened_file> and <ide_selection> injection. Manual attachments still work as expected.
Yes. The PowerShell script automatically searches .vscode, .vscode-oss, and .cursor extension directories and picks the most recent bundle.
| Action | Command |
|---|---|
| Apply fix (Windows) | .\fix-auto-attach.ps1 |
| Apply fix (Mac/Linux) | Run the perl one-liner above |
| Hardcore mode (Windows) | .\fix-auto-attach.ps1 -Hardcore |
| Revert | Restore .bak file, reload window |
| Reload window | Ctrl+Shift+P → Developer: Reload Window |
The auto-attach chip state lives in a minified React component inside the extension's webview/index.js. The relevant line looks like this in the minified bundle:
useRef(!0),[P,_]=n1.useState(!0) // true = ON by defaultThe patch changes the second !0 (true) to !1 (false):
useRef(!0),[P,_]=n1.useState(!1) // false = OFF by defaultThe useRef(!0) before the destructuring is what makes this anchor unique — there are other useState(!0) calls in the bundle, but only one is preceded by useRef(!0) with a two-variable destructuring.
Tested on: Claude Code for VS Code 2.1.177