Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly highlight a column range in vim

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?

like image 447
exhuma Avatar asked Jul 20 '11 07:07

exhuma


People also ask

How do I highlight a column in vim?

Use ctrl+v to select a column of text consisting of the first character of the place you want your new column to go in front of. Use I to go into insert mode, and type one space. Press Esc, and you'll see you have inserted a column of single spaces. Now use ctrl+v again to highlight the column of spaces.

How do I highlight a specific line in vim?

With the default backslash leader key, pressing \l will highlight the line that currently contains the cursor. The mapping also sets mark l so you can type 'l to return to the highlighted line.

What is Colorcolumn in vim?

When set, colorcolumn will change the color of one or more columns in the current window. Using this feature is easy. Simply set the colorcolumn variable to the value of the column you want to highlight in the vim command line. Below is an example of setting the colorcolumn to 80. :set colorcolumn=80.

How do I delete a selection in vim?

By default, Ctrl L in Vim clears and redraws the screen.


1 Answers

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

like image 85
exhuma Avatar answered Sep 19 '22 10:09

exhuma