I had some fun pimping my development setup during my summer vacation π π and
here's what I came up with. A script toggle-dark-mode
that toggles dark mode for
not only Mac OS, but also Hyper, tmux and vim at once. I'm gonna be so
productive now.. π
(I've customized Hyper term to remove dead space on top for maximized windows, but that currently also means smaller windows are missing the top row as well.)
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell
First an applescript that toggles Mac OS dark mode on/off. The osascript
command can be used to run the script passed in as a heredoc.
I've currently replaced iTerm with Hyper term which is configured with
~/.hyper.js
and live-reloads all windows on changes. I took the simple
approach and used sed
to substitute a dark mode plugin with a light mode
plugin.
I use tmux heavily and have a binding since previously to toggle between dark
and light status bar using a few commands. But now we want to change this for
all tmux sessions π€. My solution (thanks @Hologos) is to run set-option
without -t
to send some commands to all sessions.
I'm a full-time Vim-user, specifically Neovim these days, and I typically have
one or more instances in every tmux session, might be 20+. So I want all these
instances to change colorscheme on command.
neovim-remote to the rescue! List all
neovim instances with nvr --serverlist
to list the instances. After that it's
easy to loop over them and send a command to change colorscheme. nvr --servername $name -cc ":colorscheme onehalfdark"
#!/usr/bin/env bash
set -e
# toggle mac os dark mode
case "$1" in
"on"|"dark")
next_dark_mode_value="true"
;;
"off"|"light")
next_dark_mode_value="false"
;;
*)
next_dark_mode_value="not dark mode"
;;
esac
osascript <<APPLESCRIPT
tell application "System Events"
tell appearance preferences
set dark mode to $next_dark_mode_value
end tell
end tell
APPLESCRIPT
hyper_config=$HOME/.hyper.js
hyper_dark_plugin=hyper-one-dark-vivid
hyper_light_plugin=hyper-one-light
if [[ $(is-dark-mode) == "true" ]]; then
touch $HOME/.dark-mode
else
rm -f $HOME/.dark-mode
fi
# toggle hyper term dark mode
if [[ -f $HOME/.dark-mode ]]; then
sed -i -e "s/$hyper_light_plugin/$hyper_dark_plugin/" "$hyper_config"
else
sed -i -e "s/$hyper_dark_plugin/$hyper_light_plugin/" "$hyper_config"
fi
if [[ $? -eq 0 ]]; then
if [[ -f $HOME/.dark-mode ]]; then
tmux set-option -g status-left-bg black
tmux set-option -g status-bg black
tmux set-option -g status-fg white
tmux set-option -g window-status-current-bg white
tmux set-option -g window-status-current-fg black
else
tmux set-option -g status-left-bg colour15
tmux set-option -g status-bg colour15
tmux set-option -g status-fg black
tmux set-option -g window-status-current-bg colour12
tmux set-option -g window-status-current-fg black
fi
fi
# toggle vim dark mode
vim_instances=($(nvr --serverlist))
if [[ $? -eq 0 ]]; then
for name in ${vim_instances[@]}; do
if [[ -f $HOME/.dark-mode ]]; then
nvr --servername "$name" -cc ":colorscheme onehalfdark"
else
nvr --servername "$name" -cc ":colorscheme onehalflight"
fi
done
fi
Swapping a hyper plugin in .hyper.js
with sed
is not optimal since it
requires an internet connection when hyper runs npm install
. I think another
approach would be to have a separate hyper plugin that allows toggling two other
plugins. Or perhaps a plugin that contains both a dark and light colorscheme
and some functionality to switch between them?
Hi, you don't have to call
tmux set-option
for every session, you can just omit-t "session-name"
, it's an optional parameter.