add a color line behind line number

222 views
Skip to first unread message

stardiviner

unread,
Jul 12, 2011, 9:38:33 AM7/12/11
to vim-help
how to add a colored line behind line number.
here is the example:
http://blog.nguyenvq.com/2011/06/29/some-custom-emacs-keybindings-for-latex-to-assist-in-the-writing-process/


--
Blog: http://stardiviner.tumblr.com/
Homepage: http://stardiviner.dyndns-blog.com
Love Linux -> love code -> like this.

signature.asc

Ben Fritz

unread,
Jul 12, 2011, 5:33:00 PM7/12/11
to vim_use


On Jul 12, 8:38 am, stardiviner <numbch...@gmail.com> wrote:
> how to add a colored line behind line number.
> here is the example:http://blog.nguyenvq.com/2011/06/29/some-custom-emacs-keybindings-for...
>

Are you asking for every other line to be highlighted in a different
color as shown in the screen shot?

You can't do it exactly like the screenshot, and it will slow down
performance greatly, but you might be able to use a bunch of
matchadd() calls, to set up matches for all text on each line.

The line number column will not be highlighted, and only text within
the line will be highlighted.

There is no way to highlight entire lines, including empty space and
line numbers, alternating between two different backgrounds. At least,
not without modifying Vim's C code and recompiling.

Benjamin R. Haskell

unread,
Jul 12, 2011, 7:19:37 PM7/12/11
to vim_use
On Tue, 12 Jul 2011, Ben Fritz wrote:

> On Jul 12, 8:38 am, stardiviner wrote:
>> how to add a colored line behind line number.
>> here is the example:http://blog.nguyenvq.com/2011/06/29/some-custom-emacs-keybindings-for...
>>
>
> Are you asking for every other line to be highlighted in a different
> color as shown in the screen shot?

I assumed the request was for the red, vertical bar after (= "behind")
the line number.

I couldn't see a straightforward way to do it. My first attempt was to
subvert the 'signs' columns, but I couldn't figure out a nice way to
activate the column without actually adding signs. And after doing so I
discovered that it appears to the left of the numbers anyway.

--
Best,
Ben H

Bee

unread,
Jul 12, 2011, 8:30:14 PM7/12/11
to vim_use
Could it be something as simple as:

highlight LineNr NONE ctermfg=DarkGreen guifg=Cyan

I use green for terminal vim and cyan for gui vim.

-Bill

Christian Brabandt

unread,
Jul 13, 2011, 2:07:25 AM7/13/11
to vim...@googlegroups.com

You could possibly hack something like this:
1) Add a space at the beginning:
:%s/^/ /
2) Match the first column
call matchadd('Conceal', '^ ')
3) change syntax highlighting for the conceal highlighting group
hi Conceal ctermbg=Red ctermfg=Red

You need to adjust the color to your taste and depending on whether
you use terminal vim or gvim, you need to use cterm(bg,fg) (for the former)
or gui(bg,fg) for the latter.

regards,
Christian

Christian Brabandt

unread,
Jul 13, 2011, 2:11:58 AM7/13/11
to vim...@googlegroups.com
On Tue, July 12, 2011 11:33 pm, Ben Fritz wrote:
> Are you asking for every other line to be highlighted in a different
> color as shown in the screen shot?
>
> You can't do it exactly like the screenshot, and it will slow down
> performance greatly, but you might be able to use a bunch of
> matchadd() calls, to set up matches for all text on each line.

I tried once using several
:syn match Evenl /.*/ nextgroup=Oddl
:syn match Oddl /.*/ nextgroup=Evenl
:hi Evenl ctermbg=Red ctermfg=Black
:hi Oddl ctermbg=Yellow ctermfg=Black

Unfortunately, this did not work. I am not sure why. My understanding
of the syntax highlighting was, that the nextgroup parameter should
force matching the next group. But somehow it ends always matching the
same group. Not sure if this is a bug or my understanding is wrong.

> There is no way to highlight entire lines, including empty space and
> line numbers, alternating between two different backgrounds. At least,
> not without modifying Vim's C code and recompiling.

> There is no way to highlight entire lines, including empty space and
> line numbers, alternating between two different backgrounds. At least,
> not without modifying Vim's C code and recompiling.

That is not entirely true. If you set up signs, you can highlight
entire lines. I have made a simply plugin that makes that possible:
http://www.vim.org/scripts/script.php?script_id=2998

regards,
Christian

Ben Fritz

unread,
Jul 13, 2011, 1:13:56 PM7/13/11
to vim_use


On Jul 13, 1:11 am, "Christian Brabandt" <cbli...@256bit.org> wrote:
> On Tue, July 12, 2011 11:33 pm, Ben Fritz wrote:
> > Are you asking for every other line to be highlighted in a different
> > color as shown in the screen shot?
>
> > You can't do it exactly like the screenshot, and it will slow down
> > performance greatly, but you might be able to use a bunch of
> > matchadd() calls, to set up matches for all text on each line.
>
> I tried once using several
> :syn match Evenl /.*/ nextgroup=Oddl
> :syn match Oddl  /.*/ nextgroup=Evenl
> :hi Evenl ctermbg=Red ctermfg=Black
> :hi Oddl  ctermbg=Yellow ctermfg=Black
>
> Unfortunately, this did not work. I am not sure why. My understanding
> of the syntax highlighting was, that the nextgroup parameter should
> force matching the next group. But somehow it ends always matching the
> same group. Not sure if this is a bug or my understanding is wrong.
>

