Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: simple steps to create syntax highlight file - for logfiles

I have some (log4j generated) logfiles to go through; I know their format pretty well (I mean I have already got off-the-peg regexes etc I can use).

I want to automatically highlight them in VIM when I load them up (*.log).

A logfile entry looks something like this:

YYYY-MM-DD HH:MM:ss,SSS [...] #LOG-LEVEL# [...] Message 

Where #LOG-LEVEL# is one of the standard 'ERROR', 'INFO', 'DEBUG', 'FATAL'....and the 'YYYY-MM...' represents the date/time to millisecond resolution.

To get me started , what are the steps needed to get the date-string highlighted in (say) yellow-background with blue text - and also when the text reads ' ERROR ' this should have a red-background with white text.

I have tried going through some tutorials on this, but can't find one which is simple enough to understand, so I'm after some real basic steps here !

Cheers

EDIT: Here's the summary of what I did, based on the instructions below:

  1. Created the syntax file 'log.vim' in .vim\syntax (see below for example content).

  2. Created a file in .vim\ftdetect\log.vim with the following content:

    au BufRead,BufNewFile *.log set filetype=log

  3. Made sure the following are in my startup settings:

    syntax on filetype on

like image 961
monojohnny Avatar asked Feb 03 '10 15:02

monojohnny


People also ask

How do I create a syntax highlight 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 I highlight keywords in vim?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.

How do I set default syntax in vim?

Just type :e ~/. vimrc to vim, type in the line and save (:w).


2 Answers

There are three ways of defining syntax items (see :help :syn-define):

  • Keywords: these are for items that are simple strings of keyword characters. This is the fastest matcher.
  • Matches: these are regular expressions for matching.
  • Regions: these are for long regions that are likely to contain other items.

There are various arguments that make things more complicated (to do with matches within regions etc), see :help :syn-arguments for a discussion of this.

There is a priority that comes into effect (see :help :syn-priority).

Colouring is controlled by the highlight command and is separate to the syntax commands.

A simple way to get started would be to use a match to detect the date and a keyword to detect error. Then use highlight to make the colours come to life:

" This creates a keyword ERROR and puts it in the highlight group called logError :syn keyword logError ERROR " This creates a match on the date and puts in the highlight group called logDate.  The " nextgroup and skipwhite makes vim look for logTime after the match :syn match logDate /^\d\{4}-\d\{2}-\d\{2}/ nextgroup=logTime skipwhite  " This creates a match on the time (but only if it follows the date) :syn match logTime /\d\{2}:\d\{2}:\d\{2},\d\{3}/  " Now make them appear: " Link just links logError to the colouring for error hi link logError Error " Def means default colour - colourschemes can override hi def logDate guibg=yellow guifg=blue hi def logTime guibg=green guifg=white 

Bung all of that in ~/.vim/syntax/log.vim and make sure the file type is set properly (see :help filetype.txt) - it should then load automatically.

Hopefully that should give you something to get going with. Have a (very gradual) read of the various sections of :help syntax.txt and :help usr_44.txt for further info.

like image 107
DrAl Avatar answered Sep 20 '22 02:09

DrAl


You can start from

syn match group1 /^\d\+-\d\+-\d\+/ nextgroup=group2 skipwhite syn match group2 /....../ nextgroup=group3 contained skipwhite syn match group3 /....../ nextgroup=group4 contained skipwhite  hi link group1 Comment hi link group2 Conditional hi link group3 Identifier 

and then just continue to experiment

like image 25
Mykola Golubyev Avatar answered Sep 19 '22 02:09

Mykola Golubyev