Using \lstinputlisting to include a file but only certain lines or line ranges
Each "listing" environment understands the firstline
and lastline
keys:
\lstinputlisting[firstline=300,lastline=500]{file.cc}
listings supports line ranges not only with firstline
and lastline
(see egregs answer) but even with linerange={<first1>-<last1>,<first2>-<last2> …}
. Note, that first2
has to be greater than last1
and so on. So
\lstinputlisting[linerange={1-4,7-9}]{file.cc}
would be valid but
\lstinputlisting[linerange={7-9,1-4}]{file.cc}% Don't do this!!!
is not allowed!
For more information see section 4.3.3 "The printed range" at the listings manual.
As an addendum to egreg's answer: If you are typesetting line numbers and want these numbers to reflect the physical line numbers, the firstnumber
key comes at your rescue:
\lstinputlisting[numbers=left,firstnumber=300,firstline=300,lastline=500]{file.cc}