Vim Tips Wiki
(→‎Comments: let's have a syncolor tip)
(Reduce syncolor mention.)
 
(11 intermediate revisions by 5 users not shown)
Line 5: Line 5:
 
|created=2001
 
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
|author=benji
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=11/24
 
|rating=11/24
Line 11: Line 11:
 
|category2=
 
|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.
  +
  +
==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 <tt>*.foo</tt> files to be highlighted like HTML files: {{help|new-filetype}}
+
*I want <code>*.foo</code> files to be highlighted like HTML files: {{help|new-filetype}}
*I want to define a syntax file for <tt>*.bar</tt> files. Read the above and also: {{help|mysyntaxfile}}
+
*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: {{help|mysyntaxfile-add}}
 
*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. Again, read {{help|mysyntaxfile}}
 
*I want to change some of the colors from their defaults. Again, read {{help|mysyntaxfile}}
   
  +
==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>'''...'''</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.
----
 
{{Todo}} __NOTOC__
 
*I'm parking this information here, while waiting for a better solution.
 
*Need a tip on the <tt>:highlight</tt> command.
 
*Mention all the ways text can be highlighted (search, match, syntax, cursorline).
 
*Links to tips dealing with colors in an xterm.
 
*Links to tips dealing with colorscheme.
 
 
====Highlight examples====
 
Define two highlight groups and use them to show long lines (see [[VimTip810|Highlight long lines]]):
 
 
<pre>
 
<pre>
  +
syn region pythonComment
:highlight NearColLimit term=italic,bold cterm=italic ctermbg=yellow ctermfg=darkblue gui=bold,italic guibg=yellow guifg=darkblue
 
  +
\ start=+\%(:\n\s*\)\@<=\z('''\|"""\)+ end=+\z1+ keepend
:highlight OverColLimit term=inverse,bold cterm=bold ctermbg=red ctermfg=white gui=bold guibg=red guifg=white
 
  +
\ contains=pythonEscape,pythonTodo,@Spell
:syntax match NearColLimit /\%<81v.\%>77v/
 
:syntax match OverColLimit /\%>80v.\+/
 
 
</pre>
 
</pre>
   
 
==Comments==
Here is a simpler example showing that "<tt>highlight</tt>" can be abbreviated to "<tt>hi</tt>", and that you only need to define conditions that are applicable to your usage:
 
  +
This tip is vaguely related:
<pre>
 
:hi LineTooLong cterm=bold ctermbg=red guibg=LightYellow
 
:match LineTooLong /\%>80v.\+/
 
</pre>
 
 
This command shows the current settings for the <tt>LineTooLong</tt> group:
 
<pre>
 
:hi LineTooLong
 
</pre>
 
 
====Tips with "highlight" in name====
 
*[[VimTip14|14 Highlight all search pattern matches]]
 
*24 Changing the default syntax highlighting ''(this tip)''
 
*[[VimTip25|25 Color highlighting on telnet]]
 
*[[VimTip53|53 Better colors for syntax highlighting]]
 
*[[VimTip99|99 Identify the syntax highlighting group used at the cursor]]
 
*[[VimTip111|111 Printing with syntax highlighting independent of your normal highlighting]]
 
*[[VimTip121|121 Using vim as a syntax-highlighting pager]]
 
*[[VimTip126|126 Syntax highlighting in xterm]]
 
*[[VimTip172|172 Using Ispell on a highlighted region]]
 
*[[VimTip206|206 Highlight doubled word errors in text]]
 
*[[VimTip235|235 Highlight current word to find cursor]]
 
*[[VimTip269|269 Refresh out-of-sync syntax highlighting]]
 
*[[VimTip284|284 Print syntax highlighted buffer in one color]]
 
*[[VimTip396|396 Highlighting whitespaces at end of line]]
 
*[[VimTip421|421 The simplest map to highlight the current line]]
 
*[[VimTip449|449 Fortran highlighting problems]]
 
*[[VimTip454|454 Fix syntax highlighting so it keeps working]]
 
*[[VimTip572|572 Auto highlight current word when idle]]
 
*[[VimTip621|621 Vim as a syntax highlighting engine for web publishing]]
 
*[[VimTip639|639 Highlight debug blocks in programs]]
 
*[[VimTip641|641 Highlighting of method names in the definition]]
 
*[[VimTip744|744 Get latest releases for syntax highlighting, runtime, plugins etc]]
 
*[[VimTip754|754 Highlighting source between matching curly braces]]
 
*[[VimTip769|769 Highlight current line]]
 
*[[VimTip810|810 Highlight long lines]]
 
*[[VimTip857|857 Different syntax highlighting within regions of a file]]
 
*[[VimTip941|941 Adding MPI and PVM syntax highlighting]]
 
*[[VimTip969|969 Highlight simple Python syntax errors]]
 
*[[VimTip1017|1017 Highlight text inside matching parentheses]]
 
 
*[[VimTip1120|1120 Remove annoyance with syntax highlighting when starting a string]]
 
*[[VimTip1120|1120 Remove annoyance with syntax highlighting when starting a string]]
*[[VimTip1141|1141 List lines with current search pattern highlighted]]
 
*[[VimTip1152|1152 Improved version of Highlight Matching Paren]]
 
*[[VimTip1174|1174 Syntax highlighting for LJ and html-enabled web forums]]
 
*[[VimTip1193|1193 Syntax highlighting for HTML with embedded Javascript]]
 
*[[VimTip1274|1274 Highlight some whitespace characters]]
 
*[[VimTip1279|1279 Highlight current line using cursorline]]
 
*[[VimTip1293|1293 Highlight the current line in the active window]]
 
*[[VimTip1380|1380 Highlight cursor line after cursor jump]]
 
*[[VimTip1381|1381 Highlight special filetype docs]]
 
 
====Tips relating to color schemes====
 
Also see [[VimTip53|53]], [[VimTip111|111]], [[VimTip284|284]] (listed above).
 
*[[VimTip178|178 Create a color scheme based on another]]
 
*[[VimTip231|231 Localized color schemes]]
 
*[[VimTip341|341 Switch color schemes]]
 
*[[VimTip955|955 Easily switch between two styles of color scheme]]
 
*[[VimTip1036|1036 Change the color scheme]]
 
*[[VimTip1291|1291 Desert color scheme with Vim in PuTTY]]
 
*[[VimTip1305|1305 Define your own colors]]
 
*[[VimTip1312|1312 256 colors in vim]]
 
*[[VimTip1576|1576 Using vim color schemes with Putty]]
 
*[[VimTip1619|1619 Using GUI color settings in a terminal]]
 
 
==References==
 
*{{help|:highlight}}
 
 
==Comments==
 
I want a simple syntax highlighting which does not take attention from the real thing. Color
 
codes are:
 
:Green for comments
 
:Blue for keywords/types
 
:Bluebold for flow-control
 
:Brown or gray for strings and constants
 
:Black (or navy) for the rest
 
 
For that purpose I changed the "syncolor.vim" file (and it works best for light background xterm).
 
Every colleague I have shown this syntax-scheme says that it is superb - it works with your
 
intuition, green is definitely just something which can be ignored. I sent a mail to Bram
 
who answered that we have a repository for config files?
 
 
I wanted to share my syncolor with vim-users, but I cannot find the appropriate place to do so.
 
<pre>
 
SynColor Comment term=bold cterm=NONE ctermfg=DarkGreen ctermbg=NONE gui=NONE guifg=MediumSeaGreen guibg=NONE
 
SynColor Constant term=underline cterm=NONE ctermfg=DarkRed ctermbg=NONE gui=NONE guifg=SlateGrey guibg=NONE
 
SynColor Special term=bold cterm=NONE ctermfg=DarkRed ctermbg=NONE gui=NONE guifg=DarkRed guibg=NONE
 
SynColor Identifier term=underline cterm=NONE ctermfg=DarkCyan ctermbg=NONE gui=NONE guifg=DarkCyan guibg=NONE
 
SynColor Statement term=bold cterm=NONE ctermfg=Blue ctermbg=NONE gui=bold guifg=SlateBlue guibg=NONE
 
SynColor PreProc term=underline cterm=NONE ctermfg=DarkBlue ctermbg=NONE gui=NONE guifg=SlateBlue guibg=NONE
 
SynColor Type term=underline cterm=NONE ctermfg=DarkBlue ctermbg=NONE gui=bold guifg=Blue guibg=NONE
 
SynColor Underlined term=underline cterm=underline ctermfg=DarkMagenta gui=underline guifg=SlateBlue
 
SynColor Ignore term=NONE cterm=NONE ctermfg=white ctermbg=NONE gui=NONE guifg=bg guibg=NONE
 
</pre>
 
 
//Donald Axel//--[[User:Donald j axel|Donald j axel]] 22:54, November 11, 2009 (UTC)
 
 
:Hi Donald, and welcome to the wiki!
 
:I have only a vague idea of Vim's color schemes and syntax highlighting, but I have a feeling that syncolor allows the modification of an existing color scheme (or is it to modify syntax highlighting for a particular file type?). People use a color scheme (see {{help|:colorscheme}}) to define their standard colors. Bram would have been referring to the fact that there are ''many'' color schemes available at vim.org. I just added some more information to [[VimTip341|Switch color schemes]] to show how to locate a color scheme there.
 
:I welcome other opinions, but traditionally we would not make a "tip" of a syncolor example on the basis that scripts should be uploaded to vim.org/scripts. We do have lots of scripts here, but the aim is that tips show techniques and tutorial information; simple scripts (not worth uploading to vim.org) are here, but we hope they are mainly educational.
 
:I suppose we could have a tip on syncolor if there was something educational to say about it (it's a mystery to me – is it extending an existing scheme? replacing?) and your code could be shown as a sample. Or, you could make a user subpage to hold the script (which we would move from here to there). For example, you might make page [[User:Donald j axel/syncolor]].
 
:FYI we remove temporary comments such as these after a few weeks. If you have any questions, reply here, or on my talk page, or anywhere, because Fritzophrenic and I check all changes here. [[User:JohnBeckett|JohnBeckett]] 03:29, November 12, 2009 (UTC)
 
 
::Hi John: The difference between color scheme and syntax highlighting is that the colorscheme affects the Vim window, syntax highlighting affects only the language elements. The two principles can clash, but Vim syncolor.vim has the excellent notion of using an "if" statement to check whether we are on a dark background or a light background.
 
::Maybe it is hard to understand syncolor.vim differences if you do not try them. The code above is '''only''' a section of syncolor.vim. If you do not want to write programs, forget it. However maybe you can imagine the difference between a green comment and a screeming red comment. To my view, as soon as you start using red, purple or magenta the readability decreases.
 
::I am looking into vim.org/scripts but it seems to be mostly text-processing.
 
   
  +
===Changing syncolor.vim===
:I support the idea of a syncolor tip. There are subtle differences between a colorscheme and using syncolor, and from what I understand you should only use one or the other (though I may be wrong about that). The :help is rather cryptic on syncolor; I'd like to see some discussion of syncolor vs. a colorscheme. --[[User:Fritzophrenic|Fritzophrenic]] 15:09, November 12, 2009 (UTC)
 
  +
It is possible to replace Vim's syncolor.vim file to customise syntax highlighting. {{help|syncolor}} See [[Highlight special filetype docs]] which includes a comment that a syncolor.vim alternative is [[User:Donald j axel/syncolor|available here]]. --March 9, 2013

Latest revision as of 01:34, 10 March 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:

Changing syncolor.vim[]

It is possible to replace Vim's syncolor.vim file to customise syntax highlighting. :help syncolor See Highlight special filetype docs which includes a comment that a syncolor.vim alternative is available here. --March 9, 2013