The first answer to this question describes a method for highlighting all identifiers starting with an at sign. I want to do the same thing, just with an underscore instead. I've modified the code given there accordingly
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\makeatletter
% ``state variables''
\newif\ifincomment\incommentfalse
\newif\ifinstring\instringfalse
% language and style definition
\lstdefinestyle{mybash}
{%
language = bash,
basicstyle = \ttfamily,
keywordstyle = \color{blue},
}[keywords,strings,comments]
% --- patch to automatically highlight identifier starting by @
% (only outside strings and comments, though) ---
\lst@AddToHook{Output}{\@ddedToOutput}
\lst@AddToHook{Endgroup}{\incommentfalse\instringfalse}
% local variables
\newif\if@identifierStartsByAt@
\newcount\currentchar
\def\splitfirstchar#1{\@splitfirstchar#1\@nil}
\def\@splitfirstchar#1#2\@nil{\gdef\@testChar{#1}}
\def\@testChar%
{%
% copy the first token in \the\lst@token to \@testChar
\expandafter\splitfirstchar\expandafter{\the\lst@token}%
%
% reset switch
\@identifierStartsByAt@false
%
% equality test
\expandafter\ifnum\expandafter`\@testChar=`_%
\@identifierStartsByAt@true % if equality, set switch to true
\fi
%
% apply class style if not within string or comment
\if@identifierStartsByAt@
\ifincomment
\else
\ifinstring
\else
\def\lst@thestyle{\lst@keywordstyle}%
\fi
\fi
\fi
}
\let\@ddedToOutput\@testChar
\makeatother
\begin{document}
\begin{lstlisting}[style=mybash]
# This is an example
@Blueword(foo) _Blueword bar
\end{lstlisting}
\end{document}
However, this code gives me the following errors:
! Improper alphabetic constant.
<to be read again>
\lst@um_
l.62 @Blueword(foo) _Blueword
bar
?
! Missing = inserted for \ifnum.
<to be read again>
\char
l.62 @Blueword(foo) _Blueword
bar
?
! Missing number, treated as zero.
<to be read again>
\char
l.62 @Blueword(foo) _Blueword
bar
?
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./t.aux) )
(\end occurred when \ifnum on line 62 was incomplete)
(\end occurred when \iftrue on line 62 was incomplete)
(\end occurred when \ifnum on line 62 was incomplete)
(\end occurred when \iffalse on line 62 was incomplete)
(\end occurred when \ifnum on line 62 was incomplete)
(\end occurred when \iftrue on line 62 was incomplete)
(\end occurred when \ifnum on line 62 was incomplete)
(\end occurred when \iftrue on line 62 was incomplete)
(\end occurred when \ifnum on line 61 was incomplete)
(\end occurred when \iftrue on line 61 was incomplete)
(\end occurred when \ifnum on line 61 was incomplete)
(\end occurred when \iffalse on line 61 was incomplete)
(\end occurred when \ifnum on line 61 was incomplete)
(\end occurred when \iffalse on line 61 was incomplete)
(\end occurred when \ifnum on line 61 was incomplete)
(\end occurred when \iffalse on line 61 was incomplete)

listingsversion. – siracusa Jun 24 '19 at 13:31