vimtricks.wiki Concise Vim tricks, one at a time.

How do I search only the message part of TODO comments using match boundaries?

Answer

/\vTODO:\s*\zs.{-}\ze\s*($|#)

Explanation

When TODO comments include prefixes, owners, or trailing metadata, a plain search often lands on the wrong part of the line. This pattern uses match boundaries so Vim highlights only the message text, not the TODO: label or trailing marker. It is especially useful when you want to operate on the actionable part of comments with commands like cgn, n, N, or gn.

How it works

/\vTODO:\s*\zs.{-}\ze\s*($|#)
  • \v enables very-magic mode so the pattern is easier to read.
  • TODO:\s* matches the label and optional spaces after it.
  • \zs sets the start of the actual match right after that prefix.
  • .{-} is a non-greedy match for the message body.
  • \ze sets the end of the match before trailing whitespace and metadata.
  • \s*($|#) consumes optional trailing spaces followed by end-of-line or a # marker.

Example

Given:

TODO: tighten retry logic #backend
TODO: clean dead flags

Searching with this pattern selects only:

tighten retry logic
clean dead flags

Tips

  • Use cgn right after this search to edit only the extracted message text.
  • Replace TODO: with FIXME: or NOTE: to reuse the same boundary pattern.
  • Keep .{-} non-greedy; greedy .* tends to swallow too much text.

Next

How do I join a wrapped paragraph into one line without manual cursor moves?