Clang-Format Code Blocks
approvedby Ömer Kara
Format fenced code blocks in your notes using clang-format. - This plugin has not been manually reviewed by Obsidian staff.
Clang-Format Code Blocks
Obsidian plugin. Formats fenced code blocks in your notes using clang-format.
Requirements
- Desktop Obsidian only (uses Node's
child_process, not available on mobile). clang-formatinstalled and reachable (on PATH, or point directly at the binary in settings).
What it does
Scans the active note for fenced code blocks tagged with one of the languages clang-format supports, pipes each block's contents to clang-format, and replaces the block in place with the formatted output. Other languages and nested fences are left untouched.
Recognized fence tags: c, h, cpp/c++/cc/cxx, hpp/hh/h++/hxx, objc/objective-c/m, objc++/objective-c++/mm, cs/csharp, java, js/javascript/jsx/mjs/cjs, ts/typescript/tsx, json/jsonc, proto/protobuf, textproto/textpb/txtpb, tablegen/td, verilog/v, systemverilog/sv.
Commands
- Format all code blocks in current note — formats every matching block in the active note.
- Format code block at cursor — formats only the block the cursor is currently inside.
Assign hotkeys to these in Settings -> Hotkeys.
Format on save
Enabled by default. Formatting only runs on an explicit save (Ctrl/Cmd+S) — never while you're still typing — so it won't rewrite a block out from under you mid-edit. Failures still show a notice; successful auto-formats are silent. Toggle off in plugin settings if you don't want this.
This works by wrapping Obsidian's internal editor:save-file command (the same approach the Linter plugin uses), since there is no public "before save" hook. It's guarded so a future change to that internal shape just makes format-on-save a no-op rather than breaking saves, and the wrap is undone if you disable the plugin.
Settings
- Format on save — see above.
- clang-format executable — path or command name. Default:
clang-format. - Style —
File(use a.clang-formatconfig) or a built-in style name (LLVM,Google,Chromium,Mozilla,WebKit,Microsoft,GNU). - Custom .clang-format file — only used when Style is
File. Optional absolute path, or path relative to the vault root, to a specific config file. If set, that exact file is used for every note. If left blank,clang-formatsearches upward from the current note's folder for a.clang-formatfile. - Test connection — runs
clang-format --versionand, if a custom style file is set, checks it exists.
Installing manually (not yet in the community directory)
Create <your-vault>/.obsidian/plugins/clang-format-code-blocks/ and copy main.js and manifest.json from a release (or from a local build, see below) into it. Then enable the plugin in Settings -> Community plugins.
Development
npm install
cp deploy.config.example.json deploy.config.local.json # then edit vaultPluginDir
npm run dev # watch build
npm run build # production build
npm run deploy # copy main.js + manifest.json into vaultPluginDir
npm run build:vault # build + deploy in one step
main.ts is the source, bundled to main.js via esbuild. deploy.config.local.json is gitignored — it points npm run deploy at your own vault's plugin folder. Reload Obsidian (or disable/re-enable the plugin) after deploying.
License
MIT.
For plugin developers
Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.