Vim syntax highlighting: make region only match on single line

I defined a custom file type with these lines:

syn region SubSubtitle start=+=+ end=+=+ highlight SubSubtitle ctermbg=black ctermfg=DarkGrey syn region Subtitle start=+==+ end=+==+ highlight Subtitle ctermbg=black ctermfg=DarkMagenta syn region Title start=+===+ end=+===+ highlight Title ctermbg=black ctermfg=yellow syn region MasterTitle start=+====+ end=+====+ highlight MasterTitle cterm=bold term=bold ctermbg=black ctermfg=LightBlue 

I include all my headings in this kind of document as follows:

 ==== Biggest Heading ==== // this will be bold and light blue ===Sub heading === // this will be yellow bla bla bla // this will be normally formatted 

However, now when I use the equal sign in my code, he thinks this is the title. In any case, can I make the match be on only one line?

+6
unix vim
source share
1 answer

UPDATE . My previous answer was wrong, you can do it with a scope, just

 syn region SubSubtitle start=+=+ end=+=+ oneline 

See :help syn-oneline and :help syn-arguments . Guess this shows that I can't start vim right now, hunh?


Previous answer

According to my reading : help syntax , there is no way to do this with scope . However, you can do this with syn-match:

 syn match SubSubtitle /=\@<!=[^=]*==\@!/ 
/ = \ @ <! / says that there is no = immediately before your match, and /=\@!/ says that there is no = immediately afterwards, so this corresponds to exactly one = , group not = (not including new lines - to include newlines, this must be \_[^=] ), and then exactly one = .

The rest are similar

 syn match Subtitle /=\@<!=\{2}[^=]*=\{2}=\@!/ syn match Title /=\@<!=\{3}[^=]*=\{3}=\@!/ syn match MasterTitle /=\@<!=\{4}[^=]*=\{4}=\@!/ 

You can still play matches in sync matches, so if you have any kind of nesting, it will still work.

for example

 syn match Todo /\<TODO\>/ containedin=SubSubtitle,Subtitle,Title,MasterTitle contained 
+6
source share

All Articles