2

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?

Brandon
  • 1,997
  • 3
  • 24
  • 33

3 Answers3

5

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"'

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • 1
    Great. I used `export ITERM_PROFILE=dark` to set an environment variable then I check for it in my `.vimrc` and set appropriately. – Brandon Dec 11 '11 at 07:02
4

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
}
cecep
  • 41
  • 4
  • What is the -e option in the echo command? It's not in my man page. – StevieD Mar 31 '19 at 09:27
  • Huh, -e is on my linux box and enable interpreation of backslash escapes. It's not documented on my mac but still works. Weird. – StevieD Mar 31 '19 at 09:30
  • StevieD, regarding echo -e: When you type "echo" from bash, it uses the bash builtin command echo, not /bin/echo. The 'man echo' command shows the man page for /bin/echo. If you type 'man bash' and search for echo, you'll find the options for the bash echo builtin command (-neE) and the backslash/escape sequences you can use. – Scott Plante Dec 01 '20 at 15:43
0

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

Matt Boehm
  • 1,894
  • 1
  • 18
  • 21