Search code examples
vimsyntax-highlightingvim-syntax-highlighting

Vim turn on syntax highlighting


I've been using Vim as my text editor, but now that I'm using it to write Python code, I'd like it to hightlight the syntax like this:

How do I turn on syntax highlighting in Vim? How do I get it to stay on by default for programming files?


Solution

  • Turning On Syntax Highlighting:

    1. Open the file that you'd like to have syntax highlighting enabled for with vim filename.py.
    2. Press esc, then type :syntax on and hit enter.
    3. Syntax highlighting should now be enabled!

    Note: This will only turn on syntax highlighting temporarily. In other words, if you quit working on that file and open it again, or open a different file with vim, syntax highlighting will be off by default. If you'd like to make syntax highlighting permanent, you need to do some extra steps.

    Enabling Syntax Highlighting by Default:

    1. Change directory to your home directory by typing cd into the terminal.
    2. Type vim .vimrc, which will edit the .vimrc file if you have it, or create it if you don't.
    3. Append syntax on to the file as a new line, then hit esc, type :wq, and hit enter.

    Syntax highlighting will now be enabled by default for files ending in common programming language extensions such as .py.

    If you would like to turn off the syntax highlighting for a particular file, you can do that on a case by case basis.

    Turning Off Syntax Highlighting for a Specific File:

    1. Open the file that you'd like to have synta highlighting disabled for with vim filename.py.
    2. Press esc, then type :syntax off and hit enter.
    3. Syntax highlighting should now be disabled!