Turn on or off color syntax highlighting in vi or vim

by Vivek Gite on October 30, 2006 · 36 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 36 comments… read them below or add one }

1 trojan March 10, 2007

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

Reply

2 yoander May 12, 2008

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

Reply

3 Bobby June 10, 2008

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

Reply

4 Satya Prakash March 3, 2011

To me as well. set syntax=on is working and not just syntax=on

Reply

5 Garratt November 13, 2008

@ 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….

Reply

6 Khader December 23, 2008

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

Reply

7 Steve February 28, 2009

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!

Reply

8 rob March 11, 2009

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

Reply

9 Narendra March 12, 2009

Hey Vivek,

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

Reply

10 saritha April 3, 2009

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

Reply

11 Senthil April 13, 2009

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

Reply

12 Daniel Brockman March 2, 2011

@Senthil

:syntax on and :syntax off don’t work in vi. They are features of vim. vi is ancient, emerging from work by George Colouris and Bill Joy circa 1973 to 1979. It had no color and no understanding of syntax at all. This was an epoch before color was available on computers (except for government supercomputers used to track incoming intercontinental ballistic missiles, perhaps). Vim arrived much later, the first versions appearing in the early 1990s.

Ref: http://www.softpanorama.org/Editors/Vimorama/history.shtml

Reply

13 Zuquirio Ámaur April 17, 2009

Thanks, this works for me on ubuntu 8.10

Reply

14 Arturo April 19, 2009

It’s great! Thanks.

Reply

15 andy September 18, 2009

I am using Vim on fedora.. How do you turn off yellow highlight between space (text) in vim file?

what command turn off yellow highlight?

Reply

16 Anonymous August 9, 2011

use:
:noh
to turn it off

Reply

17 Titus September 24, 2009

I turned off the colorization. Whew. Now I would like to get rid of the highlighted
matching braces. When I put the cursor on an open curley brace it highlights that
brace and the matching closing brace. Rescue me!

Titus sends

Reply

18 sumit September 25, 2009

Even I had searched online a lot before but nothing worked..thankfully I landed here today and it finally worked..:) Thanks

Reply

19 Ben January 1, 2010

Hey. If you are using a dark background for your terminal you may want to
” set background=dark “

Reply

20 Bubnoff February 9, 2010

‘syn on/syn off’, instead of typing ‘syntax’, works as well in vim.

Imagine what your boss would think if he/she found out you were getting paid while typing
those three extra letters!!!

Bubnoff

Reply

21 Imran May 25, 2010

I got syntax highlighting turned on on linux VI editor. Thanks!

Reply

22 shijitht August 5, 2010

add a file name .exrc in home dir
set cindent ” for c indentation(correct spacing)
syntax on ” coloring according to c syntax
set nu ” line numbers

Reply

23 dilu August 6, 2010

thanks all,,,,,, i was using vi in cygwin and syntax on worked…….. lol……….

Reply

24 Manoj September 9, 2010

hi,
I am using fedora 13.I wanted to highlight keywords of my C program, i tried all the above told but i am still unable..pls help me out,I am new to linux

Reply

25 George January 16, 2011

What kind of idiot decided that we need syntax highlight ?

Reply

26 Daniel Brockman January 28, 2011

I’m very glad to have found this blog and to have learned to use syntax off. Those colors were a profound interference for me.

Reply

27 Dave January 18, 2011

Thanks! Worked well.

Syntax highlighting is a huge benefit to me. It makes commented and uncommented lines different colors in config files. Much easier for finding which options are enabled.

Reply

28 Daniel Brockman March 2, 2011

I’m colorblind, and the syntax highlighting just makes the code hard to read.

I hope the next version of VIM lets me specify what color I want for background and what color I want for foreground instead of requiring me to choose from their predefined templates.

Hey! Maybe VIM already lets me specify bg and fg colors! Does anyone know the syntax for the command?

Reply

29 Ahamed ishak January 19, 2011

I try. but i didn’t got it. its not working in my Linux OS.
I use Fedora 14.
Plz give me a help to solve this problem

Reply

30 Gobikrishnan January 31, 2011

1st checkl you have installed vim by connanding
yum install vim-enhanced
if you got message like “already installed and latest version
Nothing to do”
if not install it ,
then open your .c/ .cpp file and command
:set syntax=on

Reply

31 johan March 1, 2011

Thank you very much very helpful.

Reply

32 Rajasekhar July 12, 2011

Its working Nice..!! Thank you..!!

Reply

33 Indie July 14, 2011

If you use sudoedit to edit a file, vim won’t be able to determine what syntax to apply so you need to also do

:set syn=php

Reply

34 Ginso July 25, 2011

hi, when i open a new .tex document, i have a different syntax than, when i open an already existing .tex document. I like the first one better, but typing :set syn=tex gives me the ugly one. Any ideas how i get the other one?

Reply

35 Sourav Chakraborty September 21, 2011

Thanks for the great tip!

Reply

36 Diwanker Pandey September 21, 2011

how to compile c program in fedora 15?

Can anybody help?????

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 2 + 11 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: