vim editor: How do I enable and disable vim syntax highlighting?

vim editor syntax faq: How do I turn on (enable) or turn off (disable) vim syntax highlighting?

Short answer

Turning on syntax highlighting in your vim editor is usually fairly simple; you just need to issue a syntax on command, either in your current editor session, or in your vimrc configuration file. Here are a couple of quick examples.

Turn vim syntax highlighting on

To enable syntax highlighting in your current vim editor session just issue this command in vim’s last-line mode:

:syntax on

With syntax highlighting turned on, a vim editor session can give you some nice syntax highlighting, as shown in this image:

vi/vim syntax highlighting enabled

Turn vim syntax highlighting off

Conversely, if you want to turn syntax highlighting off in your current vim editor session, just issue this command:

:syntax off

When you turn vim syntax highlighting off, the same file in your vim editor will now look like this:

vi/vim syntax highlighting, with highlighting disabled (turned off)

Enable vim syntax highlighting by default (vimrc)

If you want syntax highlighting enabled by default when you open your vim editor, just add this command to your vimrc configuration file:

syntax on

If you’re not familiar with the vim “vimrc” file, it is the vim configuration file, and it can be found in your home directory on a Unix or Linux system. The filename actually begins with a decimal (it's a “hidden” file), and editing it is as simple as issuing this command:

vi ~/.vimrc

Just open the file as shown, then add the syntax on statement somewhere in the file.

vim syntax highlighting problems

I’ve seen vim syntax highlighting not working for several reasons. In one case, vi and vim were actually not the same binary, and the user was trying to use vi (which was older) instead of vim. Once he started using vim everything worked fine.

In another instance a user had an unusual version of vim installed; it was called something like “tiny vim”, or “vim tiny”. Once we installed the “normal” vim version, everything worked fine.

vim syntax highlighting help

As a quick note, you can always get help on vi and vim syntax highlighting by using the vi :help command. When you’re in the vi editor, just issue this last line command to get syntax highlighting help:

:help syntax

If you’ve never used vi help before, and you need to get out of that screen, just type a normal vi quit command, like this:

:q

That will take you out of the vim help screen, and put you back in the file you were editing.

vim syntax files

Mentioning this is like opening Pandora's box, but ... if you really want to dig into the vim syntax highlighting topic, it may help to know that there are vim syntax files included with your Unix or Linux vim distribution. These files will have names like this:

c.vim
cpp.vim
csh.vim
html.vim
java.vim
perl.vim
sh.vim

If you have the Unix locate command installed they are easy to find, like this:

locate java.vim

I just looked on my current macOS system, and found that the vim syntax files are all under this directory:

/usr/share/vim/vim74/syntax

vim syntax highlighting colors

It may also help to know that you can control the vim highlighting colors that are used in your editing sessions. I just wrote two new articles on these, including a vi color scheme tutorial, which shows how to find and select vi/vim color schemes; and a second vi/vim color tutorial, which discusses more fine-grained color control.