I've setup the Base16 color theme, including Base16-shell and Base16-vim, ( for my terminal (iTerm2) and Neovim. So that I can change themes from the command line I created a bash function that lets me say color <theme>
and the base16-shell script for that theme is executed, and the terminal colors changed. This bash function also updates a file, .nvim_background
, which is sourced by my Neovim configuration, ~/.config/nvim/init.vim
. I would like the colors to change in any open Neovim session when that session gains focus after the color change.
Here is the .nvim_background
file:
if !exists('g:colors_name') || g:colors_name != 'base16-chalk'
colorscheme base16-chalk
endif
And the .nvim_template
used to modify it:
if !exists('g:colors_name') || g:colors_name != 'placeholder'
colorscheme placeholder
endif
Here is the function and augroup from my Neovim init.vim
file:
" Base16 setup
function s:CheckColorScheme()
colorscheme base16-default-dark
if filereadable("~/.config/nvim/.nvim_background")
let base16colorspace=256
source ~/.config/nvim/.nvim_background
endif
endfunction
" if v:progname !=# 'vi'
if has('autocmd')
augroup MyAutocolor
autocmd!
autocmd FocusGained * call s:CheckColorScheme()
augroup END
endif
" call s:CheckColorScheme()
" endif
The bash function causes the color change in the terminal to happen immediately, and I can see that the .nvim_background
file is getting updated with the new theme name. But when I move focus back to Neovim, the colors do not change. I can do :color base16-<theme>
and change the colors in Neovim, so I know the base16 theme plugin is properly installed.
How do I setup the augroup
so that it fires when a FocusGained
event happens? Or maybe a better question is, how do I setup the s:CheckColorScheme
function so that it is called anytime Neovim regains focus?
For completeness sake, here is the bash function that manipulates the colors:
# Base16 helpers {{{
#
# set -x
BASE16_DIR=~/.config/base16-shell/scripts
BASE16_CONFIG=~/.config/.base16
BASE16_NVIM_TEMPLATE=~/.config/nvim/.nvim_template
BASE16_NVIM=~/.config/nvim/.nvim_background
BASE16_DEFAULT="base16-material-darker.sh"
color() {
SCHEME="$1"
if [ $# -eq 0 -a -s "$BASE16_CONFIG" ]; then
cat ~/.config/.base16
return
fi
if [[ "$SCHEME" = 'help' ]]; then
ACTION='help'
elif [[ "$SCHEME" = 'default' ]]; then
ACTION='default'
else
ACTION='set'
fi
case "$ACTION" in
set)
if [[ "$SCHEME" = 'default' ]]; then
FILE="$BASE16_DIR/base16-material-darker.sh"
NVIM='base16-material-darker'
else
FILE="$BASE16_DIR/base16-$SCHEME.sh"
NVIM="base16-$SCHEME"
fi
if [[ -x "$FILE" ]]; then
echo "$SCHEME" >| "$BASE16_CONFIG"
"$FILE"
sed "s/placeholder/$NVIM/g" "$BASE16_NVIM_TEMPLATE" >| "$BASE16_NVIM"
else
echo "Scheme '$SCHEME' not found in $BASE16_DIR"
return 1
fi
;;
default)
FILE="$BASE16_DIR/$BASE16_DEFAULT"
if [[ -x "$FILE" ]]; then
echo "$BASE16_DEFAULT" >! "$BASE16_CONFIG"
"$FILE"
fi
;;
help)
echo 'color set [ocean|grayscale|material-darker|default|...]'
echo
echo 'Available schemes:'
find ~/.config/base16-shell -name 'base16-*.sh' | \
sed -E 's|.+/base16-||' | \
sort | \
column
;;
*)
echo 'Unknown subcommand: use one of {default,set,help}'
;;
esac
}
# }}}