2

On my laptop, I frequently swap between a dark and a bright colorscheme for my terminal. I do this by symlinking its configuration file and asking it (termite) to read it without closing using killall -USR1 termite.

I'd like my running neovims to pick up the change somehow, and set background to either dark or light. I've visited this answer and this issue and other places in an effort to find out how to "refresh" neovim. And I'd like to avoid using packages like neovim-remote if possible.

nvim --version

NVIM v0.3.2-964-g7e97587da
Build type: RelWithDebInfo
LuaJIT 2.0.5
Compilation: /usr/bin/cc -march=x86-64 -mtune=generic -O2 -pipe
  -fstack-protector-strong -fno-plt -Wconversion -O2 -g -DMIN_LOG_LEVEL=3 
  -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes 
  -std=gnu99 -Wimplicit-fallthrough -Wvla -fstack-protector-strong 
  -fdiagnostics-color=auto -Wno-array-bounds 
  -DINCLUDE_GENERATED_DECLARATIONS 
  -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM 
  ...

Features: +acl +iconv +jemalloc +tui 
See ":help feature-compile"
5
  • Must it happen automatically (nicer, but potentially hard to do) or can it be triggered manually (e.g. via a mapping/command; much easier, but requires an extra step)?
    – D. Ben Knoble
    Dec 19, 2018 at 18:50
  • I'd like it to happen automatically. But your suggestions are welcome.
    – Bart
    Dec 20, 2018 at 17:45
  • 1
    I also don't mind patching the source. It would be nice to have an equivalent to vim's termresponse mechanism. But it's perhaps more useful to be able automatically call a function when neovim receives SIGUSR1.
    – Bart
    Dec 21, 2018 at 10:52
  • @Bart Adding a VimL event/autocmd for SIGUSR1 is a good idea, would you mind creating a ticket: github.com/neovim/neovim/issues Jan 28, 2019 at 14:57
  • @JustinM.Keyes thanks, apparently the idea is received well: github.com/neovim/neovim/pull/9564
    – Bart
    Feb 1, 2019 at 13:41

1 Answer 1

1

Since a new "Signal" autocmd was added, we can automatically call a function when neovim receives SIGUSR1. Which is nice.

$ readlink -qn ~/.config/termite/config
/home/bart/.config/termite/config_dark

I now have this in my init.vim

" ~/.config/nvim/init.vim
" ...
au Main Signal * call Darkorlight('~/.config/termite/config')

function! Darkorlight(file) abort
  if getftype(expand(a:file)) != 'link'
    return
  endif
  if resolve(glob(expand(a:file))) =~ '_dark$'
    set background=dark
  else
    set background=light
  endif
  redraw
endfunction

I then watch for the symlink to change with this unit:

# ~/.config/systemd/user/termite.path
[Unit]
Description=Make termite reload its configuration file when it changes

[Path]
PathChanged=%h/.config/termite

[Install]
WantedBy=default.target
# ~/.config/systemd/user/termite.service
[Unit]
Description=Send a signal to all termite and neovim processes

[Service]
Type=oneshot
ExecStart=/usr/bin/killall -USR1 termite
ExecStart=/usr/bin/killall -USR1 nvim

[Install]
WantedBy=default.target

This works great. It comes in handy as a toggle switch (i.e. for a toolbar):

#!/bin/sh
conf=~/.config/termite/config
if [[ `readlink $conf 2>/dev/null` =~ light ]]; then
  ln -sf "${conf}_dark" "$conf"
else
  ln -sf "${conf}_light" "$conf"
fi

Thanks to Justin M. Keyes for encouraging me to ask for a scriptable Signal feature on Github, and to Marco Hinz for implementing it so quickly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.