[Tex/LaTex] Listing language for “screen session”

languageslistings

I am writing a set of tutorial documents on some command-line based tools. This tutorials need to show a "screen session". Something along the lines of:

$ git status
# on branch "master"

I want to be able to:

  1. mark the shell prompt with a particular color (I always use the initial $ to indicate I'm on the prompt)
  2. Identify the command output with a different color
  3. Identify keywords in the shell commands

I was looking at the listings package and it seems to be on the right path, but I couldn't find a way to produce that kind of output. I saw the documentation on how to produce a different "language" for the listings package, but I couldn't get myself around that, since I couldn't find much documentation.

Is there a public implementation of such "language" specification? If not, can you give me some tips on how to define it?

Best Answer

This might answer some of your questions. For the prompt, you may use the literate option. Two ways are shown to color output. The first way is not very good but it gets the job done for very short and simple output. The other way is to define an output environment. The result from the two are different : when using the escape character, you are leaving the listings mode and the spacing between the letters is not the same. See the output text in red below.

\documentclass{minimal}

\usepackage{listings}
\usepackage{xcolor}

\lstnewenvironment{code}
    {\lstset{%
        basicstyle=\ttfamily,
        escapechar=~,
        morekeywords={foo,bar},
        keywordstyle=\color{green},
        literate={\$}{{\textcolor{blue}{\$}}}1}
    }
    {}

\lstnewenvironment{codeoutput}
    {\lstset{basicstyle=\ttfamily\color{red}}}
    {}

\begin{document}

\begin{code}
$ git status
# on branch "master"
foo in bar is blah

for short output
~\color{red}This is output\color{black}~
\end{code}

\begin{codeoutput}
This is output
\end{codeoutput}

\end{document}

The output is

output