how to enable and disable syntax highlighting in vi or vim editor

Syntax Highlighting is a feature where the readability of the displayed text is enhanced by showing certain words, phrases or keywords in differing colors or fonts. This is especially useful when viewing source code of various programming, scripting or markup languages. In fact any text with an associated grammar can have syntax highlighting.

Integrated Development Environments (IDE) or Programming Editors support syntax highlighting for the languages that it supports. Vim or vi editor is basically a versatile text editor that can be used to display any text, not necessarily source code. However it is a handy tool to quickly open simple scripts or source files for easy editing. So having syntax highlighting in vi editor makes it easier for editing or viewing files.

Escape Mode

If the file is already open in the editor, then you can easily turn syntax highlighting on or off by using the command :syntax on or :syntax off. In order to execute the command, you will need to the command mode by pressing ESC or the escape key. Then type :syntax on to enable syntax highlighting for the file.

To turn off the syntax highlighting, you would use the command :syntax off.

Default Mode

Although it is handy to do this on demand, you probably would want to set this as the default so that you do not have to do this every time you open the editor. If you want to enable or disable by default in the vim editor, then you could use the .vimrc file. The vimrc file is loaded every time you open the editor and is a handy file that you can use to configure the vi editor.


Open the .vimrc file that is in your home directory. If there isn't one, then you could create a new one.

$ vi ~/.vimrc

First type i to go the edit mode. Now, add the command syntax on in a new line. Save and close the file. That is it.

Now every time you open a file in vi, the syntax highlighting should be enabled by default. If you want the default to be off then add syntax off in the ./vimrc/ file instead of syntax on.