Change colorscheme depending on mode

Hello!

I am very new to kakoune, but I love it so far.
I’m having some trouble remembering in which mode I am (because I’m also new to modal editing).

I wonder, as a feature request, to be able to use some colorscheme for normal mode, and another for insert mode.

If it is already available, please show me how, because I cannot find thru documentation.

hook global ModeChange push:[^:]*:insert %{ colorscheme solarized-light }
hook global ModeChange pop:insert:.* %{ colorscheme solarized-dark }

although you might prefer a less drastic change in colours, for example just changing the background colour

set-face global Default default,<a-new-background-color-here>
2 Likes

Really thank you, @prion
Specially because of your prompt response!

Yes, you are right… I was wondering to switch between gruvbox dark and light, but changing the background only is a better idea.

Just as an update.

I slightly modified the gruvbox-dark and renamed it (just changed the background color). And that new theme is what I used as you showed me using hooks.

Thanks again, @prion

@prion, would you write an example of a practical use of this?
I cannot get it to work for changing the background color when in INSERT mode.
Do I have to put

set-face global Default default <some-front color?>,<a-new-background-color-here>

Well, it didn’t work :frowning:
What am I doing wrong?


Another question. If instead of changing the background color when entering in INSERT mode, I would like to change it when in NORMAL mode, and keep the original colorscheme in INSERT mode, do I just need to change insert with normal in this pair of hooks?

From:

hook global ModeChange push:[^:]*:insert %{ colorscheme solarized-light }
hook global ModeChange pop:insert:.* %{ colorscheme solarized-dark }

To:

hook global ModeChange push:[^:]*:normal %{ colorscheme solarized-light }
hook global ModeChange pop:normal:.* %{ colorscheme solarized-dark }

Well, it didn’t work :frowning:

If you run the following command interactively:

set-face global Default default,red

…does the buffer background change to red? If not, do you get an error message (check the *debug* buffer to be sure)?

If the background colour changes, you should be able to try the following:

# Whenever a window switches to insert mode,
# override its Default face to have a red background
hook global ModeChange .*:insert %{ set-face window Default default,red }
# Whenever a window leaves insert mode,
# remove its override of the Default face
hook global ModeChange .*:insert:.* %{ unset-face window Default }

…do I just need to change insert with normal in this pair of hooks?

Kakoune doesn’t just have a “current mode”, it has a “mode stack”. The top of the stack is “normal mode”, you can add an “insert” mode by pressing i, and from insert mode you can add another temporary normal mode with <a-semicolon>. From that temporary normal mode you can add another insert mode with i, and keep on going until you have as many nested modes as you like.

The original hooks given by Prion would set the colorscheme to “solarized-light” every time an insert mode was entered and set the colorscheme to “solarized-dark” every time an insert mode was exited (even if there were still more insert modes still in effect). Changing those hooks from “insert” to “normal” wouldn’t do what you want, since <a-semicolon> is the only way to push a new normal mode, and it’s hardly ever used outside scripting.

The hooks in my example above only care about “is insert the current mode” or “is insert mode no longer current”, and not about whether we got there by pushing or popping. so it should be safe to edit them from “insert” to "normal. However, a buffer usually starts in normal mode, so neither of those editing hooks would be fired, and your colours wouldn’t change until after you’d entered and left insert mode for the first time. Making the changes in response to entering/leaving insert mode just makes things a bit tidier.

1 Like

Thanks, @Screwtapello !

Now it works! I was not using default,red syntax, but another-color,red.

And you hooks are easier to maintain and tidier, indeed. No need to modify colorschemes. Your explanation helped me get clarified about the Kakoune behavior.