A VS Code extension that automatically removes unused #include directives from C/C++ files using diagnostics from clangd.
| Feature | Description |
|---|---|
| Auto-remove on save | Removes unused includes every time you save a C/C++ file |
| Command: current file | Manually apply to the active file |
| Command: workspace | Apply to all C/C++ files in the workspace at once |
| Debug: dump diagnostics | Print all diagnostics for the active file to the Output panel |
- clangd VS Code extension (
llvm-vs-code-extensions.vscode-clangd) - clangd binary installed on the system (clangd 17+ recommended for
-std=c++23support)
The VS Code clangd extension requires the clangd binary to be installed separately.
Debian/Ubuntu (via LLVM apt repository):
# Add LLVM GPG key
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key \
| sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null
# Add repository (adjust codename: bookworm, jammy, etc.)
echo "deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-19 main" \
| sudo tee /etc/apt/sources.list.d/llvm.list
sudo apt-get update && sudo apt-get install -y clangd-19
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-19 100Note: The version provided by
apt-get install clangd(without a version suffix) is typically outdated. Use the LLVM apt repository to get a recent version.
Create a .clangd file at your project root:
Diagnostics:
UnusedIncludes: StrictTo also set the C++ standard:
CompileFlags:
Add: [-std=c++23]
Diagnostics:
UnusedIncludes: StrictNote:
compile_commands.jsonis not strictly required, but clangd's accuracy improves significantly with it. For CMake projects:cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B buildFor Make-based projects:bear -- make
If the Microsoft C/C++ extension is also installed, disable its IntelliSense engine to avoid conflicts:
With this setting, clangd handles all language features and the Microsoft C/C++ extension provides only its debugger.
npm install
npx vsce packageThen install the generated .vsix file via Extensions: Install from VSIX... in the Command Palette.
npm install
npm run compilePress F5 in VS Code to launch the Extension Development Host.
{
// Enable auto-removal on save (default: true)
"cppUnusedIncludes.enableOnSave": true,
// Diagnostic source name to match against (default: "clangd")
"cppUnusedIncludes.diagnosticSource": "clangd",
// Milliseconds to wait after save before reading diagnostics (default: 100)
// Increase if includes are not removed (language server still analyzing).
"cppUnusedIncludes.waitForDiagnosticsMs": 100,
// Glob pattern for files targeted by the workspace-wide command
"cppUnusedIncludes.workspaceFileGlob": "**/*.{cpp,cc,cxx,c,h,hpp,hxx}"
}| Command | Description |
|---|---|
C++: Remove Unused Includes in Current File |
Remove unused includes from the active file |
C++: Remove Unused Includes in Workspace |
Apply to all C/C++ files in the workspace |
C++: Dump Diagnostics to Output (Debug) |
Print diagnostics for the active file to the Output panel |
Run commands from the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
- Open the Output panel (
View > Output) and select C++ Unused Includes Remover from the dropdown. - Save a C++ file and check the log.
no diagnostics — clangd is not providing diagnostics. Check that:
- The clangd VS Code extension is installed and active.
- The clangd binary is installed (
clangd --versionin the terminal). - A
.clangdfile withUnusedIncludes: Strictexists at the project root. - The file has been open long enough for clangd to finish analyzing.
0 unused include(s) to remove — Diagnostics are present but none match. Run C++: Dump Diagnostics to Output (Debug) and verify that source="clangd" entries appear.
If diagnostics are not ready when the extension reads them, no includes will be removed. Increase the wait time:
{
"cppUnusedIncludes.waitForDiagnosticsMs": 500
}File saved
│
▼
Wait waitForDiagnosticsMs (default: 100ms)
│
▼
vscode.languages.getDiagnostics() — fetch diagnostics
│
▼
Filter: source === "clangd"
AND (code === "unused-includes" OR message contains "unused"+"include")
│
▼
Sort by line number descending (prevents line-number drift)
│
▼
Delete each #include line via WorkspaceEdit
│
▼
Save document
This repository includes a Dev Container configuration. You can develop the extension in a fully pre-configured environment without installing anything locally.
Requirements: Docker and either VS Code with the Dev Containers extension or GitHub Codespaces.
- Open the repository in VS Code.
- When prompted, click Reopen in Container (or run
Dev Containers: Reopen in Containerfrom the Command Palette). - The container will automatically run
npm installviapostCreateCommand. - Once the container is ready, press
F5to launch the Extension Development Host.
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Watch mode (recompile on change)
npm run watch
# Lint / format
npm run checkMIT



