If you do log investigation it’s handy to differentiate identifiers with colors.
When you close Vim
and later open the log file again it’s fine to have preserved the colors.
-
install the syntax match plugin
-
open file with suffix
.log
or.txt
-
use command
:Y search_pattern
to highlight some text-
you’ve used the prepared shortcuts for coloring log parts
-
-
quit the file, then open it again
-
syntax is loaded from a saved syntax file and restored for you (see How works internally)
-
When installed the syntax match auxiliary files are automatically created for .txt
and .log
files.
To disable the automatic creation define in your ~/.vimrc
variable
" disable default behaviour for txt and log files of the syntax match plugin
let syntax_match_disable=1
This plugin offers functionality to save syntax match
rules to an auxiliary file placed beside of file with the original content.
Then the syntax highligting will be restored based on the saved rules in the auxiliary file.
Beside of having capability to restore defined syntax match rules when specific files is opened again
it tries to solve trouble that I have with vim match
command (:match Comment /pattern/
).
You can match only one pattern at time.
When using match
again the previously hightlighted text is not hightligted anymore.
I wanted chance to add highligting dynamically for different patterns with the same 'color'.
Sure there are commands 2match
and 3match
but for me it’s not normally enough (see my usecase).
The easiest way to install is use pathogen plugin: http://www.vim.org/scripts/script.php?script_id=2332
For installation steps for pathogen plugin see the link above or do following.
This downloads file with the pathogen plugin to directory ~/.vim/autoload
(see http://learnvimscriptthehardway.stevelosh.com/chapters/42.html)
and loads pathogen plugin each time when a vim is used.
-
mkdir -p ~/.vim/autoload ~/.vim/bundle
-
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
-
add line
execute pathogen#infect()
to your~/.vimrc
file
Now the installation of this plugin is easy like
-
git clone git@github.com:ochaloup/vim-syntax-match.git ~/.vim/bundle/vim-syntax-match
Note
|
You can have troubles with using syntax highligting or with showing colours.
Summarized
|
Whenever you open any .log
or .txt
file the plugin is activated.
Now you can use syntax match
(:help syn-match
) command to have specific
highlighting for this file.
Note
|
Syntax match command has basic syntax
|
Save the file and quit.
Next time when you open the file the syntax will be loaded.
So enjoy!
If you open any file of different type you can call command :SaveSyntax
(shortcut command name is :SS
).
At that time the syntax match rules are saved to the auxiliary file.
When you open the same file next time syntax rules are loaded and shown.
For you not need to repeatedly write command sentence :syn match Boolean /pattern/
plugin brings two things.
Predefined hightlightings named as colors. You can write :syn match red /pattern/
.
For you to see what colors are defined see ~/.vim/bundle/vim-syntax-match/colors/syntaxmatch.vim
.
Predefined user commans which abbreviate necessity to write the syn match color
.
You can write only :Yellow pattern
. Or more abbreviated only :Y pattern
.
Command | Abbreviation | Example | Meaning |
---|---|---|---|
Yellow |
Y |
|
|
Yellow2 |
Y2 |
|
|
Yellow3 |
Y3 |
|
|
Yellow4 |
Y4 |
|
|
Red |
R |
|
|
Red2 |
R2 |
|
|
Red3 |
R3 |
|
|
Red4 |
R4 |
|
|
Orange |
O |
|
|
Orange2 |
O2 |
|
|
Orange3 |
O3 |
|
|
Violet |
V |
|
|
Violet2 |
V2 |
|
|
Violet3 |
V3 |
|
|
Violet4 |
V4 |
|
|
Violet5 |
V5 |
|
|
Green |
G |
|
|
Green2 |
G2 |
|
|
Green3 |
G3 |
|
|
Green4 |
G4 |
|
|
Blue |
B |
|
|
Blue2 |
B2 |
|
|
Blue3 |
B3 |
|
|
Blue4 |
B4 |
|
|
Grey |
GY |
|
|
Grey2 |
GY2 |
|
|
Grey3 |
GY3 |
|
|
Brown |
BR |
|
|
Cyan |
C |
|
|
White |
W |
|
|
Pink |
P |
|
|
Pink2 |
P2 |
|
|
Pink3 |
P3 |
|
|
Whitefg |
WF |
|
|
Cyanfg |
CF |
|
|
Brownfg |
BRF |
|
|
Greyfg |
GYF |
|
|
Bluefg |
BF |
|
|
Greenfg |
GF |
|
|
Violetfg |
VF |
|
|
Orangefg |
OF |
|
|
Redfg |
RF |
|
|
Yellowfg |
YF |
|
|
Pinkfg |
PF |
|
|
YA |
|
|
|
RA |
|
|
|
GA |
|
|
|
BA |
|
|
|
GYA |
|
|
|
BRA |
|
|
|
CA |
|
|
|
WA |
|
|
|
PA |
|
|
|
ClearAllSyntax |
SyntaxClearAll |
|
clearing syntax in all opened buffers |
-
Plugin defines autocommand for
.log
and.txt
to call functionsyntaxmatch#saveSyntax()
function at time when such file is closed. -
syntaxmatch#saveSyntax()
function calls Vim internal command:syntax
which list all currently defined syntax highlighting rules. -
Function filter only those which uses
match
(see http://learnvimscriptthehardway.stevelosh.com/chapters/46.html or:help syn-match
). -
It converts output of
:syntax
command and creates validsyntax match <color> /pattern/
commands. -
These commands are then saved to auxiliary file with name pattern
.<original_file_name>.syntax
. That means after syntax is saved you can find a.syntax
file besides of your original file. -
When a file is opened then it check existence of the
.syntax
file with the same name. -
If the auxiliary file (
*.syntax
) exists then its content line by line is executed.
Warning
|
A file could be highlighted with different syntax highlighting rules before you start to use
your own. That’s caused by other plugin or syntax rules (see http://learnvimscriptthehardway.stevelosh.com/chapters/45.html).
All rules that uses There could be a clash between rules. Meaning two match patterns could hightlight the same text.
Which pattern is used is resolved based priority rules (see As rules are saved and then loaded from a file there is no check for their order. That causes that highlighting result will be different when edited and then later when loaded. Rules could beat each other when their patterns overlaps. |
Vim comes with handy command :TOhtml
which takes your text document and convert it to html file.
That html file is highighted as you defined it with syntax
commands.
This plugin offers small enhancement to the internal command that adds clickable line numbers.
When command :TOHtmlWithLines
is executed you can click
on the shown line numbers to get link with anchor (#
)
which can be then shared with a co-worker.
If you investigate some logs which are similar but not the same and you want to see them in sync one beside other
try vim command :set scrollbind
. Any file (buffer) that has set this is then scrolled in sync with all other
buffers that used this settings as well.
Abbreviation is :set scb
and unsetting is done by :set scb!
.
Usage is like
-
open a file
vim filename
-
:set scb
-
open other file as new window in vim
:vsplit otherfile
-
:set scb
-
scroll
(for switching from one window to other you can use shortcut CTRL+W W
)
The plugin adds special user command named SCB
which you can run as :SCB
and it’s functionality is to run scroll bind on all opened windows.
This user command does the same as when you run manually :windo set scb
.
My usecase is for investigation in log files. I have usually works with logs from WildFly app server where I investigate issues connected to Narayana transaction manager. That means that I met long transaction ids on several places of the log file and it’s very useful to differentiate them visually by coloring them.
This is especially good when I need to connect ids of JMS or JDBC with transaction ids. I normally uses the same color for the connected identifiers and then I’m able to follow what happens in the log file.