Learn Practice Daily golf Reference

How to turn on syntax highlighting in Vim

Type :syntax on and press ↵ to turn syntax highlighting on, or :syntax off to turn it off. To have it every time, put syntax on in your ~/.vimrc.

TypeWhat it does
:syntax on ↵Turn highlighting on, using the default colours. It also turns on file type detection.
:syntax enable ↵The same, but keeps colours you have already set with :highlight.
:syntax off ↵Turn highlighting off.
:set filetype? ↵Show which language Vim thinks the file is. Empty means none.
:set filetype=python ↵Highlight this file as Python, whatever its name.
:colorscheme and TabStep through the installed colour schemes; ↵ picks one, such as habamax or desert.
:set background=dark ↵Tell Vim the background is dark, so the colour scheme uses colours that show up on it.
:syntax sync fromstart ↵Fix colours that go wrong partway down a long file, by working them out from the top.
Practise the keys

Colour on, from memory

Type each command as you would in Vim, Enter included, with the command and option names spelled out in full.

Turn syntax highlighting on.
scroll here, then press the keys11 keys
1 of 5
Turn syntax highlighting on.

Keep it on

For Vim, put these two lines in ~/.vimrc:

  • syntax on
  • filetype plugin indent on

The first turns on highlighting. The second loads each language's file type plugin and indent rules, so Python files get Python indenting, for example. Neovim does both by default, so it needs neither line; to change its colours, put vim.cmd.colorscheme('habamax') (or any other scheme) in ~/.config/nvim/init.lua.

Why there is no colour

You created a `~/.vimrc`. Without one, Vim loads defaults.vim, which runs syntax on and filetype plugin indent on for you. Once any ~/.vimrc exists it no longer does, so highlighting disappears the day you add your first setting. Add the two lines above.

The Vim that comes with macOS skips defaults.vim (its system vimrc sets skip_defaults_vim), so it shows no colour until your ~/.vimrc says syntax on.

A minimal Vim. On many Linux systems vi is a small build without syntax support, and :syntax on answers E319: Sorry, the command is not available in this version. Install the full vim package; vim --version should list +syntax.

No file type. A file Vim can't recognise, like a script called deploy with no #! line at the top, gets no highlighting. :set filetype? shows an empty value; :set filetype=sh fixes it.

Neovim and Tree-sitter

Besides Vim's syntax files, Neovim can highlight with Tree-sitter, which parses the code properly and colours it more accurately. Recent versions use it out of the box for a few file types, such as Lua and Neovim's help pages. For other languages you need a parser for that language, usually installed with the nvim-treesitter plugin, and highlighting started with vim.treesitter.start() or the plugin's settings. Without one, Neovim falls back to the same syntax files as Vim.