how can I identify a single line no longer than e.g. 60 characters
preceded and followed by a blank line via regexs. This way I want to
identify section headings. What I did was mark every blank line with
%s/^$/>>>/ and than chomp the CR %s/\n//g and if the text
between the >>>'s isn't longer than 60 characters put it into
\section{}, and replace every >>> with \r\r. But in larger files
this takes a while. Is there a smarter solution to the problem?

You might try:

/^\s*\n\zs.\{1,60}\ze\n\s*$
/^\n\zs.\{1,60}\ze\n$

which should find what you describe (the former allows a "blank" line to contain whitespace while the latter looks for a truely "blank" line with zero whitespace).

It breaks down as

 ^          the start of line
 \s*        (the optional whitespace, as described)
 \n         a newline
 \zs        treat this as the beginning of the match
 .\{1,60}   1-60 non-newline characters
 \ze        you could optionally use this here
            to treat this as the end of the match
 \n         another newline
 \s*        (more optional whitespace, as described)
 $          the end of a line

The above regexp(s) (with or without the "\s*" or the "\ze") can also be used in ":s" commands to do things like

:%s/^\s*\n\zs.\{1,60}\ze\n\s*$/\U&

to uppercase all your titles, or

:%s/^\s*\n\zs.\{1,60}\ze\n\s*$/\=submatch(0)."\n".substitute(submatch(0), '.', '=', 'g')

which will underline all your hits with a row of "=" the same length as your section-title.

Just a few ideas,

-tim



Reply via email to