>> I expected :g/^\d/fold to create a fold of all lines
>> matching ^\d, but nothing happens, except that the search
>> string is higlighted. My 'foldmethod' is 'manual'. What
>> am I missing?
>
> ":fold" expects a |range|. You don't supply one. What the
> above does is put the cursor on every line starting with a
> digit and executing ":fold" on each of them, creating
> (IIUC) a one-line fold for every line starting with a
> digit.
>
> Try (untested)
>
> function FoldNumbered()
> normal! gg
> while search('^\d','W')
> let l = search('^\_[^[:digit:]]','nW')
> if l
> exe '.,' . (l-1) . 'fold'
> else
> .,$fold
> endif
> endwhile
> endfunction
This sounds like a good opportunity to use a fold expression:
:set foldmethod=expr foldexpr=getline(v:lnum)[0]=~'\\d'
will fold away lines beginning with a digit. It has fewer
odd edge-cases at the top and bottom of the file.
-tim