This is a simple script to switch color scheme in vim from light to dark and vice-versa depending upon system time. The script is written in bash.
#!/bin/bash
# vim_theme_switcher.sh
DARK_THEME=dark
LIGHT_THEME=light
# configure time in the format HHMMSS (no spaces or :)
CURRENT_TIME=$(date +%H%M%S)
STARTING_TIME=180000 # this is 18:00:00 in system time
END_TIME=083000 # this is 08:30:00 in system time
swap(){
declare -n first_ref=$1
declare -n second_ref=$2
temp=$first_ref
first_ref=$second_ref
second_ref=$temp
}
if [ $END_TIME > $STARTING_TIME ]
then
swap STARTING_TIME END_TIME
swap DARK_THEME LIGHT_THEME
fi
if [ $CURRENT_TIME -gt $STARTING_TIME ] && [ $CURRENT_TIME -lt $END_TIME ]
then
sed -i "s/set\\ background=$LIGHT_THEME/set\\ background=$DARK_THEME/" ~/.vimrc
else
sed -i "s/set\\ background=$DARK_THEME/set\\ background=$LIGHT_THEME/" ~/.vimrc
fi
exit 0
- Make the script executable with
sudo chmod +x vim_theme_switcher.sh
If you name your script file something else, replacevim_theme_switcher.sh
with your filename. - Place the file in your home directory (preferable).
cp vim_theme_switcher.sh ~/
- Add the following line to your shell rc file (.bashrc, .zshrc or any other
depending on your shell):
./vim_theme_switcher.sh
The script will be executed every time you start a new terminal session.
Issue: A file by the name of 180000 is created every time we run the script, which is coincidentally the value of the STARTING_TIME variable we have used. We have not found an appropriate solution based on web search. If you have any idea on what is causing this, please leave a comment.