# Custom line-breaks in minted environment

I'm using the minted package for my code listings. I'd like to customize the line-breaks instead of using the breaklines option that does the line-breaking automatically. Unfortunately I haven't found a proper solution yet. I also experimented with escapeinside without any success. :(

So in my case I have the following minted environment:

\begin{minted}[bgcolor=bg, breaklines]{bash}
docker run -it --net rc --name hubot -e ROCKETCHAT_URL=rocketchat.rc:3000 -e ROCKETCHAT_ROOM='' -e LISTEN_ON_ALL_PUBLIC=true -e ROCKETCHAT_USER=<username> -e ROCKETCHAT_PASSWORD=<password> -e ROCKETCHAT_AUTH=password -e BOT_NAME=bot -e EXTERNAL_SCRIPTS=hubot-pugme,hubot-help,hubot-diagnostics rocketchat/hubot-rocketchat
\end{minted}


And I'd like to set line-breaks manually... maybe by adding a ? or some other token at the position I want to have the line break. Is this possible or do you have other suggestions?

• Maybe breakafter=? Nov 21, 2018 at 20:00
• can you not simply disable automatic breaks and manually add the line breaks to your source? (that is, use the default behaviour) Nov 21, 2018 at 20:00
• breakafter=? would work of course. But it would print the additional ? also. I need a token that makes the line break but that won't get printed :)
– Timo
Nov 21, 2018 at 22:18
• If I would add line breaks to the source code it would be semantically incorrect. The whole line is one command. I cannot cut it somewhere. If I would do so the line numbering would also be incorrect.
– Timo
Nov 21, 2018 at 22:20
• Bash command lines can be broken at a backslash trailing the line. This would be semantically correct. Nov 21, 2018 at 23:12

The example below should give you what you want, or at least a good place to start. This uses breaklines to enable line breaking, but disables breaking at spaces by using showspaces to enable a custom space-replacement character, and then setting that to a non-breaking space. Currently, ? becomes \linebreak. Depending on how you use this, it might be worth looking into \allowbreak.

\documentclass{article}

\usepackage{minted}

\AtBeginEnvironment{minted}{%
\catcode?\active
\begingroup\lccode~=\?\lowercase{\endgroup\def~{\linebreak}}%
}

\begin{document}

\begin{minted}[breaklines, showspaces, space=~]{bash}
docker run -it --net rc --name hubot ?-e ROCKETCHAT_URL=rocketchat.rc:3000 ?-e ROCKETCHAT_ROOM='' ?-e LISTEN_ON_ALL_PUBLIC=true -e ROCKETCHAT_USER=<username> ?-e ROCKETCHAT_PASSWORD=<password> ?-e ROCKETCHAT_AUTH=password ?-e BOT_NAME=bot ?-e EXTERNAL_SCRIPTS=hubot-pugme,hubot-help,hubot-diagnostics ?rocketchat/hubot-rocketchat
\end{minted}

\end{document}

• Just found this, seems to be the only way to force a line break in bash code… am I correct in that conclusion? Everything else I've tried fails because the bash lexer considers backslashes as strings. How much of that block is necessary if I just want to force line breaks at ? characters? Apr 20, 2020 at 17:13
• @DustinWheeler You would need all of this if you want to specify break locations with ? rather than allowing breaks at spaces or elsewhere. Apr 20, 2020 at 20:16
• I haven't used \begingroup/\endgroup before… I don't quite understand how this works. I think that it's taking the question mark and turning it into the lowercase definition for a tilde… but why the \lowercase{ inside the group? Is there a resource to read on groups and the use of \lccode`? Apr 23, 2020 at 14:49
• @DustinWheeler Maybe look at tex.stackexchange.com/a/7372/10742, or search through some of the other questions/answers that use this approach Apr 23, 2020 at 14:58
• Will do, thanks! Apr 24, 2020 at 17:46