Skip to content

Instantly share code, notes, and snippets.

@ayamir
Last active February 27, 2025 03:50
Show Gist options
  • Save ayamir/6ec740dd1c99ce75bade1a7669220eab to your computer and use it in GitHub Desktop.
Save ayamir/6ec740dd1c99ce75bade1a7669220eab to your computer and use it in GitHub Desktop.
Switch kitty, tmux, and neovim theme without any restart operations.
#!/usr/bin/env bash
check_file_exists() {
if [ ! -f "$1" ]; then
exit 1
fi
}
check_macos() {
local os_name=$(uname -s)
if [ "$os_name" = "Darwin" ]; then
return 0
else
return 1
fi
}
kill_process() {
pid=$1
if [ -n "$pid" ]; then
kill -SIGUSR1 "$pid"
fi
}
nvim_setting=~/.config/nvim/lua/user/settings.lua
vscode_setting=~/.config/Code/User/settings.json
if check_macos; then
vscode_setting=~/Library/'Application Support'/Code/User/settings.json
fi
alacritty_setting=~/.config/alacritty/alacritty.toml
mode_state_file=~/.mode_switch_state
set_neovim_background() {
background=$1
servers=$(lsof -U | grep nvim | grep /run/user | awk '{print $9}')
if check_macos; then
servers=$(lsof -U | grep nvim | grep /var/folders | awk '{print $8}')
fi
for server in $servers; do
nvim --server $server --remote-send ":set background=$background<CR>"
done
}
switch_mode() {
input=$1
if [ "$input" == "light" ]; then
check_file_exists ~/.tmux.conf.light
check_file_exists ~/.config/kitty/kitty.conf.light
ln -sf ~/.tmux.conf.light ~/.tmux.conf
ln -sf ~/.config/kitty/kitty.conf.light ~/.config/kitty/kitty.conf
perl -i -pe 's/settings\["background"\] = "dark"/settings\["background"\] = "light"/' "$nvim_setting"
perl -i -pe 's/"workbench.colorTheme": "Default Dark Modern"/"workbench.colorTheme": "Default Light Modern"/' "$vscode_setting"
perl -i -pe 's/vscode_dark.toml/vscode_light.toml/' "$alacritty_setting"
kill_process $(pgrep kitty)
kill_process $(pgrep nvim)
set_neovim_background "light"
tmux source-file ~/.tmux.conf
elif [ "$input" == "dark" ]; then
check_file_exists ~/.tmux.conf.dark
check_file_exists ~/.config/kitty/kitty.conf.dark
ln -sf ~/.tmux.conf.dark ~/.tmux.conf
ln -sf ~/.config/kitty/kitty.conf.dark ~/.config/kitty/kitty.conf
perl -i -pe 's/settings\["background"\] = "light"/settings\["background"\] = "dark"/' "$nvim_setting"
perl -i -pe 's/vscode_light.toml/vscode_dark.toml/' "$alacritty_setting"
perl -i -pe 's/"workbench.colorTheme": "Default Light Modern"/"workbench.colorTheme": "Default Dark Modern"/' "$vscode_setting"
kill_process $(pgrep kitty)
kill_process $(pgrep nvim)
set_neovim_background "dark"
tmux source-file ~/.tmux.conf
else
exit 1
fi
}
get_current_mode() {
if [ -f "$mode_state_file" ]; then
cat "$mode_state_file"
else
echo "light"
fi
}
set_current_mode() {
echo "$1" >"$mode_state_file"
}
toggle_mode() {
current_mode=$(get_current_mode)
if [ "$current_mode" == "light" ]; then
set_current_mode "dark"
switch_mode "dark"
else
set_current_mode "light"
switch_mode "light"
fi
}
if [ $# -eq 0 ]; then
toggle_mode
elif [ "$1" == "toggle" ]; then
toggle_mode
elif [ "$1" == "light" ]; then
set_current_mode "light"
switch_mode "light"
elif [ "$1" == "dark" ]; then
set_current_mode "dark"
switch_mode "dark"
else
echo "Usage: $0 {light|dark|toggle}"
exit 1
fi
@ayamir
Copy link
Author

ayamir commented Feb 25, 2025

Prerequisites:

  1. Kitty&Tmux: prepare light and dark version config files for kitty and tmux.
  2. Neovim(with nvimdots): add settings["background"] = light to ~/.config/nvim/lua/user/settings.lua.

How to toggle theme:

chmod +x switch.sh
bash switch.sh

Set hot key to trigger theme toggle?

  • Linux: various methods, depends on your DE.
  • MacOS: use alfred workflow to control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment