01 December 2025

How to automatically create 'Table of Contents' of a markdown text file in vim?

To use the Vim global command for lines containing only "===" or "---", the pattern needs to match the entire line content exactly.
Here is how to construct the pattern:
Code
:g/^\(===\|---\)/-1p
Explanation of the pattern:
  • ^: This anchor matches the beginning of the line. It ensures that the pattern must start at the very beginning of the line.
  • \(...\): This creates a capturing group for alternation.
  • ===: This matches the literal string "===".
  • \|: This is the OR operator within a regular expression. It allows matching either the pattern before it or the pattern after it.
  • ---: This matches the literal string "---".
  • \): This closes the capturing group.
  • -1 : Print the line above the '===' line (or '---' line)
  • p : Print

No comments:

Post a Comment