[Tex/LaTex] How insert a character at the begin of every line from a source code

inputlistingsmintedstringsxstring

I'm writing a lecture with many source codes for example. The codes are stored in severus different files. I use listing and minted packages for formatting the sources.

I would like add character at the begin of every line of some codes like:

  • Bash shell ($ or #)
  • Python shell (>>>)
  • Matlab (>>)
  • Other shells (>)

I tried some solutions which have given on:

But both solutions have the same problem when I try:

\begin{foo_environment} 
    \input{bar.txt} 
\end{foo_environment}

In both solutions the linebreaking are ingnored

I have tried executing a bash command:

sed 's/^/$ /' bar.sh > bar.txt

This works locally but I can't do the same in Overleaf and Sharelatex

This is a sample code bar.sh:

cd ..
mount /dev/sda3 /mnt/
cd ~/.ssh/
ll
cd ~
ls -al

The result must be:

$ cd ..
$ mount /dev/sda3 /mnt/
$ cd ~/.ssh/
$ ll
$ cd ~
$ ls -al

Best Answer

With listings you can redefine the numberstyle:

\documentclass{article}
\usepackage{listings}

\lstset{
  language=tex,
  basicstyle=\footnotesize\ttfamily\selectfont,
  keepspaces=true,
  numbers=left,
  numbersep=5pt,
  numberstyle=\numberwithprompt,
}

\newcommand{\lstprompt}{>>>}
\newcommand\numberwithprompt[1]{\footnotesize\ttfamily\selectfont#1 \lstprompt}


\begin{document}
\begin{lstlisting}
a
b
c
\end{lstlisting}
\end{document}

enter image description here

Related Question