I know there are already many questions there covering that or at least similar topics. I've read this for example but can't get it figured out...
At first here comes my MWE:
\documentclass{scrartcl}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}% not needed, but not sure if it has any effect
\newcommand{\lstCapLbl}[2]{%
\vspace{-0.9\baselineskip}
\captionof{listing}{#1\label{lst:#2}}
\vspace{0.9\baselineskip}
}
\begin{document}
There is some random text here. And some some some some some some some
some some some some some more to get a line full.
\begin{minted}[gobble=1,linenos,tabsize=4,xleftmargin=0.7cm]{js}
function myFunc(){
someCode();
// make it as long as needed to get the page full
// in my case 35 more lines were needed
// but they were omitted here for the sake of brevity
}
\end{minted}
\lstCapLbl{some caption text}{andTheLabel}
And again some random Text just for fun.
\end{document}
What you get if you insert the right number of rows looks like that:

But what I'd like to get is something like that (I just added some more lines for the showcase):

The last lines of the minted block should go on the next page! As you probably guess, I don't want to put the code into a minipage environment or something since it should span over multiple pages.
What I tried so far is putting \nopagebreak at every possible place. It doesn't seem to have any effect.
\newcommand{\lstCapLbl}[2]{%
\nopagebreak
\vspace{-0.9\baselineskip}
\nopagebreak
\captionof{listing}{#1\label{lst:#2}}
\vspace{0.9\baselineskip}
}
I also put it between the minted environment and the \lstCapLbl. I also tried to play with widowpenalty and clubpenalty but that didn't help either.
\end{minted}
\widowpenalty=10000
\clubpenalty=10000
\lstCapLbl{some caption text}{andTheLabel}
Since I was on holiday in the final time of the bounty, I unfortunately couldn't award you the bounty. But I read that you get the bounty afterwards if you give the highest voted answer. I could even start a new bounty and award it, I don't know...
– caligula Aug 04 '15 at 08:17