I use vim in ubuntu's default terminal. I set two terminal profiles: one named "dark" with colors > built-in scheme > "Solarized dark" and one named "light" with Solarized light.
In Vim I like to have the cursor line highlighted so I put this command in my vimrc
:
hi CursorLine cterm=NONE ctermbg=254 ctermfg=NONE
This highlights the cursor line with a light gray background. It's perfect when using Solarized Light, but not at all when using Solarized Dark.
Luckily and by some mechanism I do not understand, vim is aware whether I am using solarized dark or solarized light: the background
option is being set to "light" or "dark" accordingly. For instance if I am using solarized light this is what happens:
:set background?
background=light
So I thought I would use that in my vimrc
to change the cursor line color depending on what color scheme I use:
set cursorline
if &background == "light"
hi CursorLine cterm=NONE ctermbg=254 ctermfg=NONE
else
hi CursorLine cterm=NONE ctermbg=238 ctermfg=NONE
endif
Problem : It doesn't work. Cursor line is always in light gray, as if the background
property was always set to "light"
.
It seems that at the time when vimrc
is loaded the background
property has not been changed already.
Note that if I execute source ~/.vimrc
when I am in Vim it works great, the cursor line is highlighted with the proper color.
Any idea on how to fix that?
vim 7.4.1689
GNOME Terminal 3.18.3
Ubuntu 16.04 LTS and Ubuntu 17.04
Note that I do not have any :colorscheme <something>
in my vimrc
: I get solarized themes via gnome terminal profiles. I don't think Vim "knows" I am using these colors; it is just that somehow some script sets vim's background
variable according to my gnome terminal color choice.
There is a vim plugin named AfterColors.vim
that allows one to customize things after a color scheme is loaded. You basically need to put your highlight cursorline statement ( hi CursorLine...
) in after/colors/common.vim
file.
From the documentation of AfterColors.vim script:
Allows you to create an after/colors/ script for customizing any colorscheme.
-- EXAMPLE -- If you like the 'desert' colorscheme, but you really want comments to be red and functions to be blue, previously you would copy the entire colorscheme into your home directory and customize it. With this plugin installed, you can create a small script to change just the parts you want for that colorscheme, exactly how you would for an ftplugin or syntax script:
For unix systems you would create: ~/.vim/after/colors/desert.vim: highlight Comment guifg=Red ctermfg=Red highlight Function guifg=Blue ctermfg=Blue
On windows you would create: C:\\Documents and Settings\\Peter\\vimfiles\\after\\colors\\desert.vim: highlight Comment guifg=Red ctermfg=Red highlight Function guifg=Blue ctermfg=Blue
-- VERSION 6 WARNING -- If your Vim is older than version 7, then the after/colors scripts will only be loaded once when Vim starts. This will not be a problem if you choose your colorscheme in your .vimrc file, but if you change your colorscheme after vim has loaded then your after/colors scripts will be ignored. This is not an issue in Vim 7.
And,
install details
1) Put AfterColors.vim in ~/.vim/plugin/ or $HOME\\vimfiles\\plugin\\
2) Create your colorscheme customizations in after/colors/.vim
3) Create global customizations (for all colorschemes) in after/colors/common.vim
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.