I think you are looking for the
You can see all lines starting with
and put them in a file using the
:redir command :
:redir > toc.txt
toc.txt will contain the line numbers if you have
set number. There is also this post on the subject.
Alternatively, you can delete all lines not starting with
:g! and save it to another file.
:w! toc.txt | undo
Look also for
:global applies the command you provide to each matched lines in the order they appear in the file, so
:g/^#/t0 will copy headers to the top of the file but in reverse. We could do
:v/^#/m$ to move all non-header lines to the end, leaving the headers on top but stripped from the original text. Another way is to place some marker to mark the end of the table of content and copy header lines one line above this marker :