Bash – What’s a smart way to use/maintain two separate bash_profile’s and vimrc’s

bashrcvimrc

I have two different setups I like to use in terminal and vim. One uses a light background and a somewhat fancy airline statusline in vim. The other uses a dark background and a more barebones vim appearance. I'm either wavering between the two out of indecision or just for a little variety once in a while.

What's a smart way to easily switch between these two configurations at will? Right now I've basically got two slightly different .bash_profiles and .vimrcs. When I want to go dark I manually source the dark profile and I've defined a bash alias to start vim with the alternate vimrc. I'm sure there's a better way, and I'd be interested in suggestions.

Update:
I went with the excellent suggestion of setting a THEME environment variable to reference in the config files. Works like a charm. Also found this gem (not in the Ruby sense) which lets me swith the iTerm profile to a dark one at the same time. A warning: defining the bash function as a one-liner like that was giving me a syntax error so I had to break it across multiple lines.

it2prof() {
  echo -e "\033]50;SetProfile=$1\a"
}

alias dark="export THEME=dark && it2prof black && . ~/.bash_profile"
alias light="unset THEME && it2prof white && . ~/.bash_profile"

Better still, it turns out iTerm2 has a bunch of escape codes available to changing settings on the fly.

Another update:
The iTerm2 docs warn that the escape sequences may not work in tmux and screen, and indeed they don't. To get them working you need to tell the multiplexer to send the escape sequence to the underlying terminal rather than trying to interpret it. It's a little hairy, but this is now working for me in tmux, in screen, and in a normal shell session:

darken() {
  if [ -n "$ITERM_PROFILE" ]; then
    export THEME=dark
    it2prof black
    reload_profile
  fi
}

lighten() {
  if [ -n "$ITERM_PROFILE" ]; then
    unset THEME
    it2prof white
    reload_profile
  fi
}

reload_profile() {
  if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
  fi
}

it2prof() {
  if [[ "$TERM" =~ "screen" ]]; then
    scrn_prof "$1"
  else
    # send escape sequence to change iTerm2 profile
    echo -e "\033]50;SetProfile=$1\007"
  fi
}

scrn_prof() {
  if [ -n "$TMUX" ]; then
    # tell tmux to send escape sequence to underlying terminal
    echo -e "\033Ptmux;\033\033]50;SetProfile=$1\007\033\\"
  else
    # tell gnu screen to send escape sequence to underlying terminal
    echo -e "\033P\033]50;SetProfile=$1\007\033\\"
  fi
}

Best Answer

Use an environment variable. This way, you can set THEME=dark or THEME=light in a shell, and all programs started by that shell will use the desired scheme.

In bash or any other shell:

case $THEME in
  light)
    PS1='\[\e05m\]fancy stuff\[\e0m\]';;
  *)
    PS1='\w\$ ';;
esac

In your .vimrc:

if $THEME == "light"
  …
else
  …
endif
Related Question