lilroo
lilroo

Reputation: 3128

Enable Vim Syntax Highlighting By Default

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?

Upvotes: 154

Views: 107410

Answers (7)

Amit Verma
Amit Verma

Reputation: 9474

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

:set filetype=py

OR the shorter format:

:se ft=py

Above Vim commands will change the syntax-highlighting for currently opened file as it should for a Python file/script.

Upvotes: 15

musiceni
musiceni

Reputation: 31

To Find the vimrc_example.vim as suggested in answers above

Command : sudo find /usr -iname "vimrc_example.vim"

Upvotes: 1

tomato_soup
tomato_soup

Reputation: 354

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'

Upvotes: 1

Ben
Ben

Reputation: 21

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.

Upvotes: 2

Ilmo Euro
Ilmo Euro

Reputation: 5115

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.

Upvotes: 234

Josh Peak
Josh Peak

Reputation: 6237

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

Upvotes: 4

Anurag Choudhary
Anurag Choudhary

Reputation: 890

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.

Upvotes: 8

Related Questions