How do I print with folds

66 views
Skip to first unread message

Andrew Chalmers

unread,
Jun 23, 2008, 8:25:33 PM6/23/08
to v...@vim.org
I have a source file I want to print an extract of, for review or presentation
purposes. From time to time I've had a need for this. I want the printout to
show that I have removed certain sections. But I don't want to create an edited
version of the file every time I want a particular extract. So I used folds to
hide the unwanted sections leaving only the fold markers.
But when I printed out the file the folded section was displayed in full.

I searched the vim help but couldn't anything connecting folds and hard copy or
printing.

Can someone tell me if or how I can generate a printout from vim that will
include only the text I want and replace the rest with markers, something like
those used for folds.

Currently using vim 7.1.

Cheers,

Andrew Chalmers.


Tony Mechelynck

unread,
Jun 23, 2008, 9:00:48 PM6/23/08
to vim...@googlegroups.com, v...@vim.org

:help :TOhtml

This function can be used with a range, converts your file to HTML (in a
new window), showing the syntax highlights and displaying the folds the
way they are.

Then you can load the result in your favourite browser and print from there.


Best regards,
Tony.
--
If you had any brains, you'd be dangerous.

Andrew Chalmers

unread,
Jun 24, 2008, 1:21:25 AM6/24/08
to vim...@googlegroups.com

Thanks Tony.

I don't suppose there is a plain text equivalent, to save me having to load up a
browser?


Thanks,

Andrew.

Tony Mechelynck

unread,
Jun 24, 2008, 1:47:39 AM6/24/08
to vim...@googlegroups.com

AFAIK, there isn't.

Best regards,
Tony.
--
Occident, n.:
The part of the world lying west (or east) of the Orient. It
is largely inhabited by Christians, powerful sub-tribe of the
Hypocrites, whose principal industries are murder and cheating, which
they are pleased to call "war" and "commerce." These, also, are the
principal industries of the Orient.
-- Ambrose Bierce, "The Devil's Dictionary"

Andreas Politz

unread,
Jun 24, 2008, 4:29:31 AM6/24/08
to vim...@googlegroups.com

I once posted this in a different topic. The function returns
a list of lines of the current buffer with respect to folds.

func! FoldedToText( )
let res = []
let l = 1
while l <= line('$')
if foldclosed(l) != -1
let res+= [ foldtextresult(l) ]
let l = foldclosedend(l)
else
let res+= [ getline(l) ]
endif
let l+=1
endwhile
return res
endfun

call writefile(FoldedToText(),'/tmp/bar')

-ap

Christian Brabandt

unread,
Jun 24, 2008, 4:38:40 AM6/24/08
to vim...@googlegroups.com
Am Di, 24.06.2008, 02:25, schrieb Andrew Chalmers:
> I have a source file I want to print an extract of, for review or
> presentation
> purposes. From time to time I've had a need for this. I want the printout
> to
> show that I have removed certain sections. But I don't want to create an
> edited
> version of the file every time I want a particular extract. So I used
> folds to
> hide the unwanted sections leaving only the fold markers.
> But when I printed out the file the folded section was displayed in full.

Well I guess you could use a function that would return all non-folded
text. Something like this might actually work for you:

function CopyNonFolded()
let lnum=1
let buffer=[]
while lnum <= line("$")
if (foldclosed(lnum) == -1)
let buffer += getline(lnum, lnum)
let lnum += 1
else
let buffer += [ foldtextresult(lnum) ]
let lnum = foldclosedend(lnum) + 1
endif
endwhile
top new
set bt=nofile
call append(".",buffer)
0d_
endfu

This function will copy all non folded text into a new buffer, from
which you could easily print the text.

Note, that I do write vim functions only occasionally, so there might be
better ways to do what you like to achieve.

regards,
Christian

Benjamin Fritz

unread,
Jun 24, 2008, 8:42:56 AM6/24/08
to vim...@googlegroups.com

Before writing complicated functions, keep in mind the :folddoopen and
:folddoclosed commands.

I'm not sure of an exact method to use them for this application, but
I'm sure they'll be useful.

Reply all
Reply to author
Forward
0 new messages