Jacopo Gori110 downloadsHighlight selected lines in fenced code blocks without modifying the code itself.
Select Code Lines Highlighter is a self-contained Obsidian plugin that highlights an exact text selection or whole lines inside a fenced Markdown code block. Highlights appear in the editor and Reading view. Exact-text highlights are stored in the plugin's local data, leaving the entire note unchanged. Whole-line highlights use compact hl: fence metadata.
It is useful for terminal output, Nmap scans, logs, stack traces, configuration snippets, and other blocks where copying or rewriting content just to emphasize a few lines is undesirable.
[!IMPORTANT] Version 0.2.0 adds exact-text highlights with yellow and red colors. Exact-text highlights are stored in the plugin's local data: include that data in your backups. The default command mode is now Exact text; choose Whole lines in settings to keep using line highlights.
hl: specification.hl: metadata without modifying the note.The default Highlight selection as → Exact text mode highlights only the selected characters. Select CIAO inside a code block, then run Highlight code selection. Inserting prefix before that word keeps CIAO highlighted when its surrounding context can still identify it. Selecting across lines highlights the selected text on each line.
Right-click the selection and choose Highlight code selection → Yellow or Red. Recoloring replaces only the selected part; surrounding highlights keep their colors. The original command/shortcut uses yellow in Exact text mode. Highlight code selection as critical (red) is a separate command for red text, regardless of the global selection mode. Removal affects either color. The submenu uses an optional Obsidian hook; hosts without it receive two flat menu choices instead.
Exact-text anchors live in .obsidian/plugins/select-code-lines-highlighter/data.json, keyed by note path. Each stores the text, up to 32 Unicode characters of context on each side, and occurrence information. Copying code into another note does not copy its highlights. Back up the plugin data as well as your notes; uninstalling the plugin or syncing only Markdown files can lose the highlights.
Existing local anchors without a color remain yellow. The color-aware database is version 3 and accepts the earlier version 2 format. Earlier local builds cannot edit a version 3 database; retain a backup before downgrading.
Use Remove highlight from code selection to subtract only the selected part. Clear text highlights in current code block removes located local highlights in that block. Clear all local text highlights in current note also removes unresolved anchors, whose original block cannot be determined safely. These commands participate in native Undo/Redo during the editing session.
Deleting or editing highlighted text in the editor removes its local anchor. Undo restores both. External changes are treated conservatively: unrecognizable anchors remain hidden until the note is reopened or cleared. Note and folder rename/delete events update the database. No vault-wide scans run while typing; saves are batched after a short idle period. Abrupt application termination before a save completes can lose the latest highlight changes, but cannot change the note's code.
Legacy ht:<base64url-json> metadata is still rendered. Run Move legacy text highlights to plugin storage in current note to migrate it. The command backs up the opening fences in a timestamped file inside the plugin directory, saves and reads back the new database, then removes the tokens in one undoable transaction. It refuses malformed or unresolved anchors and leaves the note unchanged. Keep migration backups until you have verified the result; they are not automatically purged.
For whole-line highlights, select Highlight selection as → Whole lines in the plugin options.
Given this block:
```text
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
445/tcp open microsoft-ds
```
Select the last two lines and run Highlight code selection in Whole lines mode. The result is:
```text hl:3-4
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
445/tcp open microsoft-ds
```
Only the opening fence changes. Run Remove highlight from code selection in Whole lines mode to subtract selected lines from the existing hl: value.
You can also right-click a selection inside a supported fenced code block to access these commands. Local text highlights, legacy ht: and hl: render together regardless of the selected command mode. Migrate legacy text highlights before editing/removing them with the local commands.
To assign a shortcut, open Settings → Hotkeys, search for Highlight code selection, and choose your preferred key combination. Existing shortcuts remain valid because the original command IDs have not changed. No shortcuts are assigned automatically.
Open Settings → Community plugins → Select Code Lines Highlighter → Options to choose Exact text or Whole lines. Text highlights have two fixed colors (yellow and red), selected per highlight in the context menu. Highlight intensity affects both colors and whole-line highlights. The color picker, accent color and accent width apply only to whole-line highlights. Visual changes apply immediately in the editor and Reading view. Restore defaults resets visual settings and selects Exact text mode; it does not recolor existing text highlights.
The plugin includes its own renderer for the editor and Reading view. It does not require Codeblock Customizer, Code Styler, or a special theme. The portable hl: metadata remains compatible with other tools that understand the same syntax.
In Obsidian, open Settings → Community plugins → Browse, search for Select Code Lines Highlighter, and install and enable it. Existing users can use Check for updates and update the plugin in place, preserving settings and local text highlights.
The GitHub release also provides the files for manual installation. The community listing notes that the plugin has not been manually reviewed by Obsidian staff.
Requires Obsidian 1.5.0 or later.
main.js, manifest.json, and styles.css from the latest GitHub release.<your-vault>/.obsidian/plugins/select-code-lines-highlighter/.hl: values must contain only positive safe integers and ascending ranges such as 2,4-6.hl: tokens, and malformed metadata are rejected.Requires Node.js 18 or later and pnpm.
pnpm install
pnpm run build
pnpm run lint
pnpm test
The production bundle is written to main.js. The source structure and build configuration follow the current official Obsidian sample plugin conventions.
Tests cover pure anchoring/metadata logic, mocked editor transactions, CodeMirror decorations and Reading view DOM slices. Real Obsidian visual checks are tracked separately from automated tests; unit tests alone do not establish visual correctness, persistence or Undo/Redo behavior.
The plugin runs locally with no telemetry, analytics or network requests. Highlight data and migration backups contain selected text and nearby context in plain text, inside your vault's plugin directory. Exact-text commands do not edit notes; whole-line commands and explicit legacy migration change only opening fences.