Thanks a lot, Brett. Your answer is very helpful ! 2009/9/20 Stahlman Family <[email protected]>
> > > > Stahlman Family wrote: > > > > > > Lenin Lee wrote: > >> I want to write notes and save them into plain text files, in my notes, > >> there are always some pieces of code. > >> > >> I want to apply syntax color of that language only to the block of code, > is > >> there a way to do so ? > > > > I think the following tip from the Vim tips wiki describes how to do > > what you want. > > > http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file > > > > If you want to highlight the plain text (non-code) within your file with > > arbitrary formats and colors (i.e., like rtf highlighting), you could > > use the Txtfmt plugin. > > http://www.vim.org/scripts/script.php?script_id=2208 > > > > Here's a quick example of how I included C++ and Txtfmt regions within > > the same file. > > > > --- inside snip.vim --- > > :syntax on > > :syntax include @CPP syntax/cpp.vim > > :syntax region cppSnip matchgroup=Snip start="@begin=cpp@" > > end="@end=cpp@" contai...@cpp > > :hi link Snip SpecialComment > > > > :syntax include @TXTFMT syntax/txtfmt.vim > > :syntax include ftplugin/txtfmt.vim > > :syntax region txtfmtSnip matchgroup=Snip start="@begin=txtfmt@" > > end="@end=txtfmt@" contai...@txtfmt > > :hi link Snip SpecialComment > > > > --- inside snip.txt --- > > @begin=cpp@ > > ... C++ code here... > > @end=cpp@ > > > > @begin=txtfmt@ > > ...Plain text highlighted with Txtfmt here... > > @end=txtfmt@ > > > > Note that you have to source snip.vim from within snip.txt. You can do > > so manually... > > :so snip.vim > > ...but a better approach might be to use an autocommand: e.g., something > > like... > > au! BufRead,BufNewFile *.snip runtime syntax/snip.vim > > If you're using something like Txtfmt to highlight the non-code portions > of the file, you may not want to have to enclose these sections within > explicit @begin / @end markers. > > The following example supports C++ and Perl code blocks with Txtfmt > regions at the top-level (i.e., outside any snippet begin/end markers), > with a very slight modification to Ivan Tischenko's TextEnableCodeSnip > function. > > *** Modification to TextEnableCodeSnip *** > Change... > \ contains=@'.group > ...at the end of the function to... > \ contains=@'.group > \ .' containedin=Tf.*' > > This permits the various code snippet blocks to be recognized *inside* > the Txtfmt syntax regions. Of course, if you want to use a markup syntax > other than Txtfmt for your plain text regions, you would change Tf.* to > a regular expression matching the other script's region names. > > --- snip.vim --- > :syntax on > :runtime syntax/txtfmt.vim > :runtime ftplugin/txtfmt.vim > unlet! b:current_syntax > > call TextEnableCodeSnip('cpp', '@begin=cpp@', '@end=cpp@', > 'SpecialComment') > call TextEnableCodeSnip('perl', '@begin=perl@', '@end=perl@', > 'SpecialComment') > > Note that you would need to ensure that the TextEnableCodeSnip function > definition is sourced before snip.vim: e.g., you could put it in a file > within your plugin directory, or in your .vimrc. > > Brett Stahlman > > > > > Brett Stahlman > > > >> Thanks ! > >> > > > > > > > > > > > -- 初从文,三年不第。遂习武,校场发一矢,中鼓吏,逐之出。后学医,有所成,自撰一良方,服之,卒。 --~--~---------~--~----~------------~-------~--~----~ You received this message from the "vim_use" maillist. For more information, visit http://www.vim.org/maillist.php -~----------~----~----~----~------~----~------~--~---
