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

ADVERTISEMENTS

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

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:
/usr/share/vim/vim*/colors/
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:

How to turn on or off color syntax highlighting and colors in vim

Using a vim color scheme

Summary

Add command in ~/.vimrc

vim CommandDescription/Purpose
syntax onEnabling vi/vim syntax colors
syntax offDisabling vi/vim syntax colors
colorscheme fooSpecifying 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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
62 comments… add one
  • trojan Mar 10, 2007 @ 13:22

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

  • yoander May 12, 2008 @ 14:16

    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

  • Bobby Jun 10, 2008 @ 17:51

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

    • Satya Prakash Mar 3, 2011 @ 10:56

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

  • Garratt Nov 13, 2008 @ 8:51

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

    • Kent Aug 1, 2014 @ 13:12

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

  • Khader Dec 23, 2008 @ 22:40

    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

  • Steve Feb 28, 2009 @ 5:48

    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!

  • rob Mar 11, 2009 @ 15:50

    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
  • Narendra Mar 12, 2009 @ 20:29

    Hey Vivek,

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

  • saritha Apr 3, 2009 @ 8:53

    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

  • Senthil Apr 13, 2009 @ 7:04

    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

    • Daniel Brockman Mar 2, 2011 @ 19:19

      @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

    • Chris G Oct 11, 2014 @ 19:22

      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!

  • Zuquirio Ámaur Apr 17, 2009 @ 4:13

    Thanks, this works for me on ubuntu 8.10

  • Arturo Apr 19, 2009 @ 15:16

    It’s great! Thanks.

  • andy Sep 18, 2009 @ 22:24

    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?

    • Anonymous Aug 9, 2011 @ 21:01

      use:
      :noh
      to turn it off

      • dave bratton Feb 29, 2012 @ 19:40

        This worked for me

      • Priya Apr 11, 2012 @ 14:56

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

      • Gayathri Mar 6, 2014 @ 5:01

        thank a lot……….

  • Titus Sep 24, 2009 @ 17:46

    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

  • sumit Sep 25, 2009 @ 9:19

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

  • Ben Jan 1, 2010 @ 23:35

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

  • Bubnoff Feb 9, 2010 @ 19:47

    ‘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

  • Imran May 25, 2010 @ 12:30

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

  • shijitht Aug 5, 2010 @ 12:47

    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

  • dilu Aug 6, 2010 @ 19:09

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

  • Manoj Sep 9, 2010 @ 4:28

    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

  • George Jan 16, 2011 @ 5:04

    What kind of idiot decided that we need syntax highlight ?

    • Daniel Brockman Jan 28, 2011 @ 0:54

      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.

    • Erik Aug 22, 2012 @ 5:51

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

  • Dave Jan 18, 2011 @ 22:25

    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.

    • Daniel Brockman Mar 2, 2011 @ 19:23

      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?

  • Ahamed ishak Jan 19, 2011 @ 18:25

    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

    • Gobikrishnan Jan 31, 2011 @ 18:30

      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

  • johan Mar 1, 2011 @ 22:47

    Thank you very much very helpful.

  • Rajasekhar Jul 12, 2011 @ 9:00

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

  • Indie Jul 14, 2011 @ 16:36

    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
  • Ginso Jul 25, 2011 @ 21:43

    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?

  • Sourav Chakraborty Sep 21, 2011 @ 8:10

    Thanks for the great tip!

  • Diwanker Pandey Sep 21, 2011 @ 10:38

    how to compile c program in fedora 15?

    Can anybody help?????

  • Theresa Jul 20, 2012 @ 18:14

    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

  • Abi Aug 24, 2012 @ 10:03

    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.

  • Mark Nov 1, 2012 @ 14:34

    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

  • praveen Nov 23, 2012 @ 19:11

    hey..i m getting syntax comman not found

    • dpeach Dec 7, 2015 @ 14:50

      You have to do this inside of a file you are editing in vim, not from the command line before opening a file.

  • Simon Dec 12, 2012 @ 2:07

    Cool. Good to know. Thanks.

  • UncaAlby Jan 15, 2013 @ 18:32

    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.

  • Splendid Bill May 22, 2013 @ 15:32

    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 )

  • Reefersleep Oct 30, 2013 @ 10:18

    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 :)

  • Lee Xiang Wei Dec 2, 2013 @ 15:54

    Thanks

  • ron Jul 16, 2014 @ 12:55

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

  • Kent Aug 1, 2014 @ 13:21

    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.

  • Gabe S. Apr 23, 2015 @ 15:19

    Is there a way to selectively disable syntax highlighting on a line-by-line basis, I mean shut it off only for a couple of lines here and there within a file, while keeping it on for the rest of the file I am editing?

    Btw using ‘vim-gtk’ package under Debian sid.

  • Vijay Jul 22, 2015 @ 3:30

    Thanks for the simple explanation. it works.

  • anonymous Oct 26, 2015 @ 9:17

    Good article.

    My 50 cents. You can redefine any color theme after “syntax on”

    # cat /root/.vimrc 
    map  :w\|!sh % 
    map  :w\|!bash % 
    map  :w\|!python % 
    map  :w\|!python % &
    
    
    " http://vimdoc.sourceforge.net/htmldoc/syntax.html#:colorscheme
    highlight RedundantWhitespace ctermbg=DarkBlue guibg=red
    match RedundantWhitespace /\s\+$\|\t/
    
    syntax on
    highlight Comment ctermfg=green
    

    Now, all comments will be green, not a dark blue. Much clear.

    Cheers!

  • Karen Apr 20, 2016 @ 20:14

    Wow, this doc really helps !!! THANKS !!!

  • Karen Apr 20, 2016 @ 20:16

    When I simply do “ls -l ” in one dir, some links show colors.
    How can I turn on and off colors for “ls ” ?
    I need all files , dirs listed white, while my background is black.

    Thanks !

  • ranga May 2, 2016 @ 13:55

    thank you very much it worked

  • forza Jun 14, 2017 @ 15:09

    This is nice !
    Thank you

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.