Search Markdown notes fast with ripgrep, fzf and MiaoYan

Managing notes6 min read

Keep your notes as plain .md files in one folder, use MiaoYan's built-in search (⌘/ across the library, ⌘F inside a note, plus #tag and todo: filters) while writing, and reach for ripgrep (rg) with fzf in the terminal when you need regular expressions, multiline frontmatter queries, or batch checks across thousands of files. Neither tool keeps a hidden database, so a line saved in MiaoYan is searchable by rg in the next millisecond, and any match rg prints opens straight back in MiaoYan. The one catch is cloud eviction: if iCloud Drive offloads an older note into a .icloud placeholder, disk-level search tools see nothing until that file is downloaded locally.

Why terminal search and app search agree on the same folder

Notes apps built around SQLite or proprietary bundles force every search through their own index. When that index lags behind sync or strips punctuation during tokenization, code snippets, command flags, and bracketed links become hard to find, and command-line tools cannot read the store safely.

In a local-first Markdown folder, the files on disk are the only source of truth. When MiaoYan searches your library on Mac, iPhone, or iPad, a background matcher checks note titles (file names without .md) first without reading from disk, ranks every title match at the top, and scans unloaded UTF-8 note bodies off the main thread up to 100 results. It also normalizes combining accents with canonical Unicode composition so searching café matches both precomposed and decomposed characters without copying every body in memory.

ripgrep walks that same directory tree using multiple threads and a finite-automaton regex engine. On an Apple silicon Mac, scanning three thousand Markdown notes takes a few milliseconds, with no background daemon and no index file left behind.

Everyday ripgrep flags for a Markdown library

Point rg at your MiaoYan folder (~/Documents/MiaoYan, or $MIAOYAN_PATH if set for the CLI) with four flags:

rg -n -S -t md -g '!Trash/**' 'connection pool' ~/Documents/MiaoYan

-n prints 1-based line numbers alongside each path. -S enables smart case: lowercase patterns (connection pool) match case-insensitively, while any uppercase letter (NSWindow) switches to exact case. -t md restricts the scan to Markdown extensions recognized by ripgrep.

-g '!Trash/**' skips MiaoYan's library-level Trash folder, where deleted notes sit before permanent removal. You need no extra globs for .git or MiaoYan's .miaoyan-conflicts backup folder, since rg skips hidden dot-directories unless you pass --hidden. When searching literal code or punctuation such as $PATH or [1], add -F (--fixed-strings) so brackets and dollar signs are not read as regex operators.

Regular expressions let rg answer structural questions across your archive in one pass.

Unfinished tasks

To list every unchecked GFM task item (- [ ], * [ ], or + [ ]), including indented subtasks:

rg -n -t md -g '!Trash/**' '^\s*[-*+] \[ \]' ~/Documents/MiaoYan

Backslashes escape [ and ] so rg matches literal brackets around a single space. For numbered tasks like 1. [ ], widen the prefix to '^\s*(?:[-*+]|\d+[.)]) \[ \]'. See Markdown task lists for GFM checkbox rules.

Inline tags without headings

Searching a bare # matches every ATX heading. Requiring start-of-line or whitespace before # and a letter right after skips # Heading lines and issue numbers like #524:

rg -n -t md -g '!Trash/**' '(?:^|\s)#[A-Za-z\p{Han}][\w/-]*' ~/Documents/MiaoYan

\p{Han} matches Chinese characters alongside ASCII letters, and [\w/-]* covers nested tags like #work/q4. Note that this regex still matches # comments inside fenced code blocks, unlike MiaoYan's built-in tag parser.

