All Articles

Vim plugin to work with text highlighting

vim syntax match plugin

Are you Vim user and have you ever investigated long server logs with multiple identifiers needed to be tracked? Check out my plugin Vim syntax match that can help you with it.

Topic background

This happens to me quite often with WildFly and transaction identifiers. Thus I was searching how to make my life easier in this respect. I found that colorizing referred parts of the log helps me to follow and understand what has happened.

Vim provides match command (and its alternatives 2match and 3match). You say what color to use and what text to mark with the color. You do it in way :match <color> /<pattern>/. The example would be for :match DiffAdd /2018-[^ ]*/. This expression colorize text starting with 2018- up to the space in the text with color named DiffAdd. You can search for available colors when you write :highlight (shortcut as :hi). And you can define your own color with :hi <new-color-name> <terminal-type>=<color-code> (see details at :help hi). As an example :hi yellow guibg=#dfff87 ctermbg=192 where we define a color name yellow to colorize background with color of code #dfff87 and 192 respectively. Check the color codes for example here https://jonasjacek.github.io/colors/.

vim match command

But this is restricted only to three text patterns to be highlighted. If you need more you can define your own arbitrary syntax match rules with :syntax match DiffAdd /2018-[^ ]*/. This sounds fine but it requires more text typing than I liked.

The second pain point that I feel was loosing all the knowledge connected with the hightligting the important parts of the texts (in my case they were identifiers in the server log) when I closed the file. I longed for the tool which preserves the colors for me.

Vim synax match plugin

As I haven’t found any I decided to created a Vim plugin adressing all my desires.

What it offers? As it was said:

  • text highlighting shortcuts for several colors, like :Y <pattern> for yellow background or :GF <pattern> for green foreground

  • saving the existing highlighting pattern to be loaded next time the file is opened (automatically works with .txt and .log files)

If you install it and wish to get highlighted the pattern introduced in the initial section of this blogpost, you can write just(!) :Y 2018-[^ ]*.

If you want to know what all the shortcuts the plugin offers don’t hesitate to check the helper commands at the README file and you can read all the details how the plugin works at https://github.com/ochaloup/vim-syntax-match#how-works-internally.

How to install

In short you need to have installed pathogen plugin and then just clone the vim syntax match respository under folder .vim/bundle.

For whole installation guidelines see README file at https://github.com/ochaloup/vim-syntax-match#how-to-install.

Tips

  • Do you know that you can open a secondary file as a vertical Vim window having those to being scrolled together? Try :vsplit <path-to-file> and :set scrollbind. Alternatively use plugin user command :SCB

  • Do you know Vim can export the opened file as HTML page where highligting is applied? Try to run :Tohtml. Alternatively use the plugin user command :TOHtmlWithLines which adds clicable line numbers on top of the output provided by :Tohtml.

Published May 4, 2018

Developer notes.