Given the following piece of code:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{shell}{Verbatim}{
commandchars=\%\{\},
label=\shelltitle,
frame=single,
samepage=true,
formatcom=\setcounter{prompt}{0}\start
}
\newcommand{\shelltitle}{This is a shell}
\makeatletter
\def\start{\let\FV@FV@ProcessLine\FV@ProcessLine
\def\FV@ProcessLine{\noindent\vrule height3ex depth2ex
\hbox to\hsize{\kern\FV@FrameSep This is the shell prompt\hfil}%
\kern-.8pt\vrule\par
\let\FV@ProcessLine\FV@FV@ProcessLine
\FV@ProcessLine}%
}
\makeatother
\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}
\begin{document}
\begin{shell}
%prompt echo foo{}
foo
%prompt echo bar
bar
\end{shell}
\end{document}
The samepage=true option doesn't affect the heading line ("This is the shell prompt") and page break happens immediately after that line. How can I ensure the heading line resides on the same page of the rest of the listing?

\nobreakjust after\kern-.8pt\vrule\par? – egreg Aug 2 '12 at 13:59