113

How do I do I set the syntax highlighting in Vim 7 for python?

I would like to set my own colorschemes, and syntax highlighting for a type of code file.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
hidroto
  • 1,313
  • 3
  • 10
  • 9

4 Answers4

165

The command to enable syntax highlighting in vim is :syntax on, if you want it to be active everytime you launch vim, just add a line containing syntax on in your .vimrc file.

If you're already editing a Python file and syntax highlighting wasn't on, after enabling syntax highlighting (as above) then: :set filetype=python

Mike S
  • 1,235
  • 12
  • 19
mdeous
  • 17,513
  • 7
  • 56
  • 60
  • 45
    maybe your vim doesn't have filetype detection enabled, try adding `filetype on` to your `.vimrc` or type `:set filetype=python` while editing a file. – mdeous Jan 22 '11 at 21:14
  • i think that vim is broken becuse :help gives E433: No tags file E149: Sorry, no help for vi_help.txt Press ENTER or type command to continue – hidroto Jan 23 '11 at 05:31
  • 2
    maybe try reinstalling it, either with your package manager or from sources, never had these errors, can't help you for this. – mdeous Jan 26 '11 at 16:02
  • 2
    thanks anyway it may have something to do with useing "..The vim-minimal package includes a minimal version of VIM,...." according to package manager – hidroto Jan 27 '11 at 07:14
  • Pretty hard to detect a filetype unless it reads the first line of the file. I never use extensions for commands. filetype=python seems to work. – SDsolar Jun 08 '17 at 06:27
  • @hidroto `vim-tiny` by default provides no help, or any of the truly powerful features of vim. – D. Ben Knoble Dec 22 '18 at 23:21
  • None of this works for me. Tried adding `filetype on` and `syntax on` on `.vimrc`, also `:set filetype=python` while editing the file, and added the shebang as suggested elsewhere :( – Amy Pellegrini Jun 22 '19 at 17:38
  • @hidroto, a tag file must be created independently of Vim. One easy option is to use a program like Exuberant Ctags, which will do it for a whole directory tree in one quick command. See the Vim docs (which are good) at http://vimdoc.sourceforge.net/htmldoc/usr_29.html#29.1, and https://vimways.org/2018/you-should-be-using-tags-in-vim/. – John Nov 06 '19 at 07:33
  • for noobs not knowing the location of `.vimrc` file, here you go: `echo "syntax on" >> ~/.vimrc` – enator Jan 05 '21 at 14:37
13

Put the line syntax on in your .vimrc.

Robie Basak
  • 6,492
  • 2
  • 30
  • 34
9
  1. Verify that you have the latest version of vim, equally execute sudo apt-get install vim
  2. Modify the .vimrc file with the instruction echo "syntax on" >> ~/.vimrc
  3. Open the file with vi app.py. You will see the Syntax highlighting

enter image description here

Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
Revol89
  • 871
  • 8
  • 4
1

If you're on *nix system (linux, macos) or cygwin and if you already have vim :set syntax set but your filename doesn't end with .py you can add a shebang to the first line of your file:

#!/usr/bin/env python 

Next time you open the file in vim, you should see syntax highlighting. Note this will work for other file types, you just use the interpreter name i.e. (python, ruby, sh)

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48