140

I know how to turn syntax highlighting on and off in vim by running this in the editor:

:syntax on/off

But I want syntax highlighting to be enabled by default, so I don't have to turn it on every time I run vim.

How do I do this?

uloco
  • 2,283
  • 4
  • 22
  • 37
lilroo
  • 2,928
  • 7
  • 25
  • 34

7 Answers7

216

Edit your $HOME/.vimrc (Unix/Linux/OSX) or $HOME/_vimrc (Windows) to include the following line:

syntax on

EDIT

If your syntax highlighting doesn't work when you start Vim, you probably don't have a $HOME/.vimrc or $HOME/_vimrc (known collectively as vimrc from now on). In that case, you have two options:

  • Create an empty vimrc.
  • Copy vimrc_example.vim as your vimrc (recommended, thanks @oyenamit). You can find vimrc_example.vim in the runtime directory.

The location of the runtime directory varies between operating systems:

  • On my system (Arch Linux, and Mac, thanks @totophe), it's in /usr/share/vim/vim73.
  • On Windows, it's in \Program Files\Vim\vim73.
Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
  • 1
    I cant find .vimrc, im using a mac running Lion. I can find .viminfo – lilroo Jun 30 '12 at 08:44
  • 7
    If there isn't one, you can create it. Just type :e ~/.vimrc to vim, type in the line and save (:w). – Ilmo Euro Jun 30 '12 at 08:47
  • 6
    @lilroo While this solves your problem, I would advise you against creating a vimrc from scratch. Instead, pickup the example vimrc from the following location: /Applications/MacVim.app/Contents/Resources/vim/runtime/vimrc_example.vim. Copy this file to your $HOME as .vimrc. It switches on a lot of basic stuff for you automatically (syntax, search highlighting, backup etc). You can then tweak it based on your needs. – oyenamit Jun 30 '12 at 14:51
  • 3
    Is there any difference between `syntax on` and `syntax enable`? – Sonique Aug 29 '14 at 15:31
  • 4
    @Sonique `syntax enable` keeps your color settings, `syntax on` overrides them (`:help :syn-on`). – Ilmo Euro Sep 08 '14 at 05:16
  • The Mac folder is `/usr/share/vim/vim73/`. The one in the response supposes you have installed an extra application. – totophe Oct 01 '15 at 13:01
  • Make sure you add `syntax on` to `/etc/vim/vimrc` file (sudo required), to enable syntax on system-wide files (e.g. config files at /etc/) – forzagreen Jan 03 '18 at 20:46
  • Now even `~/.vimrc` is colored :) – Qback Jun 19 '18 at 12:34
11

Also, to highlight the syntax of a Specific File TYPE (or programming language extension), you can use following commands, while file is already opened in Vim and you want to try syntax highlighting on the fly:

:set filetype=php

OR shortcut:

:se ft=php

Above commands will change the syntax-highlighting for currently opened file as it should be for PHP code.

Amit Verma
  • 8,660
  • 8
  • 35
  • 40
  • 1
    The above method is especially helpful for when you have an unsaved file that you want to set the syntax highlighting for (e.g. when you use vim as a notepad with markdown) – Aron Hoogeveen Oct 25 '21 at 09:54
7

Uncommenting the "syntax on" in vimrc file.

Move to the directory,

cd /etc/vim/

vim vimrc

now search "syntax" and uncomment it. Save it and reopen the file in vim.

Anurag Choudhary
  • 752
  • 1
  • 11
  • 16
  • 1
    Best way for me. Tks. If you don't have `syntax` just add, ```syntax enable```. Then ```source vimrc```. You got it permanently for later use – Ender phan Nov 23 '18 at 08:54
3

For anyone that gets here because of TurnKeyLinux using vim-tiny which doesn't have the syntax module enabled try this article to install full vim

http://www.turnkeylinux.org/forum/support/20140108/solved-bash-command-not-found-after-replacing-package

tl;dr

# apt-get remove vim-tiny
# apt-get install vim
# hash vim
# vim
Josh Peak
  • 5,898
  • 4
  • 40
  • 52
  • 1
    FWIW as of v14.0, you can just install vim and it all works as you'd expect (regardless of whether you uninstall vim-tiny or not). It was a bug in the way vim-tiny was configured to run as 'vim'. Now it updates the binary path properly... – Jeremy Davis Dec 17 '15 at 12:26
1

I also found that this is one of the lessons in vimtutor.

To find it, you can type command vimtutor in your Terminal (I used on Mac), and scroll down to see if there is a lesson called CREATE A STARTUP SCRIPT (for me it was Lesson 7.2), where it describes how to set up an initial vimrc file.

Ben
  • 11
  • 1
  • I have trouble with this. Running the tutorial has been fine although I don't have a $VIMRUNTIME set so I have no idea where this vimrc_example.vim is stored. – Nebbles Dec 22 '18 at 17:29
0

In my $HOME/.vimrc I load a color scheme (solarized) and found that I need to place syntax on after I load the plugin. If it's before loading the plugin it doesn't work.

" this turns syntax highlighting on by default

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'lifepillar/vim-solarized8'

syntax on
" this does not turn syntax highlighting on by default

syntax on

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'lifepillar/vim-solarized8'
matt
  • 318
  • 3
  • 11
0

To Find the vimrc_example.vim as suggested in answers above

Command : sudo find /usr -iname "vimrc_example.vim"
musiceni
  • 21
  • 4