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.

Command: (Note: The $ signs represent the start of a new line/command)
$ 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
  • 1
    Why do you source the file? This will try to load it into he current shell (which probably doesn‘t understand the syntax command). Also the instructions for method 1 are misleading, the $ should not be part of the .vimrc file. Basically a simple echo syntax >> ~/.vimrc will do. – nohillside Mar 24 '18 at 21:48
  • @patrix You source the file to make its contents available for the current session. If you changed the .bashrc or .bash_profile file, the changes would not be accessible until you ran bash in the terminal or you open a new terminal window. This is the same with the newly created .vimrc file. And as I said in the answer, "The $ sign represents the start of a new line/command." But I will remove them if it is that confusing. – StrangeRanger Mar 24 '18 at 21:57
  • You do not need to source a .vimrc into the shell at all. It will be read by vim the next time you start vim. – nohillside Mar 24 '18 at 22:01
  • And conventionally $ is sometimes used to indicate things you type into the shell itself, but definitely not for things you add to a file. – nohillside Mar 24 '18 at 22:02
  • 1
    @patrix Oh, ok. I just changed a bit of the answer. Thank you for pointing out my mistake/unnecessary commands. – StrangeRanger Mar 24 '18 at 22:12

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

:syntax on

while in vi/vim

You must log in to answer this question.

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