I think it is because you aren't including the newline in the match.
So, Oddl matches first, tries matching Evenl on the line ending (which
fails because line endings aren't matched with '.'), and therefore
Oddl matches again on the next line. Possibly just using ^.*\n as the
pattern would fix it, but this will probably not work well together
with existing syntax highlights. That's why I suggested matchadd()

> > There is no way to highlight entire lines, including empty space and
> > line numbers, alternating between two different backgrounds. At least,
> > not without modifying Vim's C code and recompiling.
> > There is no way to highlight entire lines, including empty space and
> > line numbers, alternating between two different backgrounds. At least,
> > not without modifying Vim's C code and recompiling.
>
> That is not entirely true. If you set up signs, you can highlight
> entire lines. I have made a simply plugin that makes that possible:http://www.vim.org/scripts/script.php?script_id=2998
>

WOW! I didn't even know about that. I stand corrected.

However, you still don't get the alternating highlight of the line
number column, the sign column will then always be displayed, and the
sign column is only 2 characters wide always so you cannot just use it
as a replacement for the line number column. Also, 'wrap' and
'showbreak' interact in unexpected (but not incorrect) ways.

So, something like this can alternate line colors, coloring all even
lines in blue and falling back to Normal highlight for odd lines (very
gaudy, probably not recommended with colors as-is) in an existing
buffer:

:sign define EvenL linehl=EvenLbg
:hi EvenLbg guibg=blue
:g#^#if line('.')%2==0 | exec 'sign place '.line('.').'
line='.line('.').' name=EvenL buffer='.bufnr("%") | endif
:nohls

To keep the highlight up-to-date, you'd need to re-apply them after
every change. So far I've come up with this:

sign define EvenL linehl=EvenLbg
hi EvenLbg guibg=blue
augroup ALT_LINES
au!
autocmd BufWinEnter,CursorMoved,CursorMovedI,CursorHold,CursorHoldI
*
\ if !exists('b:my_changedtick') || b:my_changedtick !=
b:changedtick |
\ let b:my_changedtick = b:changedtick |
\ let pos_sav = getpos('.') |
\ for id in range(2,line('$'),2) | exec 'sign unplace '.id.'
buffer='.bufnr('%') | endfor |
\ keepjumps exec 'g#^#if line(".")%2==0 | exec "sign place
".line(".")." line=".line(".")." name=EvenL buffer=".bufnr("%") |
endif' |
\ nohls |
\ call setpos('.',pos_sav) |
\ endif
augroup END

The for loop in there is an attempt to allow multiple buffers
simultaneously. It's faster to do :sign unplace * but that will not
work for multiple buffers.

It's also really slow for big files--unbearably slow for really big
ones, since it updates on every change--and the screen flashes on
every change for small files. This could be improved by removing the
CursorMoved autocmds at the sacrifice of not having completely correct
highlight sometimes, but could still be very annoying.

If I ever start using something like this, I'd probably tweak it to
only fire on buffers with empty buftype, remove the CursorMoved and
CursorMovedI events, and repalce them with InsertLeave.

Christian Brabandt

unread,
Jul 14, 2011, 2:38:53 AM7/14/11
to vim...@googlegroups.com
On Wed, July 13, 2011 7:13 pm, Ben Fritz wrote:
> On Jul 13, 1:11�am, "Christian Brabandt" <cbli...@256bit.org> wrote:
>> On Tue, July 12, 2011 11:33 pm, Ben Fritz wrote:
>> > Are you asking for every other line to be highlighted in a different
>> > color as shown in the screen shot?
>>
>> > You can't do it exactly like the screenshot, and it will slow down
>> > performance greatly, but you might be able to use a bunch of
>> > matchadd() calls, to set up matches for all text on each line.
>>
>> I tried once using several
>> :syn match Evenl /.*/ nextgroup=Oddl
>> :syn match Oddl �/.*/ nextgroup=Evenl
>> :hi Evenl ctermbg=Red ctermfg=Black
>> :hi Oddl �ctermbg=Yellow ctermfg=Black
>>
>> Unfortunately, this did not work. I am not sure why. My understanding
>> of the syntax highlighting was, that the nextgroup parameter should
>> force matching the next group. But somehow it ends always matching the
>> same group. Not sure if this is a bug or my understanding is wrong.
>>
>
> I think it is because you aren't including the newline in the match.
> So, Oddl matches first, tries matching Evenl on the line ending (which
> fails because line endings aren't matched with '.'), and therefore
> Oddl matches again on the next line. Possibly just using ^.*\n as the
> pattern would fix it, but this will probably not work well together
> with existing syntax highlights. That's why I suggested matchadd()

Yes, this works. Thanks for pointing me to the obvious ;)

regards,
Christian

Ben Fritz

unread,
Jul 14, 2011, 11:10:57 AM7/14/11
to vim_use
But, how well does it work with existing syntax? Is it easy to apply
to C highlighting, for example? I imagine it would be very easy to get
into a tangled mess of contains and containedin tweaks, so that it
would be difficult to apply generically to all syntaxes. But I haven't
ever tried, so maybe I'm just being paranoid.

Christian Brabandt

unread,
Jul 14, 2011, 5:40:54 PM7/14/11
to vim_use
Hi Ben!

It works, but overrides your existing syntax rules (I guess because of
:h syn-priority). And it get's empty lines wrong.

Mit freundlichen Gr��en
Christian

Reply all
Reply to author
Forward
0 new messages