2
votes

I have two iTerm profiles setup, a dark one to use the Pastel theme and a light one to use solarized light theme. Is there any way to have Vim change the background and colorscheme options based on what profile iTerm is currently using?

3

3 Answers

4
votes

You could add a login command to either profile (in iTerm2 there's a "send text at start" option, I don't know about regular iTem) to alias VIM to use the color scheme you want:

alias vim='vim -c "colorscheme pastel"'

4
votes

This is an old subject, but using Brandon's comment I found a solution:

First, I created two iTerm profiles, simply called light and dark. In my .bashrc I added this function, taken from here and slightly modified:

theme-switch () { echo -e "\033]50;SetProfile=$1\a"; export ITERM_PROFILE=$1; }

Now I can switch iTerm themes using theme-switch light or theme-switch dark

Finally, in my .vimrc I use the ITERM_PROFILE environment variable to determine the background for vim:

let iterm_profile = $ITERM_PROFILE
if iterm_profile == "dark"
    set background=dark
else
    set background=light
endif

Update 2017-12-04: Tmux and Mac OS menu bar

If you use Tmux, things are a bit more tricky. I couldn't get echo -e "\033]50;SetProfile=PROFILENAME\a" to work inside a Tmux session, so I created a shortcut in iTerm to quickly switch between a light or dark profile (Preferences -> Keys -> add a new one and choose 'Change Profile'). The theme-switch function is still needed to set ITERM_PROFILE (which we need to set dark or light background in VIM), but we need to also change its value for Tmux. Moreover, we should load a dark or light theme for the Tmux status bar.

My more expanded theme-switch function adds dark mode to change the look of the Mac OS menu bar, sets ITERM_PROFILE for Tmux and loads the light or dark version of the solarized status bar theme I'm using (you can use your custom status bar config as long as you put the light and dark version in separate files):

function theme-switch {
 echo -e "\033]50;SetProfile=$1\a"
 export ITERM_PROFILE=$1
 if [ $1 = "dark" ]; then
    dark-mode on 2> /dev/null # Prevent error message if dark-mode is not installed
    if tmux info &> /dev/null; then
        tmux set-environment ITERM_PROFILE dark
        tmux source-file ~/.tmux/plugins/tmux-colors-solarized/tmuxcolors-dark.conf
    fi
 else
    dark-mode off 2> /dev/null
    if tmux info &> /dev/null; then
        tmux set-environment ITERM_PROFILE light
        tmux source-file ~/.tmux/plugins/tmux-colors-solarized/tmuxcolors-light.conf
    fi
 fi
}
0
votes

http://vimdoc.sourceforge.net/htmldoc/term.html#term-dependent-settings

If you want to set options or mappings, depending on the terminal name, you can do this best in your .vimrc. Example:

if &term == "xterm"

 ... xterm maps and settings ...    

else if &term =~ "vt10."

 ... vt100, vt102 maps and settings ...    

endif