How to toggle dark and light mode for linux terminal, tmux, Vim, fish shell and Ubuntu Gnome?
You know the situation: you've worked through the night with your development system and now you need to go outside or take the train. Normally you feel comfortable with the dark mode. But suddenly you need to change it to light mode.
Introduction
By this post, I'd like to share how I switch between light and dark mode with a single script. Maybe it will help you to create your own, because it is customized for my development setup.
For development, I use the following setup:
- OS: Linux Ubuntu (Gnome) with Wayland
- tmux terminal emulator with powerline (source)
- foot terminal (source)
- theme.sh (source)
- VI for coding (Neovim)
Bash Script
For those in a hurry, the bash script below will switch all necessary applications between light and dark mode for the above setup:
~/bin/switch_scheme.sh#!/bin/bash # # Switch color scheme between light and dark for the following applications: # * terminal (theme.sh) # * fish # * gnome (color scheme and gtk-theme) # * tmux (reload powerline) # # Limitations: # * Running vim sessions do not change the theme and have to be reopened # Function for switching scheme depending on the content of the file # ~/.colorscheme # # Defaults to dark and changes to light function switch_scheme() { scheme=`cat ~/.colorscheme` # If scheme is "light", change to dark if [ "$scheme" = "light" ]; then # Select a dark theme for terminal: /usr/bin/theme.sh ayu # Set variable "background_mode" of fish. A fish config will update # the theme (@see ~/.config/fish/conf.d/update_theme.fish) fish -c "set --universal background_mode 'dark'" # Gnome: set preferable scheme to dark gsettings set org.gnome.desktop.interface color-scheme prefer-dark # and change the gtk-theme gsettings set org.gnome.desktop.interface gtk-theme 'Yaru-prussiangreen-dark' # Finally set the current mode to "dark" echo "dark" > ~/.colorscheme else /usr/bin/theme.sh selenized-light fish -c "set --universal background_mode 'light'" gsettings set org.gnome.desktop.interface color-scheme prefer-light gsettings set org.gnome.desktop.interface gtk-theme 'Yaru-prussiangreen' echo "light" > ~/.colorscheme fi } # If running inside tmux: # * Leave tmux (detach), run this script and attach again and update powerline if [ "$TERM" = "screen-256color" ] && [ -n "$TMUX" ]; then session_name=`tmux display-message -p '#S'` tmux detach -E "switch_scheme.sh && tmux attach -t $session_name" /home/mitosch/.tmux/tmux-powerline/src/powerline.sh init else switch_scheme fi
Some applications need a special setup to react on the switch.
Fish shell: Change theme interactively
To change the theme of fish, we make use of a universal variable which fish is listening to and changes the theme:
~/.config/fish/conf.d/update_theme.fishfunction update_theme --on-variable background_mode if [ "$background_mode" = "dark" ] fish_config theme choose dark else if [ "$background_mode" = "light" ] fish_config theme choose light end end
For this to work, you'll need those fish themes in the directory "~/.config/fish/themes":
~/.config/fish/themesdark.theme light.theme
Vim (Neovim): Switch dark and light themes
To start Vim with the correct colorscheme depending on the dark or light theme, the following lua script will read the content of the file "~/.colorscheme":
~/.config/nvim/lua/colorscheme.lua-- Detect dark or light theme saved in .colorscheme. defaults to dark local background_mode local f = io.open(os.getenv('HOME')..'/.colorscheme', 'r') if f ~= nil then background_mode = f:read('*all') background_mode = string.gsub(background_mode, "\n", '') f:close() else background_mode = 'dark' end -- use ayu theme for dark mode, selenized for light mode if background_mode == 'dark' then vim.g.ayucolor = background_mode vim.cmd("colorscheme ayu") else vim.cmd("colorscheme selenized") vim.cmd("set background=" .. background_mode) end
I'm using the ayu colorscheme for the dark mode in my terminal and Vim. In the light mode the selenized colorscheme is being used. You can find the Vim colorschemes here:
Conclusion
With a simple bash script you should now be able to change between light and dark mode. There is still a limitation for the running Vim sessions, which need to be restarted. When I find a fix in the future, I'll let you know.
To get a feeling about the setup, here you find the foot terminal in dark and light mode:
I hope this helped somebody to create an own script to change your development setup fast and efficient. It helps me every day, when the sun is shining. For feedback or suggestions, just drop me an email.