Vim syntaxfile: Difference between revisions

From wikinotes
Line 82: Line 82:
<blockquote>
<blockquote>
<syntaxhighlight lang="vim">
<syntaxhighlight lang="vim">
syntax match
" keyword matches are literal word-matches
   \ " ...
syntax keyword mySqlKeywords
   \ SELECT INSERT INNER LEFT JOIN


syntax region
" matches are regex matches
   \ " ...
syntax match myComments
  \ '\(--\|#\)'
 
" regions let you define (potentially multiline) regions
" you can have different rules within different types of regions
" (contains defines matches/keywords that apply within the region)
syntax region myBlock
  \ start='{' end='}'
  \ transparent
   \ contains=mySqlKeywords,myComments
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Match Regions -->
</blockquote><!-- Match Regions -->
= Highlight =
<blockquote>
You apply highlights to captured keywords, matches, or regions.
<syntaxhighlight lang="vim">
" apply style to `someKeywordOrMatch`
highlight someKeywordOrMatch
  \ cterm=italic ctermfg=2      ctermbg=160
  \ gui=italic  guifg='#111111' guibg='#FFFFFF'
</syntaxhighlight>
</blockquote><!-- Highlight -->

Revision as of 15:34, 30 July 2023

Syntax for writing vim syntax-files.

Documentation

:h syn-match https://vimhelp.org/syntax.txt.html#%3Asyn-match
:h syn-region https://vimhelp.org/syntax.txt.html#%3Asyn-region
:h syn-arguments (ex. conceal, cchar, contained, ..) https://vimhelp.org/syntax.txt.html#%3Asyn-arguments

Locations

${vim-or-plug}/syntax/${vim_filetype}.vim syntax files

Example

ftdetect/my_language.vim


Register a filetype based on a file-extension

" ftdetect/my_language.vim

au BufRead,BufNewFile *.todo set filetype=todolist_simple


syntax/my_language.vim


Define the syntax-highligting for the filetype

" syntax/my_language.vim

" don't reload if already loaded
if exists('b:my_language_syntax')
    finish
endif

" Define marker colours (CLI/GUI)
let s:todo_colour     = 'magenta'
let s:gui_todo_colour = '#F57900'

" Define Regexes
let s:todo_regex = '\(^\s*\)\@<=\*\([a-zA-Z]\)\@!'

" Define syntax for Regexes
execute "syntax match my_language_todo  '". s:todo_regex ."'"

" Apply highlighting
execute ' highlight todolistsimple_todo ctermfg='. s:todo_colour .' guifg='. s:gui_todo_colour .' ctermbg=none cterm=bold gui=bold'

" mark file as loaded
let b:my_language_syntax = 1


Basics

Match Regions

" keyword matches are literal word-matches
syntax keyword mySqlKeywords
  \ SELECT INSERT INNER LEFT JOIN

" matches are regex matches
syntax match myComments
  \ '\(--\|#\)'

" regions let you define (potentially multiline) regions
" you can have different rules within different types of regions
" (contains defines matches/keywords that apply within the region)
syntax region myBlock
  \ start='{' end='}'
  \ transparent
  \ contains=mySqlKeywords,myComments

Highlight

You apply highlights to captured keywords, matches, or regions.

" apply style to `someKeywordOrMatch`
highlight someKeywordOrMatch
  \ cterm=italic ctermfg=2      ctermbg=160
  \ gui=italic   guifg='#111111' guibg='#FFFFFF'