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

Posted on in Categories , , , , , , , , last updated October 26, 2015

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.

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” i.e. type as follows:
: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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 62 comments/add one below):

62 comment

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

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


  3. 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
    sudo apt-get install vim-full

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

    [email protected]:infrastructure$ dpkg -l vim*

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

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

    2. 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!

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

  6. ‘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!!!


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

    1. 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?

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

  8. 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?

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

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

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

  12. 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 )

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

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

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

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


  17. 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 !

Leave a Comment