Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom syntax highlighting in Vim

how to highlight strings that begin with sql_ and are inside quotes?

My Logfile:

MGPostgreSQLConnection.OpenQuery; "sql_p_factory_history"-ExecTime: 47ms
2010-11-12T17:28:18+01:00;custom; MGPostgreSQLConnection.OpenQuery; "sql_p_factory_history"-ExecTime+FetchTime: 47ms
2010-11-12T17:28:18+01:00;custom; MGPostgreSQLConnection.OpenQuery; "sql_factory"-ExecTime: 47ms
2010-11-12T17:28:18+01:00;custom; MGPostgreSQLConnection.OpenQuery; "sql_factory_contactperson"-ExecTime+FetchTime: 62ms

My vimrc (This doesn't work, of course):

au BufRead,BufNewFile *.log syn match "sql_*"
au BufRead,BufNewFile *.log hi sql guifg=white guibg=red
like image 275
JAVH Avatar asked Nov 12 '10 17:11

JAVH


People also ask

How do I add highlighting code in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does vim have syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

How do you color code in Vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.


2 Answers

You were nearly there! This version (works and) doesn't highlight the quotation marks.

au BufRead,BufNewFile *.log hi sql guifg=white guibg=red ctermfg=white ctermbg=red
au BufRead,BufNewFile *.log syn match sql /"\zssql_\w*\ze"/

screen-shot

See the following for more information:

  • :help :syn-match " for syntax matching, erm, syntax
  • :help /\zs " sets the start of the match there
  • :help /\ze " sets the end of the match there
  • :help /\w " word character

Debugging:

The command :verbose :syn should give you something like this:

--- Syntax items ---
[...]
sql            xxx match /"\zssql_\w*\ze"/

And :verbose :hi:

        Last set from ~/.vimrc
[...]
sql            xxx cterm=bold ctermfg=7 ctermbg=1 guifg=white guibg=red

The xxx should be in the same colours as you have specified (and look like the highlighting in my screen-shot). If you don't see those, check that your .vimrc (or _vimrc on Windows) is sourced:

:scriptnames
1: /home/javh/.vimrc
[...]

Of course this only works when:

:echo has('syntax')

...returns 1 (or :version includes +syntax).

like image 137
Johnsyweb Avatar answered Sep 21 '22 17:09

Johnsyweb


The syntax for syn match is syn match highlight_group reg_exp.

So, try :

au BufRead,BufNewFile *.log syn match Todo /"sql_\w\+"/

Why don't you read the help for :syn? Vim help is so good that you find all answers fast :)

:help :syntax
like image 31
Benoit Avatar answered Sep 21 '22 17:09

Benoit