To find every note linking to [[Project plan]], including [[Project plan|the plan]] and [[Project plan#Timeline]], match the opening brackets and title as a fixed string:

rg -n -F -t md -g '!Trash/**' '[[Project plan' ~/Documents/MiaoYan

To audit the library for broken [[wikilinks]] whose target .md file does not exist:

cd ~/Documents/MiaoYan
comm -23 \
  <(rg -o --no-filename -t md -g '!Trash/**' '\[\[([^\]|#]+)' -r '$1' | sed 's/[[:space:]]*$//' | sort -u) \
  <(rg --files -t md -g '!Trash/**' | sed -E 's#^(.*/)?([^/]+)\.[^.]+$#\2#' | sort -u)

comm -23 compares unique link targets (before any | or #) against existing note file names without extensions and prints dangling targets.

YAML frontmatter blocks

Passing -U (--multiline) lets a pattern span lines, and anchoring with \A locks the match to byte zero so --- dividers in the body never match:

rg -n -U -t md -g '!Trash/**' '\A---\r?\n[\s\S]*?^status:\s*draft\b[\s\S]*?\r?\n---' ~/Documents/MiaoYan

Interactive browsing with fzf and the miao CLI

Piping rg into fzf gives you an interactive fuzzy picker with a context preview pane:

hit=$(rg --line-number --no-heading --color=always -S -t md -g '!Trash/**' '' ~/Documents/MiaoYan |
  fzf --ansi --delimiter : \
      --preview 'sed -n "$(( {2} > 5 ? {2} - 5 : 1 )),$(( {2} + 20 ))p" {1}')
[ -n "$hit" ] && open -a MiaoYan "${hit%%:*}"

Pressing Return opens the chosen .md file in MiaoYan. With the miao CLI installed (see Markdown notes from the terminal), miao search "query" runs rg -F against your detected library while skipping Trash and prints relative path:line:text hits, so miao open "$(miao search 'quarterly review' | head -1 | cut -d: -f1)" opens the first match directly.

Built-in search and filters in MiaoYan

Inside MiaoYan on Mac, press ⌘/ (File > Search) to focus library search, ⌘F to find inside the active editor or rendered preview, and ⌥⌘F for Find and Replace. On iPhone and iPad, use the Search tab across the library or the reader's find bar inside a note.

All three devices share the same query parser (NoteSearchQuery and NoteMetadataIndex) and support two structured prefixes:

  • #tag and #parent/child match tags in YAML frontmatter (tags:) and inline prose, automatically including child tags under #tag/. Fenced code, inline code, headings, URLs, pure numbers (#123), and CSS hex colors (#fff, #1C5D33) are ignored, while hex-letter English words like #facade and #decade stay valid tags.
  • todo: filters to notes with at least one open task (- [ ], * [ ], + [ ], or 1. [ ]) outside frontmatter and fenced code. Combining todo: #client invoice ranks notes higher when invoice appears inside the open task line itself and scrolls straight to that checkbox.

MiaoYan for Mac is open source on GitHub, and the App Store version covers Mac, iPhone, and iPad.

What goes wrong when matches go missing

  1. When macOS Optimize Mac Storage is on, iCloud Drive offloads untouched files into .Note Title.md.icloud binary plist placeholders. rg skips dotfiles and placeholders hold no text, returning zero hits. Run find "$NOTES_PATH" -name '*.icloud' to check, then choose Keep Downloaded in Finder or run brctl download "$NOTES_PATH".
  2. rg '- [ ]' treats - as a flag and [ ] as a character class; write rg -F -- '- [ ]' instead.
  3. MiaoYan opens .md, .markdown, and .txt files, whereas -t md skips .txt. Pass -t md -t txt when mixing both.
  4. Omitting -g '!Trash/**' includes deleted notes from MiaoYan's Trash folder.

FAQ

Does running ripgrep or editing files from the terminal corrupt MiaoYan?

No. rg only reads files, and MiaoYan watches your folder via macOS file system events. If a script modifies an open note that has unsaved edits, MiaoYan backs up your buffer in .miaoyan-conflicts before reloading.

How does miao search differ from rg?

miao search auto-detects your library path, runs fixed-string rg -F excluding Trash, and prints relative paths ready for miao open or miao cat. Use rg directly for regexes, -U multiline frontmatter queries, or fzf.

Can ripgrep or MiaoYan search inside PDFs and images in files/ or i/?

No. Both search plain-text notes (md, markdown, txt), not binary attachments.

Why does #333 or #123 run a plain text search instead of a tag filter?

MiaoYan excludes pure numbers and 3-, 6-, or 8-digit hex color values from tag extraction so issue numbers and CSS values never clutter tags, falling back to case-insensitive text search.