9

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?

  • sounds like issue 542 – Christian Brabandt Oct 25 '16 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. – Wolfgang Jeltsch Oct 25 '16 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. ☹ – Wolfgang Jeltsch Oct 25 '16 at 10:50
  • I am now writing my own color scheme, which apparently also works around this bug. – Wolfgang Jeltsch Oct 25 '16 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? – Wolfgang Jeltsch Oct 25 '16 at 11:52
5

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.

  • I think it is because of this Vim issue; so your reponse really seems to be the correct answer to my problem. – Wolfgang Jeltsch Oct 25 '16 at 12:48
3

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 as several chapters on this topic.

  • 2
    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. – Wolfgang Jeltsch Oct 25 '16 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. – Wolfgang Jeltsch Oct 25 '16 at 10:11
  • @WolfgangJeltsch Yup as my post and the doc says the directory should be ~/.vim/after/syntax not ~/.vimrc – statox Oct 25 '16 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. – Wolfgang Jeltsch Oct 27 '16 at 15:19
  • nope! not working. – Yar Dec 4 at 6:45
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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