Turn on or off color syntax highlighting in vi or vim

This entry is part 2 of 10 in the series VI / VIM Text Editor

Q. How do I turn on or off color syntax highlighting in vi or vim text editor under UNIX or Linux?

A. Vim or vi is a text editor. It can be used to edit all kinds of plain text. It is especially useful for editing programs or UNIX/Linux configuration files.

Task: Turn on color syntax highlighting

Open file (for example file.c):
$ vi file.c

Now press ESC key, type : syntax on
:syntax on

Task: Turn off color syntax highlighting

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

You can edit ~/.vimrc file and add command syntax on to it so that next you will start vim with color syntax highlighting option
$ cd
$ vi .vimrc

Append the following line:
syntax on
Save and close the file.

C source code editing while syntax highlighting is on in vi or vim:
Turn on or off color syntax highlighting in vi or vim

Series Navigation«How To Show Line Numbers In vi / vim Text EditorUbuntu Linux Vim Sorry, the command is not available in this version: syntax on»
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 12 comments… read them below or add one }

1 trojan 03.10.07 at 1:22 pm

Hey this Syntax-highlighting works!!
Looks cool
Thanks.

2 yoander 05.12.08 at 2:16 pm

In vim editor you can use syntax enable too.

Inside vim in command mode press shift : and write syntax press space and press tab and vim go through out different syntax command

3 Bobby 06.10.08 at 5:51 pm

I believe that the exact syntax is “set syntax=on”. At least that is how it works for mine.

4 Garratt 11.13.08 at 8:51 am

@ Bobby, while it may work, I’m guessing you have an extra command after typing vim filename, that is because it’s asking for extra info…and can be avoided by hitting enter the correct way is:

set ts=4
set sw=4
set spell
set spl=en
syntax on
set wrap
set ai

etc….

5 Khader 12.23.08 at 10:40 pm

All of the obove suggestions may not work since on most flavors of linux vi is pointing to vim. unalias vi.
$ which vi
vi=vim
$ unalias vi
$ which vi
/bin/vi

6 Steve 02.28.09 at 5:48 am

FINALLY I got syntax highlighting turned on. I had no idea how to do it. It wasn’t in the man page at all or anywhere else I had looked online until I found it here. Thanks!

7 rob 03.11.09 at 3:50 pm

For Ubuntu, you probably have the package “vim-tiny” installed by default.
To check this run:
dpkg -l vim*

So install the real vim with:
sudo apt-get install vim
or
sudo apt-get install vim-full

On Debian Etch I originally just had vim-tiny, but I now have:

svn@svn:infrastructure$ dpkg -l vim*

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name                   Version                Description
+++-======================-======================-============================================================
ii  vim                    7.0-122+1etch3         Vi IMproved - enhanced vi editor
ii  vim-common             7.0-122+1etch3         Vi IMproved - Common files
un  vim-doc                                 (no description available)
un  vim-full                                (no description available)
un  vim-gnome                               (no description available)
un  vim-gtk                                 (no description available)
un  vim-lesstif                             (no description available)
un  vim-perl                                (no description available)
un  vim-python                              (no description available)
un  vim-ruby                                (no description available)
ii  vim-runtime            7.0-122+1etch3         Vi IMproved - Runtime files
un  vim-scripts                             (no description available)
un  vim-tcl                                 (no description available)
ii  vim-tiny               7.0-122+1etch3         Vi IMproved - enhanced vi editor - compact version
8 Narendra 03.12.09 at 8:29 pm

Hey Vivek,

Thanks:) it’s working!!!!!!

9 saritha 04.03.09 at 8:53 am

I am using fedora 5.My editor is VIM 6.4.7.I wanted to highlight keywords of my C program.I tried adding “syntax on” to your $HOME/.vimrc file which is copied from /usr/share/vim/vim64/gvimrc.examples…

and I tried the above mentioned tips also….pls help

10 Senthil 04.13.09 at 7:04 am

Hi,

This doesn’t work in vi. any idea why? i don’t use vim and is there any setup required to use vim?

Thanks,
Senthil

11 Zuquirio Ámaur 04.17.09 at 4:13 am

Thanks, this works for me on ubuntu 8.10

12 Arturo 04.19.09 at 3:16 pm

It’s great! Thanks.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , ,

Previous post: Host a domain without CPanel or Plesk Control Panel

Next post: Fetch email from a remote server to a local user mailbox