How to turn on syntax highlighting in VIM 7.3 OSX

vim

System = OSX 10.9.4

I am trying to turn on syntax highlighting in vim while using the terminal. However, I am unable to get it to work properly.

Things I've tried:

  • located the vimrc file and added the following code:

     set ai                  " auto indenting 
     set history=100         " keep 100 lines of history
     set ruler               " show the cursor position
     syntax on               " syntax highlighting
     set hlsearch            " highlight the last searched term
     filetype plugin on      " use the file type plugins
    
  • Located vimrc under directory:

     cd /usr/share/vim/
    

The interesting thing is that once I add the code to the vimrc using vim, followed by exiting (x), and re-opening the file again, syntax is correctly highlighted in the vimrc.

However, when I try to make a new vim file called "test", copy the same code, save and exit. Re-open it, the syntax is not highlighted at all.

It appears that syntax highlighting only works when I open the actually vimrc file—and not when I try to create a new one or open another file that should have syntax highlighting.

  • I've also tried to create a .vimrc (exact copy) under the ~/ (directory). No success.
  • Made a new file called "test" and tried turning it on while active as well:

       vim test 
    

"then within vim"

       :syntax on

I am really confused as to why this partially works.

Any help is much appreciated.

Cheers!

p.s. I have followed these instructions as well from: http://geekology.co.za/article/2009/03/how-to-enable-syntax-highlighting-and-other-options-in-vim

*I am aware of macvim, but would like a solution for the native vim in terminal. Thanks.

Best Answer

  1. NEVER do anything in $VIM as it will work only by luck, cause unexpected behaviors and likely be overwritten next time Vim is updated.

    What you have put in /usr/share/vim/vimrc should be in ~/.vimrc.

  2. filetype on and syntax on are the bare minimum you need in your ~/.vimrc for syntax highlighting to work.

  3. $ vim ~/.vimrc gives you syntax highlighting because the file is recognized by Vim as a vim file. Filetype detection is mostly dependent on file extensions so you can't expect any syntax highlighting in a file called test.

    $ vim test.py, on the other hand, should give you syntax highlighting.

    If the filetype is not detected, you can force it with :set filetype=python.