No syntax highlighting in GVIM on Windows with _vimrc

6

4

My GVIM 7.3.46 install on Windows XP Professional SP3 32bit does not perform syntax highlighting if there exists a _vimrc file, at least not PHP and C. Even explicitly mentioning set syntax=php I get not highlighting, whether or not the filename extension ends in .php.

Even if there is a 0-byte blank file in C:\Documents and Settings\myUsername\_vimrc then files will not be syntax highlighted. I can rename that 0-byte file to _vimrcBLAH and syntax highlighting works again.

What might cause this, and how might I fix it?

dotancohen

Posted 2012-04-17T14:20:12.593

Reputation: 9 798

Answers

15

The gvim program alone does not have syntax highlighting enabled by default. Syntax highlighting is only enabled by explicitly enabling it with a command such as :syntax on. So the only way to have syntax enabled is to enable it yourself at the Vim command line or to have such a command in a configuration file that's read on startup.

Vim is typically installed on Windows with a standard set of configuration commands in C:\Program Files\Vim\_vimrc. That standard set includes syntax on. Without any other configuration files, gvim will read that file on startup and you will have syntax highlighting.

In :help _vimrc it says, "Four places are searched for initializations. The first that exists is used, the others are ignored." The list of user vimrc files includes $HOME/_vimrc followed by $VIM/_vimrc. On your system, $VIM is C:\Program Files\Vim.

So, when you use a C:\Documents and Settings\myUsername\_vimrc file, gvim reads that file and skips C:\Program Files\Vim\_vimrc. If your _vimrc does not contain syntax on you will not get syntax highlighting.

If your are going to use your own _vimrc, then make sure you copy into it from C:\Program Files\Vim\_vimrc any of those configuration commands you need, including syntax on.

garyjohn

Posted 2012-04-17T14:20:12.593

Reputation: 29 085

Thank you Gary! This is exactly what I needed to know. I'm rather glad to know that the answer is in the fine manual, I'm going to go through it later today. Thanks! – dotancohen – 2012-04-17T16:58:12.380

Thank you for that. How odd that gvim 7.3 on Linux seems to default this to on (I can't find syntax on in any of the .vimrc or .gvimrc files on my Linux system), but gvim 7.3 on Windows doesn't. – T.J. Crowder – 2012-07-20T13:54:36.010

@T.J.Crowder: The set of files discussed in the answer are referred to as personal initialization files. Before loading one of those, Vim searches for and loads a system initialization file. (See :help startup.) A standard Vim installation on Windows has a personal initialization file at $VIM/_vimrc but no system initialization file, so your $HOME/_vimrc is loaded instead of $VIM/_vimrc. A standard Vim installation on a Linux distribution has a system initialization file usually at $VIM/vimrc which is always loaded before and in addition to the personal initialization file. – garyjohn – 2012-07-20T15:04:09.380

@T.J.Crowder: So you will find a syntax on command in the system initialization file on your Linux system, probably in /usr/share/vim/vimrc or /etc/vimrc. The location is given in the output of the :version command. – garyjohn – 2012-07-20T15:06:26.493

@garyjohn: Right, I looked in those as well before posting the above. I can't find one in any of them. I've looked in any .vimrc, .gvimrc, and gvimrc file I could find under both $VIM and $HOME. – T.J. Crowder – 2012-07-20T15:07:11.577

@garyjohn: The one I missed out was $VIM/vimrc (no .). That's where it was hiding. I looked for $VIM/.vimrc instead (doh!). We should clean these comments up, they won't help anyone who doesn't do the same dumb thing. :-) – T.J. Crowder – 2012-07-20T15:12:22.557

1

Since $HOME/_vimrc will be picked up first if it exists, and I don't like to change the stock config file, I just add this line in my $HOME/_vimrc. This way I get my setting done in $HOME/_vimrc, and pick up all the other setting from the installations. And it works even after any reinstall or upgrade.

source $VIMRUNTIME/../_vimrc

Randy Lei

Posted 2012-04-17T14:20:12.593

Reputation: 11

How exactly is your answer related to a question tagged windows? – Dmitry Grigoryev – 2016-03-23T08:41:31.327

I am not familiar with the software but it might refer to a configuration file? Perhaps the author can clarify? – Burgi – 2016-03-23T09:10:55.123

Okay, my use of $HOME might be questionable but I was talking about windows. On Windows 7 it should be C:\Users<ID>. The _vimrc under that directory will always be picked up first. – Randy Lei – 2016-03-24T03:31:08.773