Quickly highlight a column range in vim
Asked Answered
E

1

6

When working with positional data files, it is often very useful to highlight a specific column. In such files it is quite common to have huge "areas" filled with spaces (or NULL values) and only very sparse data points. In such cases it becomes difficult to read the file.

Here's an example excerpt:

                                                   462                                                   
                                                 63082                                                   
                                                 01089                                                   
                                                 75518                              735301               

                                                 53473                              017146               
                                                                                     37217               
                                                                                        07               
                                                                                    940376               
                                                   762                                2842               

                                                                                     88331               
                                                 40680                                8928               
            645718                                                                                       
                                                  0131                                                   
                                                                                     03522               

             47210                                                                   27431               

             93837                                                                                       
                                                                                   8825072    49479415   

                                                 52084                                8940               
                                               0591705                              205635               
                                                                                    525429               
                                                 65339                                 300               


                                                  0397                                                   
                                                                                      1983               
                                                     0                                                   
                                                                                   2605768               
            121991                                                                     648               
                                                  3892                                                   

                                                  1260                                                   

I found it helpful to simply highlight a specific column. At first I tried to use a regular :match, but this turned out to be way to slow on huge data files. I posted this as another question. The answer is simple. cursorcolumn (available since vim 7.3) can be set to a range and is a lot faster as it does not need to match the characters.

Implementing the proposed solution I saw it working. But it's cumbersome, and - knowing vim - there should be an easier way to specify this.

Question:

Is it possible to set the cursorcolumn range to the columns of the currently selected (visual) block?

Endocrinotherapy answered 20/7, 2011 at 7:56 Comment(0)
E
8

It turns out it's very simple. The function getpos can retrieve the position of any marker. We'll use this to extend the solution to the earlier problem:

:let &l:cc = join(range(getpos("'<")[2], getpos("'>")[2]),',')

Now, we can map this easily:

:vnoremap <F5> <ESC>:let &l:cc = join(range(getpos("'<")[2], getpos("'>")[2]),',')<CR>

So, now when editing a positional data file, we'll simply have to enter visual mode, select the portion of the file we need highlighted and press F5

Endocrinotherapy answered 20/7, 2011 at 7:59 Comment(4)
Why you are using vmap instead of vnoremap here?Abutter
@ZyX: because, honestly, I don't know the difference between the two and vmap is shorter to type. ;)Endocrinotherapy
@ZyX: following your comment, I read through the docs. And indeed, I could have used vnoremap here. And I can see now why it could be considered better. In light of this, I'll update the post to reflect this. Thanks for the nudge ;)Endocrinotherapy
This is wonderful. One addition: set cc=0 can be used to turn off the column highlight.Butlery

© 2022 - 2024 — McMap. All rights reserved.