Saturday, January 31, 2015

VIMScript: Highlight Multiple Regex

My job requires me to read some very large and complex log files for troubleshooting.

A few months down the road, I got so sicked that I started to find a way to ease my job. Since I was (and am) using vim and gvim to open those files, it was natural for me to explore whether VIMScript can do some wonders.


Google led me to this awesome VIMScript maintained by John Beckett. However, based on my brief and limited trials (hence I could be wrong :), the script lacks a few things I need:

(1) the script provides functionality to highlight words dynamically, but I need something that can remember my settings (keywords, highlight style, etc.) since I will be re-using the configuration a lot to read files of the same format and with same keywords.


(2) I need a script that would allow me to update or switch configuration on the fly without needing to re-open the input file. Just imagine that you suddenly want a different highlight style for some keywords or you have multiple different configurations for a single file type that you can switch to and fro dynamically.

Hence, despite not being a VIMScript expert, I started to explore how to re-use some of the codes from the original script (after getting permission from John to reuse and share) to do what I need.

The result is this:
FROM
TO

If this interests you enough to try it out, you can follow the instructions below:

For WINDOWS:
(1) Install GVIM (version 7.2 and above) from http://www.vim.org/download.php
(2) Once GVIM is installed, make the directory C:\Users\<USERNAME>\vimfiles\plugin
(3) Download the following archive file into the newly created directory and extract the content (only 2 files) there.
(4) File "highlight.csv" is the configuration file (details on how to configure can be found in the file itself).
(5) Configure as needed.
(6) Open a text file and press "\m".
(7) Enjoy the color coded text :)


For LINUX:
(1) You have a choice of using terminal or GVIM (install vim-x11). If you choose to use terminal, you are limited to a small amount of colors (see below) =>
NOTE: If you decide to use terminal, please configure using the ctermfg and ctermbg columns in the "highlight.csv" file.

   NR-16   NR-8    COLOR NAME ~
   0    0    Black
   1    4    DarkBlue
   2    2    DarkGreen
   3    6    DarkCyan
   4    1    DarkRed
   5    5    DarkMagenta
   6    3    Brown, DarkYellow
   7    7    LightGray, LightGrey, Gray, Grey
   8    0*    DarkGray, DarkGrey
   9    4*    Blue, LightBlue
   10    2*    Green, LightGreen
   11    6*    Cyan, LightCyan
   12    1*    Red, LightRed
   13    5*    Magenta, LightMagenta
   14    3*    Yellow, LightYellow
   15    7*    White

(2) Make a directory <user home directory>/.vim/plugin.
(3) Download the following archive file into the newly created directory and extract the content (only 2 files) there.
(4) File "highlight.csv" is the configuration file (details on how to configure can be found in the file itself).
(5) Configure as needed.
(6) Open a text file and press "\m".
(7) Enjoy the color coded text :)

NOTE: "vi" is not "vim". If you have installed vim, you can create an alias in <user home directory>/.bashrc file to map "vi" to "vim". Remember to source the .bashrc file after amending it.

Eg. 
alias vi="vim"
alias view="vim -R"