Use Whitespace face in Strings?

I personally use a very dark gray to view whitespace in my files. But when the whitespace appears in a string, it uses the string’s color. Is there any way for me to use whitespace color (gray) instead of the string color (green)?


I believe Kakoune applies highlighters like paint, in the order that they’re defined. So if one highlighter says to paint a character red, then the next highlighter says to paint the same character green, it winds up green. If your show-whitespace highlighter is defined globally, and your syntax highlighter is installed per-window (following convention), then I think the syntax highlighter will always be applied second, and therefore always result in whitespace-inside-strings being drawn in the string colour, not the whitespace colour.

As an experiment, you might load a file (like the example you screenshot), then manually remove your whitespace highlighter and re-add it at window scope, to see if that produces the effect you want. If so, you might be able to do the thing you want by installing your show-whitespace highlighter at window scope in a hook, instead of installing it globally. Maybe something like:

# Whenever a new window is created...
hook global WinCreate .* %{
    # ...wait until everything else has loaded and Kakoune is ready to accept input...
    hook -once -always window NormalIdle .* %{
        # ...and add this highlighter as the last highlighter
        add-highlighter window show_whitespace #... etc. etc.

Thanks for your help!

I think this is close to optimal plus fixes highlighting if I change the filetype:

hook global WinSetOption filetype=.* %{
	try %{ remove-highlighter window/whitespace }
	add-highlighter window/whitespace regex '\h+' "0:Whitespace"

You can keep the highlighter global and use a final face.

See show-characters.kak for example.

1 Like

What does it mean to “use a final face” or “keep the faces final”? As far as I can tell final isn’t a keyword

1 Like

It is a attribute of a face. :doc faces.

    final, override the previous face instead of merging with it, and this face will only be replaced if another face with the final attribute is applied
1 Like