Neovim Remote


Situation

I have a bash script that I run to swap out my system colorscheme (dark mode vs light mode) so I don’t have to use the settings gui. In this script I also update my tmux and I want it to also update my neovim config, but I want it to change all my current neovim instances and any future instances.

Previously I was just letting my neovim colorscheme be completely inherited from my terminal colorscheme, which kept everything very simple (didn’t have to update anything on the neovim side).

But after a couple weeks of that, the way neovim inherits the colors just wasn’t great (the syntax handling for whatever reason isn’t as good). So you get nicer colors if you just find the same colorscheme for your terminal and neovim and apply them separately.

Which unfortunately made my setup a little more complex … but usage/maintenance is still pretty straightforward.

Final script (as of today)

#!/bin/bash

SCHEME=$(gsettings get org.gnome.desktop.interface color-scheme)
if [[ $SCHEME == "'prefer-light'" ]]; then
	gsettings set org.gnome.desktop.interface color-scheme prefer-dark
	tmux set -g status-bg black
	tmux set -g status-fg white
	cp ~/.config/alacritty/alacritty-dark.toml ~/.config/alacritty/alacritty.toml
	cp ~/.config/nvim/colorscheme-dark.lua ~/.config/nvim/plugin/colorscheme.lua
	ls /run/user/1000 | grep nvim | xargs -I{} nvim --server /run/user/1000/{} --remote-send '<Esc>:set background=dark<cr>'
else
	gsettings set org.gnome.desktop.interface color-scheme prefer-light
	cp ~/.config/alacritty/alacritty-light.toml ~/.config/alacritty/alacritty.toml
	cp ~/.config/nvim/colorscheme-light.lua ~/.config/nvim/plugin/colorscheme.lua
	tmux set -g status-bg white
	tmux set -g status-fg black
	ls /run/user/1000 | grep nvim | xargs -I{} nvim --server /run/user/1000/{} --remote-send '<Esc>:set background=light<cr>'
fi

Step 1: changing the system theme

I simply just searched for using the terminal to change the system theme on Ubuntu to go from dark mode to light mode.

gsettings set org.gnome.desktop.interface color-scheme prefer-dark

Step 2: changing the tmux theme

I’m not really using a full tmux theme right now, so I just update the background and foreground colors accordingly.

tmux set -g status-bg black
tmux set -g status-fg white

Step 3: changing the alacritty theme

For this I have two files for each mode, and then I just copy the file on top of the default alacritty config which will cause alacritty to automatically update itself.

cp ~/.config/alacritty/alacritty-dark.toml ~/.config/alacritty/alacritty.toml

Step 4: changing the neovim theme for new neovim instances

Similarly to alacritty’s config, I have two separate neovim files which simply set the colorscheme and the background and then I copy the appropriate one over the plugin file that gets loaded.

In my neovim config folder I have a file called “colorscheme.lua” which will automatically be loaded by neovim on startup because it’s a lua file inside that folder. It’s a neovim convention.

vim.cmd [[colorscheme catppuccin]]
vim.cmd [[set background=dark]]

Currently the only difference I have for light mode and dark mode is the value “dark” is switched for “light” because my current colorscheme supports both and I’m using the same colorscheme for alacritty as well.

Step 5: changing the neovim theme for existing neovim instances

Now I want all current neovim instances to also be updated. This was the trickiest part. First I need to get all the current neovim sessions. I found out that on my ubuntu system, neovim by default spins up a new socket/server per instance in the /run/user/1000 directory. You can find out the current server location by running this command in any neovim session:

:echo v:servername

I get something like this as a response:

/run/user/1000/nvim.30857.0

So I list all everything in that directory and grep for nvim, and then I pipe those results using xargs into a neovim command for remotely controlling a given neovim instance.

ls /run/user/1000 | grep nvim | xargs -I{} nvim --server /run/user/1000/{} --remote-send '<Esc>:set background=dark<cr>'

Conclusion

Now I can run my little helper script that I have in my system’s PATH and swap between light mode and dark mode whenever. I may update things in the future to update my tmux and bash colorschemes as well, but I imagine it’ll follow a similar pattern/convention of having two separate files for each mode and then just copying the appropriate file over the default config when swapping. But for now I’m quite pleased.

Bonus

I had to also tweak my browser’s (Brave) Appearance settings to get them to respond to the system setting. Need to choose the “Classic” theme.

Brave > Settings > Appearance

Brave colors: “Same as Linux” Theme: Classic