Skip to content

Instantly share code, notes, and snippets.

@fictorial
Last active November 11, 2021 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fictorial/110fd418ecbbe827e7f68cc646b9c3a8 to your computer and use it in GitHub Desktop.
Save fictorial/110fd418ecbbe827e7f68cc646b9c3a8 to your computer and use it in GitHub Desktop.
switch vim color scheme to match iterm profile
" If we're using iterm2 (OS X), check if there's a colorscheme in vim matching the iterm2 profile name.
" If the profile name has suffix " - light" then set bg=light else bg=dark.
" If we're not using iterm2, based on the time of day, pick a colorscheme light (day) vs dark (night).
" I like the yin and yang colorschemes for vim and iterm2: https://github.com/pgdouyon/yin-yang
" So I have a profile in iterm2 named "yang - light" and "yin - dark".
" Note: iterm2 does not _update_ the env var ITERM_PROFILE after _changing_ the profile, just on launch.
" You'll have to `source ~/.vimrc` after changing the iterm2 profile or if not running vim, launch it.
let itermprofile = system("osascript -e 'tell application \"iTerm2\"\nget profile name of current session of current window\nend tell'")
try
if itermprofile =~ " - light"
execute "colorscheme " . substitute(itermprofile, " - light", "", "")
set background=light
elseif itermprofile =~ " - dark"
execute "colorscheme " . substitute(itermprofile, " - dark", "", "")
set background=dark
else
" no "- light" nor "- dark" so try verbatim.
execute "colorscheme " . itermprofile
set background=dark " assume it is dark
endif
catch /^Vim\%((\a\+)\)\=:E185/
" Not using iterm2 or colorscheme not found.
let hr = system('date +%H')
if hr < 8 || hr > 17
" early morning or late at night
set background=dark
color yin
else
" daytime
set background=light
color yang
endif
endtry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment