VIM:创建语法高亮文件的简单步骤 - 用于日志文件 [英] VIM: simple steps to create syntax highlight file - for logfiles

查看:45
本文介绍了VIM:创建语法高亮文件的简单步骤 - 用于日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(log4j 生成的)日志文件要查看;我非常了解它们的格式(我的意思是我已经有了现成的正则表达式等我可以使用).

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).

我想在加载它们时在 VIM 中自动突出显示它们 (*.log).

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

其中#LOG-LEVEL# 是标准的ERROR"、INFO"、DEBUG"、FATAL"之一......而YYYY-MM..."表示以毫秒为单位的日期/时间分辨率.

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 !

干杯

以下是我根据以下说明所做的总结:

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

  1. 在 .vim\syntax 中创建了语法文件log.vim"(参见下面的示例内容).

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

在 .vim\ftdetect\log.vim 中创建了一个文件,内容如下:

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

au BufRead,BufNewFile *.log set filetype=log

au BufRead,BufNewFile *.log set filetype=log

确保以下在我的启动设置中:

Made sure the following are in my startup settings:

语法文件类型

推荐答案

定义语法项有三种方式(见:help :syn-define):

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

  • 关键字:这些是针对简单的关键字字符串字符串的项目.这是最快的匹配器.
  • 匹配:这些是用于匹配的正则表达式.
  • 区域:这些用于可能包含其他项目的长区域.

有各种参数使事情变得更加复杂(与区域内的匹配等有关),请参阅 :help :syn-arguments 以了解有关此问题的讨论.

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.

有一个优先级生效(见:help :syn-priority).

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

着色由 highlight 命令控制,与语法命令分开.

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

在 ~/.vim/syntax/log.vim 中加载所有这些并确保文件类型设置正确(请参阅 :help filetype.txt) - 然后它应该会自动加载.

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.

希望这能让你有所收获.阅读(非常渐进的):help syntax.txt:help usr_44.txt 的各个部分以获取更多信息.

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.

这篇关于VIM:创建语法高亮文件的简单步骤 - 用于日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