16

I have several highlight commands in my .vimrc file. In Vim 7.3, these took effect immediately after Vim had started. After updating to Vim 7.4, this is not the case anymore. Vim uses its default highlighting initially. When I load .vimrc a second time by executing source ~/.vimrc, highlighting works as I want.

You should be able to reproduce this problem using the following steps:

  1. Make a copy of your .vimrc file.

  2. Replace ~/.vimrc with a file that contains just the line highlight Type ctermfg=28 cterm=bold.

  3. Run vim dummy.c in a 256 color terminal (for example GNOME Terminal).

  4. Type int into the buffer. The int should appear in regular font and be colored light green, which is not what I want.

  5. Enter :source ~/.vimrc. Now the int should appear in bold font and be colored dark green.

According to the article How to control/configure vim colors, it should be okay to just put the highlight commands into .vimrc. The article says that you should put syntax enable after the highlight commands, but this did not make any difference.

My first fix was to prepend each highlight command in .vimrc with autocmd VimEnter *. This worked for some file types, but not for others (C files are an example).

What is the reason for this behavior? How can I have the highlight commands executed when Vim is started such that they take effect for all buffers during the whole Vim session?

7
  • sounds like issue 542 Oct 25, 2016 at 10:38
  • Yes, when adding set compatible at the beginning of .vimrc, syntax highlighting works as expected. So this really seems to be this bug in action. Oct 25, 2016 at 10:43
  • I have preceded the highlight commands with autocmd ColorScheme *, which was proposed as a workaround in the bug report. It still does not work. ☹ Oct 25, 2016 at 10:50
  • I am now writing my own color scheme, which apparently also works around this bug. Oct 25, 2016 at 11:51
  • Christian, can you just write your comment as an answer? After all, this is the right answer to my problem. Or is there any other approach for dealing with questions that actually reveal bugs? Oct 25, 2016 at 11:52

4 Answers 4

8

This is no definite answer and your problem sounds like issue 542. Apparently you can work around it, by writing your own basic colorscheme, instead of writing those hi ... statements directly into your .vimrc.

1
  • I think it is because of this Vim issue; so your reponse really seems to be the correct answer to my problem. Oct 25, 2016 at 12:48
4

Actually you shouldn't put your custom highlights in your vimrc but in customs syntax files.

These files should be located in ~/.vim/after/syntax/filetype.vim where filetype is the expected filetype. If you are mostly satisfied with a syntax file but would like to add a few command you can follow these steps (taken from :h mysyntaxfile-add):

  1. Create a directory in there called "after/syntax". For Unix:

     mkdir ~/.vim/after
     mkdir ~/.vim/after/syntax
    
  2. Write a Vim script that contains the commands you want to use. For example, to change the colors for the C syntax:

     highlight cComment ctermfg=Green guifg=Green
    
  3. Write that file in the "after/syntax" directory. Use the name of the syntax, with ".vim" added. For our C syntax:

    :w ~/.vim/after/syntax/c.vim

That's it. The next time you edit a C file the Comment color will be different. You don't even have to restart Vim.

:h mysyntaxfile is a good place to get information about how to right your own syntax files. Also Learn Vimscript The Hard Way has several chapters on this topic.

8
  • 6
    What if I want to change the highlighting for all file types. For example, I want the color of comments to be dark blue, no matter what the file type is. Oct 25, 2016 at 9:55
  • I tried out more or less the approach you suggested. I added a file .vimrc/highlighting.vim, created a directory .vimrc/syntax/after and added several symbolic links of the form *filetype*.vim that point to .vimrc/highlighting.vim to this directory. Syntax highlighting still does not work. Oct 25, 2016 at 10:11
  • @WolfgangJeltsch Yup as my post and the doc says the directory should be ~/.vim/after/syntax not ~/.vimrc
    – statox
    Oct 25, 2016 at 10:50
  • That’s correct. However, this was only a typo in my comment. Actually, I worked in the directory .vim and syntax highlighting did not work, probably because of this bug 542. Oct 27, 2016 at 15:19
  • 1
    Files inside syntax should be named as the filetype they should be applied to, i.e. a syntax file for C should be named c.vim. There's no highlighting filetype, that's why ~/.vim/after/syntax/highlighting.vim doesn't work. If you want to reuse highlights defined in one filetype in another filetype you can source the former: so ~/.vim/after/syntax/myotherfile.vim. As statox pointed out it's all explained in :h mysyntaxfile
    – Tae
    Apr 20, 2020 at 9:00
3

For what it's worth, I had this problem with MacVim (8.1.950 (155)). Placing the highlight statement after colorscheme in .vimrc fixed it for me. E.g.:

highlight SpellCap guisp=Yellow
...
colorscheme evening

->

colorscheme evening
...
highlight SpellCap guisp=Yellow

1
  • Didn't work for me on MacVim 8.1.
    – shoe
    Nov 5, 2020 at 23:07
0

Similar to Halil Özgür's answer, putting the highlight statement after a set background=dark command that I had in my vimrc file worked for me.

macOS, VIM 8.2

EDIT Just found this from the help files:

The ":syntax enable" command will keep your current color settings.  This
allows using ":highlight" commands to set your preferred colors before or
after using this command.  If you want Vim to overrule your settings with the
defaults, use: >
    :syntax on

If you were using syntax on, switching to syntax enable might help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.