How is verbatim implemented?

Let's begin with an excerpt from latex.ltx (the LaTeX "kernel"):

\def\verbatim{\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\def\endverbatim{\[email protected] \leavevmode\fi\endtrivlist}
\def\[email protected]{\normalfont\ttfamily}

The instruction \@verbatim does most of the heavy lifting. It is defined as follows (also in latex.ltx, right before \def\verbatim...):

\def\@verbatim{\trivlist \item\relax
  \[email protected]\else\vskip\parskip\fi
  \leftskip\@totalleftmargin\rightskip\[email protected]
  \parindent\[email protected]\parfillskip\@flushglue\parskip\[email protected]
  \language\[email protected]
    \[email protected]
      \leavevmode \null \@@par\penalty\interlinepenalty
  \let\do\@makeother \dospecials
  \obeylines \[email protected] \@noligs
  \everypar \expandafter{\the\everypar \unpenalty}%

There's a lot going on, isn't there? The most important instructions are as follows. First, LaTeX enters a trivlist environment (which will be terminated by \endtrivlist, at the end of \endverbatim). Unless we're in a minipage environment, a vertical skip in the amount of \parskip is performed. After executing a paragraph break (via \@@par) and suppressing all hyphenation, \par is redefined locally. The single most important set of instructions is let\do\@makeother \dospecials. They set the catcodes of \ (space), \ (backslash), {, }, $, &, #, ^, _, %, and ~ to "other" (12). Essentially, all TeX-special characters are not special inside a verbatim environment. Next, TeX is instructed to obey line breaks, switch the font to monospaced (aka "teletype" or tt), and activate some characters in order to break ligatures such as << for «. The standard paragraph-builder penalty for line breaks is unset. Wow -- we've made it to the end of the \@verbatim code block.

Next, \frenchspacing (a plain-TeX command that's replicated in the LaTeX kernel) ensures that no extra space is inserted after punctuation marks. In comparison, the chores handled by \endverbatim are nearly trivial: The most important instruction is \endtrivlist, which closes the trivlist environment that was opened at the start of \@verbatim.

The trivlist environment is a very important tool in LaTeX. It occurs quite a few times in latex.ltx. For example, the center, flushleft, flushright environments are all set up as trivlists, and the list environment employs trivlist internally. The enumerate, itemize, and description environments employ list environments internally and thus, ultimately, trivlist environments as well.

For example

 verbatim text & $ #
 in more    lines... 

can be implemented as

      ... set all special catcodes to other
      ... set space as active and define it as \space
      ... set ^^M as active and define it as \par
\def\beginverbatimA #1<tricky of \endverbatim separator>{%
    \endgroup % restores normal catcodes

Of course, this is only pseudocode. We need to define \beginverbatimA with parameter #1 separated by \endverbatim where this separator uses \ as normal character with catcode "other". It needs some "tricky" code. For example, like this:


The main idea is presented here, not the whole implementation.

Edit: A LaTeX-notice: second answer here specializes to LaTeX but one thing is missed there: how the \@xverbatim works and how it is implemented. This is analogue to \endverbatimA mentioned in my answer, but the separator of #1 parameter must be \end{verbatim} where not only \ but above that the { and } must have catcode "other". This can be done, for example by:


but you can see in latex.ltx that another approach was used here.