# penalize page breaks in \lstlistings

I have a document with some rather lengthy \lstlisting's (source code snippets). It looks really bad if there is a page break within a listing/snippet. Thus I tried to avoid page breaks in listings by putting the listings into a minipage ( Avoid page breaks in \lstlistings ).

However, on the other hand it looks really bad if consecutive long snippets with little text in between create very underfull vboxes and pages with only a few lines of text.

So basically I want to tell LaTeX that both extremes look rather bad, and that page breaks may be used sparingly to avoid bad cases of underfull vboxes but should be avoided/penalized. Right now I can totally ban page breaks or I can allow them, but then it is assumed they are "totally OK" and used a lot when they should be a last resort.

How can I fine-tune the badness of either alternative in order to get a visually pleasing result?

-
Use the float option: \begin{lstlisting}[float]...\end{lstlisting}; the listing will not admit page breaks and will float to the best position. –  Gonzalo Medina Feb 11 at 18:23
@GonzaloMedina I have a lot of one-liners where it breaks the reading flow if I have to put a reference to a float and the reader has to search for the float. It might be a good idea to selectively allow the longer listings to float, I will try that. Floating will help the most in these cases anyways, and it will not break the reading flow that badly as the listing is longer. Thanks for the input! –  mnagel Feb 13 at 10:40
–  Jubobs Mar 2 at 13:42