Code coloring separates keywords, strings, and comments in Vim. Vim's syntax commands control code coloring for the current session, while a vimrc setting makes the chosen state persistent.
Filetype detection identifies the language of a file. With syntax highlighting enabled, Vim normally loads the matching colors automatically.
Search highlighting is separate from syntax coloring, so search matches can be cleared without turning off the colors.
If only the last search matches need clearing, :nohlsearch removes those highlights until the next search.
Steps to enable or disable syntax highlighting in Vim:
- Open an existing source-code file in Vim.
Syntax highlighting needs the full version of Vim; minimal builds such as vim-tiny leave it out.
- Press Esc to return to Normal mode.
- Run either :syntax on to enable syntax highlighting or :syntax off to disable it.
Either command applies to every file open in this Vim session and is not saved for future sessions.
- Set the language for the current file if the colors do not match it.
:set syntax=php
Other language names include html and python. This setting is not saved when Vim exits.
- Open your vimrc if you want to keep your choice for future sessions.
:edit ~/.vimrc
On Windows, use :edit $HOME/_vimrc. If you keep your settings in ~/.vim/vimrc, open that file instead.
- Add the setting you want as a new line at the end of the file.
syntax on
Use syntax off instead to keep syntax highlighting disabled. A vimrc setting has no leading colon.
- Save your vimrc.
:wq
- Reopen the source-code file in Vim.
- Check that the code shows the state you chose.
With syntax highlighting enabled, keywords, strings, and comments appear in different colors. With it disabled, all the code is one color.