17

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?

Wolfgang Jeltsch
  • 355
  • 1
  • 2
  • 11

4 Answers4

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.

Christian Brabandt
  • 25,820
  • 1
  • 52
  • 77
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.

Enlico
  • 2,194
  • 15
  • 31
statox
  • 49,782
  • 19
  • 148
  • 225
  • 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. – Wolfgang Jeltsch Oct 25 '16 at 09: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 04 '19 at 06:45
  • 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 '20 at 09:00
  • @WolfgangJeltsch Did you ever figure out how to set same highlighting for all file types? – anirudh Jun 26 '20 at 03:55
  • I wrote my own color scheme, which I load with the colorscheme command. See :help colorscheme. – Wolfgang Jeltsch Jun 27 '20 at 22:42
4

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

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.

will
  • 15
  • 2