Skip to content

Instantly share code, notes, and snippets.

@pranjalpokharel7
Last active March 2, 2021 05:28
Show Gist options
  • Save pranjalpokharel7/d7e3fb72f524a7239f36ec9f63d76baa to your computer and use it in GitHub Desktop.
Save pranjalpokharel7/d7e3fb72f524a7239f36ec9f63d76baa to your computer and use it in GitHub Desktop.
Vim Auto Theme Switcher (dark<->light)

vim_theme_switcher

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

Instructions

  1. Make the script executable with
    sudo chmod +x vim_theme_switcher.sh
    If you name your script file something else, replace vim_theme_switcher.sh with your filename.
  2. Place the file in your home directory (preferable).
    cp vim_theme_switcher.sh ~/
  3. 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.

By:

@pranjalpokharel7
Copy link
Author

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.

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