# Listings style for INI (conf) files

I'm trying to create a simple style for Windows INI files using the listings package

I'd like this results:

• purple [sections] tags
• red assignment operators (= and :)
• gray comment lines starting with (; or #)

Here is how Notepad++ highlights it.

So far I was able to achieve purple [tags] and ; comments. But I can't get the assignment operators to hightlight, nor the # comments to work.

\documentclass{article}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}

\lstdefinelanguage{Ini}
{
basicstyle=\ttfamily\small,
columns=fullflexible,
tag=[s]{[]},
tagstyle=\color{Orchid}\bfseries,
usekeywordsintag=true,
morecomment=[l]{;},
alsoletter={=},
ndkeywords={=},
ndkeywordstyle=\color{green}\bfseries
}[html]

\title{INI test}
\author{Me}
\begin{document}
\maketitle
INI file highlighting test.

\begin{lstlisting}[language={Ini}]
; comment1
[section1]
var1 = val1
var2 = val2

#comment2
[section2]
var1: val1
var2: val2
\end{lstlisting}
\end{document}

• What kind of .ini files do you mean? Windows or KDE like settings? Can you provide a working tex code with an example .ini?
– user31729
Sep 9 '15 at 14:27
• I added an example and highlight from Notepad++ (similar in function, but different colors). Sep 9 '15 at 14:33
• You should use otherkeywords for symbols instead of (the deprecated) ndkeywords (I'll show you how in an answer) Sep 9 '15 at 14:46
• I think you're abusing the tag and tagstyle keys, here. You should use moredelim, instead. You also need to add morecomment=[l]{\#}. Sep 9 '15 at 14:50
• @Jubobs I tried, but it doesn't work. I suspect building on the html style is creating problems for the assignment operators, but I can't get the tags to work like that. Sep 9 '15 at 14:56

I think the best way to implement this is avoiding using the html style as a base:

\lstdefinelanguage{Ini}
{
basicstyle=\ttfamily\small,
columns=fullflexible,
morecomment=[s][\color{Orchid}\bfseries]{[}{]},
morecomment=[l]{\#},
morecomment=[l]{;},
morekeywords={},
otherkeywords={=,:},
keywordstyle={\color{green}\bfseries}
}


This satisfies the requirements but will not highlight html inside the ini's values.

• # cannot be specified as a comment delimiter without escaping it (it is a special character)
• to highlight symbols instead of changing their class it is better to use otherkeywords (which can only be used if morekeywords is used first, hence the morekeywords={} entry)
• you were abusing tags where morecomment is more than sufficient (you could also have used moredelim)