Ignore specific lines when numbering lines

I think for this task it is easier to drop the lineno package and implement the line number typesetting with the verbatim package. This is based on an example from the verbatim package documentation, p. 5. Edit: I modified the solution to resume the numbering from before it was switched off; I assume that's the behavior you want.

\usepackage{verbatim}

\makeatletter
\newif\ifprintlineno
\newcounter{reallineno} % the line number in the input
\newcounter{lineno} % the line number displayed

\newtoks\testlinenotoks
\testlinenotoks={\printlinenotrue}
\def\testlineno{\the\testlinenotoks}

\def\verbatim@processline{%
  \addtocounter{reallineno}{1}
  \testlineno
  \ifprintlineno
    \addtocounter{lineno}{1}
    \leavevmode\llap{\scriptsize\thelineno\hskip1em\hskip\@totalleftmargin}%
    \the\verbatim@line\par
  \else\the\verbatim@line\par\fi
}
\edef\verbatim@finish{\verbatim@finish
  \setcounter{lineno}{0}\setcounter{reallineno}{0}}
\makeatother

\newcommand\SuppressLineNoFromTo[2]{%
  \testlinenotoks=\expandafter{\the\testlinenotoks
  \ifnum\value{reallineno}>\numexpr#1-1\relax
  \ifnum\value{reallineno}<\numexpr#2+1\relax
    \printlinenofalse\fi\fi}}
\newcommand\SuppressLineNoAt[1]{%
  \testlinenotoks=\expandafter{\the\testlinenotoks
  \ifnum\value{reallineno}=#1
  \printlinenofalse\fi}}
\newcommand\ClearSuppressLineNo{\testlinenotoks={\printlinenotrue}}

This defines three commands:

  • \SuppressLineNoFromTo will suppress the line numbering from line number #1 to line number #2 in all verbatim environments
  • \SuppressLineNoAt will suppress the line number at line #1 only
  • \ClearSuppressLineNo clears all the rules defined with the two commands above if you want to use different sets of rules with different environments

You can combine them in various ways. The command \verbatim@processline handles how each line in a verbatim environment is typeset, and the \llap part sets the line number into the margin, so adaptations should go there.

Here is an example assuming the above setup:

\SuppressLineNoFromTo{5}{10}
\SuppressLineNoFromTo{15}{16}
\SuppressLineNoAt{1}

\begin{document}

\begin{verbatim}

A
B

C


D
E
F
G
H
I
J


\end{verbatim}
\end{document}

example


Old answer:

I don't know if this is what you are looking for, but with this setup here

\usepackage{lineno}
\usepackage{verbatim}

{\catcode`\%=13
\gdef\getnextchar#1\par{\setbox0 \hbox{\ifx%#1\gdef\nextchar{c}\else\ifx!#1\gdef\nextchar{!}\else\gdef\nextchar{?}\fi\fi}}
\gdef\ignorenext#1{}
\gdef\recat{\catcode`\%=13
  \long\def%##1\par{\getnextchar##1\par\if\nextchar c\%\fi\if\nextchar!\%\ignorenext##1\par\else
    {\nolinenumbers##1\par}\fi}}
\globaldefs=1 
\newenvironment{myverbatim}{\verbatim\recat%}{\endverbatim}}

you get an environment myverbatim within which the following works:

  • a comment character % is invisible in the output and suppresses the line number of the current line
  • the combination %% creates a verbatim comment that will be visible in the output, the line will not have a number
  • the combination %! creates a comment where the line will have a number

Example:

\documentclass{article}
\usepackage{lineno}
\usepackage{verbatim}

{\catcode`\%=13
\gdef\getnextchar#1\par{\setbox0 \hbox{\ifx%#1\gdef\nextchar{c}\else\ifx!#1\gdef\nextchar{!}\else\gdef\nextchar{?}\fi\fi}}
\gdef\ignorenext#1{}
\gdef\recat{\catcode`\%=13
  \long\def%##1\par{\getnextchar##1\par\if\nextchar c\%\fi\if\nextchar!\%\ignorenext##1\par\else
    {\nolinenumbers##1\par}\fi}}
\globaldefs=1 
\newenvironment{myverbatim}{\verbatim\recat%}{\endverbatim}}

\begin{document}
\begin{linenumbers}
\begin{myverbatim}
%
A
B
%this input will be visible, the line does not have a number
just like this one %
C
%% this comment will show up, without line number
D %% like this one

E
%! this comment has a line number
F %! like this one
G

H
%
%
\end{myverbatim}
\end{linenumbers}
\end{document}

old example