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

by on October 30, 2006 · 54 comments· LAST UPDATED November 14, 2013

in , ,

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

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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsvim
Estimated completion time2m

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.

Task: Turn on color syntax highlighting

Open a file, for example open existing file called file.c, enter:
$ vi file.c

Now press ESC key, type : syntax on
:syntax on
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

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

Task: Turn off color syntax highlighting

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

How do I make the syntax highlighting permanent?

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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 54 comments… read them below or add one }

1 trojan March 10, 2007 at 1:22 pm

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

Reply

2 yoander May 12, 2008 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

Reply

3 Bobby June 10, 2008 at 5:51 pm

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 at 10:56 am

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

Reply

5 Garratt November 13, 2008 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….

Reply

6 Kent August 1, 2014 at 1:12 pm

@Garratt thank you for including the set command list. It is a great edition to this good tutorial. It all worked for me on a Mac OS X Mavericks.

Reply

7 Khader December 23, 2008 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

Reply

8 Steve February 28, 2009 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!

Reply

9 rob March 11, 2009 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

Reply

10 Narendra March 12, 2009 at 8:29 pm

Hey Vivek,

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

Reply

11 saritha April 3, 2009 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

Reply

12 Senthil April 13, 2009 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

Reply

13 Daniel Brockman March 2, 2011 at 7:19 pm

@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

14 Chris G October 11, 2014 at 7:22 pm

On many systems, the vi command is linked to vim. So you may be using vim and not even know it. You could try doing this:

vi –v

And see what the version is. If it says something like mine:

VIM – Vi IMproved 7.4 (2013 Aug 10, compiled Aug 12 2013 00:27:34)

then you have VIM. However, if you are still not getting highlighting, then you might be using a compact version of VIM (vim-tiny). So see the post above from
rob on March 11, 2009 at 3:50 pm to find out more about that.

Good luck!

Reply

15 Zuquirio Ámaur April 17, 2009 at 4:13 am

Thanks, this works for me on ubuntu 8.10

Reply

16 Arturo April 19, 2009 at 3:16 pm

It’s great! Thanks.

Reply

17 andy September 18, 2009 at 10:24 pm

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

18 Anonymous August 9, 2011 at 9:01 pm

use:
:noh
to turn it off

Reply

19 dave bratton February 29, 2012 at 7:40 pm

This worked for me

Reply

20 Priya April 11, 2012 at 2:56 pm

This worked for me. Thanks a ton. Saved a lot of my time.

Reply

21 Gayathri March 6, 2014 at 5:01 am

thank a lot……….

Reply

22 Titus September 24, 2009 at 5:46 pm

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

23 sumit September 25, 2009 at 9:19 am

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

Reply

24 Ben January 1, 2010 at 11:35 pm

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

Reply

25 Bubnoff February 9, 2010 at 7:47 pm

‘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

26 Imran May 25, 2010 at 12:30 pm

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

Reply

27 shijitht August 5, 2010 at 12:47 pm

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

28 dilu August 6, 2010 at 7:09 pm

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

Reply

29 Manoj September 9, 2010 at 4:28 am

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

30 George January 16, 2011 at 5:04 am

What kind of idiot decided that we need syntax highlight ?

Reply

31 Daniel Brockman January 28, 2011 at 12:54 am

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

32 Erik August 22, 2012 at 5:51 am

Indeed. Thank you.
Some completely other kind of idiot than me, apparently.

Reply

33 Dave January 18, 2011 at 10:25 pm

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

34 Daniel Brockman March 2, 2011 at 7:23 pm

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

35 Rob K May 12, 2011 at 7:44 pm

All you could want to know about vim and syntax highlight:
http://vimdoc.sourceforge.net/htmldoc/syntax.html

Reply

36 Ahamed ishak January 19, 2011 at 6:25 pm

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

37 Gobikrishnan January 31, 2011 at 6:30 pm

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

38 johan March 1, 2011 at 10:47 pm

Thank you very much very helpful.

Reply

39 Rajasekhar July 12, 2011 at 9:00 am

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

Reply

40 Indie July 14, 2011 at 4:36 pm

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

