Extend a language with additional keywords?

It seems that morekeywords also works for extending a language. For instance the Python keyword super isn't in the list of default keywords. In my preamble, I can set it as a keyword by doing:

\lstset{language=Python}
\lstset{
 morekeywords={super}
}

Of course, it would be ideal to contribute those missing keywords to the listings package.


You can define the keywords in a separate file and just include it in your preamble. Here is an example of adding for and downto as new keywords:

enter image description here

\documentclass{article}
\usepackage{listings}%
\usepackage{xcolor}

\lstset{%
    backgroundcolor=\color{yellow!20},%
    basicstyle=\small\ttfamily,%
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
    }%

% Add your keywords here, and have this in a separate file
% and include it in your preamble
\lstset{emph={%  
    downto, for%
    },emphstyle={\color{red}\bfseries\underbar}%
}%

\begin{document}
\begin{lstlisting}
y = 0
for i = n downto 0
    y = a_i + x * y
\end{lstlisting}
\end{document}

As Dominique said,

\lstset{language=Python}
\lstset{
morekeywords={super}
}

works, but doing so firstly add the Python language to all listing and then adds the keyword to all languages.

To add the keyword only for one language use

\lstdefinestyle{Python}{
morekeywords={super}
}

instead.