How do I enable syntax highlighting in vim on Mac?


There are two common methods:

Method 1: One of the easiest and most commonly recommended methods is by creating a file called .vimrc in your home directory. The one and only command you need to run is echo syntax on >> ~/.vimrc. The next time you access a non-txt file with vim, you will notice highlighted text.

echo "syntax on" >> ~/.vimrc

Method 2: Another method, one that I personally use/used, is by installing "vim --override-system-vim". First run vim --version in the terminal. Notice the +(s) and -(s) and keep it in mind, maybe even take a picture. Next we will install a package manager, if one is not already installed on your computer. We will use Homebrew for this tutorial. Run xcode-select --install in the terminal. It will take about 10-15 min to install the Xcode Command Line Tools. Once the installation is complete, run ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)". After brew has installed, run brew doctor to make sure that everything is in working order. If it recommends you run any commands to resolve an issue, run them first before continuing. After the previous step is complete, run brew install vim --override-system-vim. After vim is installed, run vim --version and notice the difference of the +(s) and -(s) in comparison to when you first ran the command. Not only has syntax highlighting been enabled, but there are other features that have been enabled as well.

Commands: (Note: The $ signs represent the start of a new line/command. Lines that starts with ## are comments)
$ cd
$ vim --version
$ xcode-select --install
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew doctor
#resolve any issues that are provided by brew doctor
$ brew install vim --override-system-vim
$ vim --version

if you want to toggle this on/off (without creating a .vimrc file) simply type

:syntax on

while in vi/vim


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

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

" 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


if you run echo syntax >> ~/.vimrc (pay attention, there isn't "on" after syntax) as one of the answers up there suggested, and then vim randomfile.py, this may be the terminal's response:

No Syntax items defined for this buffer

Press ENTER or type command to continue

and you press enter it goes to vim displaying the code with no syntax at all.

However, if you just copy and paste @StrangeRanger's line, which is this:

echo "syntax on" >> ~/.vimdc

press enter, and then run vim randomfile.py, you got your syntax all over vim.

The aforementioned line which lacks 'on' is misleading.

You must log in to answer this question.

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