Turn On or Off Color Syntax Highlighting In vi or vim Editor

How do I turn on or off color syntax highlighting in vi or vim text editor on a Linux or Unix-like operating system?

Vim or vi is a text editor. It can be used to edit all kinds of plain text files, editing programs, and UNIX/Linux configuration files. Vim supports additional options and it includes a lot of enhancements above old good vi text editor.

Syntax highlighting is nothing but a feature of vi/vim text editors that displays text, especially source code, in different colors and fonts according to the category of terms. The following instructions show you how to enable or disable syntax colors for VI/VIM text editor running on a Linux or Unix-like system.

How to enable vim syntax colors option

  1. Edit ~/.vimrc file by typing the command: vi ~/.vimrc
  2. Append the following option
  3. syntax on
  4. Save and close the file
  5. Test it by running vim command: vim foo.sh

Let us see steps in details.

Turn on color syntax highlighting in vim

Open a file, for example open existing file called file.c, enter:
$ vi file.c
Now press ESC key, type “: syntax on” i.e. type as follows:
:syntax on
Here is C source code editing while syntax highlighting is on in vi or vim:

Fig.01: Turn on or off color syntax highlighting in vi or vim

Turn off color syntax highlighting in vim

To turn it back off, press ESC key, type : syntax off
:syntax off

How do I make the syntax highlighting permanent under vim?

You can edit ~/.vimrc file and add append vim command syntax on to it. This ensures that vim will start with color syntax highlighting option:
$ cd ~
$ vi .vimrc

Append the following line:
syntax on
Save and close the file. You should now be able to use vim to edit files with syntax on options.

How to set and use a vim color scheme

You can find installed color schemes in the following path:
Use the ls command to list it:
$ ls -l /usr/share/vim/vim*/colors/
Sample outputs:

total 76
-rw-r--r-- 1 root root 2476 Jul 27 01:43 blue.vim
-rw-r--r-- 1 root root 2990 Jul 27 01:43 darkblue.vim
-rw-r--r-- 1 root root  548 Jul 27 01:43 default.vim
-rw-r--r-- 1 root root 2522 Jul 27 01:43 delek.vim
-rw-r--r-- 1 root root 2812 Jul 27 01:43 desert.vim
-rw-r--r-- 1 root root 1666 Jul 27 01:43 elflord.vim
-rw-r--r-- 1 root root 2452 Jul 27 01:43 evening.vim
-rw-r--r-- 1 root root 1958 Jul 27 01:43 industry.vim
-rw-r--r-- 1 root root 3555 Jul 27 01:43 koehler.vim
-rw-r--r-- 1 root root 2460 Jul 27 01:43 morning.vim
-rw-r--r-- 1 root root 2006 Jul 27 01:43 murphy.vim
-rw-r--r-- 1 root root 1037 Jul 27 01:43 pablo.vim
-rw-r--r-- 1 root root 2673 Jul 27 01:43 peachpuff.vim
-rw-r--r-- 1 root root 2904 Jul 27 01:43 README.txt
-rw-r--r-- 1 root root 1393 Jul 27 01:43 ron.vim
-rw-r--r-- 1 root root 2720 Jul 27 01:43 shine.vim
-rw-r--r-- 1 root root 2445 Jul 27 01:43 slate.vim
-rw-r--r-- 1 root root 1629 Jul 27 01:43 torte.vim
-rw-r--r-- 1 root root 1840 Jul 27 01:43 zellner.vim

To use torte color scheme type the following in vim text editor:
:colorscheme torte
Sample outputs:

Using a vim color scheme


Add command in ~/.vimrc

vim Command Description/Purpose
syntax on Enabling vi/vim syntax colors
syntax off Disabling vi/vim syntax colors
colorscheme foo Specifying a colorscheme called foo. Use ‘/usr/share/vim/vim*/colors/’ to find installed color scheme

Please note that on most Linux distro vi is a symbolic link to vim.

See also

This entry is 4 of 5 in the Coloring Outputs in the CLI Apps Tutorial series. Keep reading the rest of the series:

  1. How to enable colorized output for ls command in MacOS X Terminal
  2. How To Turn On/Off Colors For ls Command In Bash On a Linux/Unix
  3. Turn off color in Linux terminal/bash session
  4. Turn On or Off Color Syntax Highlighting In vi or vim Editor
  5. BASH Shell: Change The Color of My Shell Prompt Under Linux or UNIX

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.