59

How do I enable syntax highlighting in vim on Mac?

4 Answers 4

84

Method 1

The easiest method is to place syntax on into your .vimrc. Though I've found that only having this in the vim resource file might overwrite a vast majority of the default configurations, so I'd recommend also including source $VIMRUNTIME/defaults.vim at the top of the file. You can execute the following command to easily set your .vimrc:

echo "\" Sets the default vim settings. 
source \$VIMRUNTIME/defaults.vim

\" Enable syntax highlighting.
syntax on
" > ~/.vimrc

Note that this will overwrite any current information in .vimrc. If you don't want that to happen, use >> instead of >. But keep in mind that the source should remain somewhere near the top of the file, as to not overwrite any custom configurations.

Method 2

TL;DR ➡ install vim with homebrew then open a new terminal tab

Method 2 is more ideal if you want greater configurability and better default configurations.

First you'll need to install homebrew, which will take some time depending on your Mac's specs: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once homebrew has successfully installed, I recommend ensuring that your system is ready to brew, using brew doctor. Before continuing, make sure that any warnings or errors displayed by the command are resolved.

Next, go ahead and install the newest version of vim using brew install vim. This version of vim should have syntax highlighting enabled by default, along with many other options.

Once vim has been installed, you'll need to reload your shell profile (or simply close your terminal and reopen it). If you don't, the new version of vim won't be used, and therefore won't have syntax highlighting enabled by default.

2
  • Error: invalid option: --override-system-vim Jul 22, 2022 at 7:41
  • @SlavaSemushin At this point, I think that the information in method 2 this answer is somewhat obsolete. I think you just need to do brew install vim instead of including the --override-system-vim
    – Hunter T.
    Aug 5, 2022 at 23:00
20

If you want to toggle this on/off (without creating a .vimrc file) simply type :syntax on while in vi/vim.

2

syntax enable is better for this, read help for further info

if !exists("g:syntax_on")
    syntax enable
    filetype on
endif

" To toggle syntax 
" nnoremap <Leader>s Toggle Syntax
" with a little help from vi.stackexchange.com: https://vi.stackexchange.com/a/24418/7339
nnoremap <silent> <expr> <Leader>s exists('g:syntax_on') ? ':syntax off<cr>' : ':syntax enable<cr>'

Of course, you can define your oun key combination instead of <Leader>s

source: https://stackoverflow.com/a/33380495/2571881 The ":syntax enable" command will keep your current color settings. This allows using ":highlight" commands to set your preferred colors before or after using this command. If you want Vim to overrule your settings with the defaults, use: > :syntax on

2

Merely adding syntax on in .vimrc configuration file is not good enough. Your configurations in this file will still be overridden by system initialisation. The easiest way without recompiling vim is to invoke it with your configuration file as command-line option as follows (all other unknown initialisations will be bypassed):

vim -u ~/.vimrc

You can set up an alias in .bashrc should you use bash shell as follows:

alias vim='vim -u ~/.vimrc'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .