Wikia

Vim Tips Wiki

Changes: Changing the default syntax highlighting

Edit

Back to page

m (Reverted edits by 58.9.37.162 (talk | block) to last version by JohnBot)
Line 1: Line 1:
+
{{TipImported
Headline text
+
|id=24
  +
|previous=23
  +
|next=25
  +
|created=2001
  +
|complexity=intermediate
  +
|author=
  +
|version=6.0
  +
|rating=11/24
  +
|category1=Syntax
  +
|category2=
  +
}}
 
Vim provides syntax files that can provide syntax coloring or folding for certain types of files. This tip discusses how to change some features of the standard syntax files to suit a personal preference. Do not change any of the files distributed with Vim because such updates will be lost when Vim is updated. Instead, use the techniques discussed here.
 
Vim provides syntax files that can provide syntax coloring or folding for certain types of files. This tip discusses how to change some features of the standard syntax files to suit a personal preference. Do not change any of the files distributed with Vim because such updates will be lost when Vim is updated. Instead, use the techniques discussed here.
   
 
==Documentation==
 
==Documentation==
 
Here are some pointers to the Vim documentation on syntax highlighting.
 
Here are some pointers to the Vim documentation on syntax highlighting.
*I want <code>files to be highlighted like HTML files:
+
*I want <code>*.foo</code> files to be highlighted like HTML files: {{help|new-filetype}}
*I want to define a syntax file for <code> Read the above and also
+
*I want to define a syntax file for <code>*.bar</code> files. Read the above and also: {{help|mysyntaxfile}}
*I want to make a few changes to the existing syntax highlighting:
+
*I want to make a few changes to the existing syntax highlighting: {{help|mysyntaxfile-add}}
*I want to change some of the colors from their defaults.
+
*I want to change some of the colors from their defaults. Again, read {{help|mysyntaxfile}}
   
 
==Python triple-quoted comments==
 
==Python triple-quoted comments==
In Vim 7.3 the file <code>vim73/syntax/python.vim</code> provides syntax highlighting for Python files. That file (correctly) treats <code></code> as a string and highlights it accordingly. If you prefer, instances of <code></code> or <code><nowiki><code> that start on the line following a line ending with a colon can be treated as a comment. To do that, create file <code>python.vim<code> in your after syntax directory as in the following. The next Python file you open should treat triple-quoted doc strings in functions as comments includes commands like <code>filetype and <code>syntax on<code>
+
In Vim 7.3 the file <code>vim73/syntax/python.vim</code> provides syntax highlighting for Python files. That file (correctly) treats <code>"""..."""</code> as a string and highlights it accordingly. If you prefer, instances of <code>"""..."""</code> or <code><nowiki>'''...'''</nowiki></code> that start on the line following a line ending with a colon can be treated as a comment. To do that, create file <code>python.vim</code> in your "after" syntax directory as in the following. The next Python file you open should treat triple-quoted doc strings in functions as comments (assuming your [[vimrc]] includes commands like <code>filetype indent plugin on</code> and <code>syntax on</code>).
  +
  +
Add the following to file <code>~/.vim/after/syntax/python.vim</code> (Unix) or <code>$HOME/vimfiles/after/syntax/python.vim</code> (Windows)—you may need to create the directories and the file.
  +
<pre>
  +
syn region pythonComment
  +
\ start=+\%(:\n\s*\)\@<=\z('''\|"""\)+ end=+\z1+ keepend
  +
\ contains=pythonEscape,pythonTodo,@Spell
  +
</pre>
  +
  +
==Comments==
  +
This tip is vaguely related:
  +
*[[VimTip1120|1120 Remove annoyance with syntax highlighting when starting a string]]

Revision as of 16:25, March 8, 2013

Tip 24 Printable Monobook Previous Next

created 2001 · complexity intermediate · version 6.0


Vim provides syntax files that can provide syntax coloring or folding for certain types of files. This tip discusses how to change some features of the standard syntax files to suit a personal preference. Do not change any of the files distributed with Vim because such updates will be lost when Vim is updated. Instead, use the techniques discussed here.

Documentation

Here are some pointers to the Vim documentation on syntax highlighting.

Python triple-quoted comments

In Vim 7.3 the file vim73/syntax/python.vim provides syntax highlighting for Python files. That file (correctly) treats """...""" as a string and highlights it accordingly. If you prefer, instances of """...""" or '''...''' that start on the line following a line ending with a colon can be treated as a comment. To do that, create file python.vim in your "after" syntax directory as in the following. The next Python file you open should treat triple-quoted doc strings in functions as comments (assuming your vimrc includes commands like filetype indent plugin on and syntax on).

Add the following to file ~/.vim/after/syntax/python.vim (Unix) or $HOME/vimfiles/after/syntax/python.vim (Windows)—you may need to create the directories and the file.

syn region pythonComment
      \ start=+\%(:\n\s*\)\@<=\z('''\|"""\)+ end=+\z1+ keepend
      \ contains=pythonEscape,pythonTodo,@Spell

Comments

This tip is vaguely related:

Around Wikia's network

Random Wiki