Monday, September 6, 2010

Re: Quick way to highlight a report file

[Fixing quotation. Please don't top poste.]

On Tue, September 7, 2010 6:32 am, C K Kashyap wrote:
> On Mon, Sep 6, 2010 at 2:46 PM, Christian Brabandt <cblists@256bit.org>
> wrote:
>> call matchadd('WildMenu', '\_^.*\n\zs\%([^ ]* \)\{3\}\zs[^
>> ]* ')
> Thank you very much Christian ... this is exactly what I was looking for.
> May I request you to explain the expression please.

The Regular Expression boils down to (<tab> is is used in the example to
visualize the white space part. When using the expression, use a literal
Tab instead of <tab>):
\_^.*\n\zs\%([^<tab>]*<tab>\)\{3\}\zs[^<tab>]*<tab>

This means:
\_^ " Start of line followed
.* " Anything or nothing followed by
\n " a line break
\zs " The matching starts right after \zs

So bascially, we skip one line from matching. Following comes the line
that we want to match:
\%( " Start a group at the beginning of a line (remember we are
" after a line break)
[^<tab>]* " Any other charachter but a <tab> followed by
<tab> " a <tab>
\)\{3\} " This group repeats 3 times. (the first 3 columns, we don't
" want to match)
\zs " The match and highlight starts right after here
[^<tab>]* " Anything but a <tab> (can be empty)
<tab> " The <tab> delimiter for the 4th column

Now that I look at it, I notice, that the first \zs can be left out. I
didn't notice yesterday, that I used it twice (which obviously does not
make sense).

By the way, typing this might become awkward, especially if you need to
highlight different columns. So here is a small script, that
defines the :HiCol command to highlight a specific column. Use :HiCol
<nr> to have it highlight column <nr>. It uses tab completion so if you
press <tab> after :HiCol it let's you select the number of columns in
the current line.

Simply save the following lines in a small file called columns.vim in your
plugin folder (e.g. ~/.vim/plugin on Unix or $VIM/vimfiles/plugin on Windows)
and the script will then be loaded automatically whenever you start vim.
(Again, please make sure, the regular expression uses literal tabs instead of
white space.)

:com! -nargs=1 -complete=custom,NrDelimiters HiCol :call
HighlightColumn(<args>)

fun! NrDelimiters(A,L,P)
return join(range(1,len(split(getline('.'), "\t"))), "\n")
endfun

fun! HighlightColumn(nr)
if exists("b:matchcol")
call matchdelete(b:matchcol)
endif
let b:matchcol=matchadd("WildMenu", '\_^.*\n\%([^ ]* \)\{' .
\ (a:nr-1) . '\}\zs[^ ]* *')
endfun

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: