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

- 30,372
- 14
- 101
- 138
-
1Great. 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
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
}

- 41
- 4
-
-
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
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

- 1,894
- 1
- 18
- 21
-
Ah wait, I suppose in you're situation you're using iterm for both so terminal info would be identical. – Matt Boehm Dec 11 '11 at 06:05