41 Ginso July 25, 2011 at 9:43 pm

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

42 Sourav Chakraborty September 21, 2011 at 8:10 am

Thanks for the great tip!

Reply

43 Diwanker Pandey September 21, 2011 at 10:38 am

how to compile c program in fedora 15?

Can anybody help?????

Reply

44 Theresa July 20, 2012 at 6:14 pm

Hooray! Thanks, Rob who commented in 2009. I had syntax on and had adjusted the color settings, from 8 colors to 256, but the final key was to kill vim-tiny. Running Ubuntu and had installed vim-full but I was still apparently using vim-tiny. Just ‘sudo apt-get remove vim-tiny’ed. This post helped me with color settings http://stackoverflow.com/questions/3761770/iterm-vim-colorscheme-not-working

Reply

45 Abi August 24, 2012 at 10:03 am

Thanks. The syntax highlighting finally works for me when I type :syntax on.
I use Vim Editor in Cygwin. 1.7.16-1(Latest version). But I am unable to make it default. I tried editing the $vi .vimrc. It just doesn’t work. Help would be appreciated.

Reply

46 Mark November 1, 2012 at 2:34 pm

One other piece of this puzzle, for those who are using the correct syntax to turn highlighting on, but are still not getting color: you might need to tell vim what syntax highlighting to use. It can’t always figure out the correct one.
For example, when editing a config file in vim, type
:set syntax=sh
That sets it to “shell script” highlighting. The syntax highlighting configs available are: c perl java html cpp sh csh
so if you’re editing a perl file, you’d type :set syntax=perl

Reply

47 praveen November 23, 2012 at 7:11 pm

hey..i m getting syntax comman not found

Reply

48 Simon December 12, 2012 at 2:07 am

Cool. Good to know. Thanks.

Reply

49 UncaAlby January 15, 2013 at 6:32 pm

I’m happy to turn the syntax highlighting OFF

It gets confusing with all the damn colors. I can never tell where the cursor is!

Now I need to turn off the colors that highlights EVERY DAMN MATCH when you search for text! Then that STAYS ON when you exit and return later.

Look, that’s a nice feature, but it deserves its own distinct command. Usually when I’m searching for something, I only want the NEXT one. And again, it makes it difficult to find the cursor, so I always end up making changes in the wrong place.

Reply

50 Splendid Bill May 22, 2013 at 3:32 pm

To turn off highlighting :noh
If anyone is still not getting colour or getting shades of grey (I was using securecrt) the doing :set term=ansi before :syntax on gave me colours. (Sinclair ZX Spectrum colours lol )

Reply

51 Reefersleep October 30, 2013 at 10:18 am

Thanks for the hint, I’ve been using vim in the terminal on my Mac for a while now and have wondered why everything was just black and white! Just put “syntax on” in my ~/.vimrc, and it worked right away. Cheers :)

Reply

52 Lee Xiang Wei December 2, 2013 at 3:54 pm

Thanks

Reply

53 ron July 16, 2014 at 12:55 pm

/etc/vim/vimrc is the path to the config on debian.
Uncommenting the syntax on line enables the functionality.

Reply

54 Kent August 1, 2014 at 1:21 pm

This all worked for me on a Mac OS X with Mavericks. I used the following options in my ~/.vimrc file:
1 syntax on –> for syntax highligting
2 set ts=4 –> for tab spaces set to four per tab character
3 set sw=4 –> Set the shift width to four spaces
4 set spell –> set spellchecking on
5 set spl=en –> Set spelling language to english
6 set ai –> Enable auto indentation
7 set nu –> Enables line numbers

You will notice I have line numbers set with ‘set nu’. I did not type in the numbers one through seven though they were put there by VIM. Then, when I copied and pasted the text it gave me line numbers!

Thanks to all who contributed to this post.

P.S.: When using the options from my post make sure to remove the line numbers and my comments that are all prefixed with ‘–>’ since they are not real VIM comments. I added them to this post after copying and pasting from VIM.

Reply

Leave a Comment

Tagged as: , , , , , , , , ,

Previous Faq:

Next Faq: