Vim logo vim online Vim Book Ad

basic Tip #263: color active line

 tip karma   Rating 15/11, Viewed by 1400 

created:   June 18, 2002 7:05      complexity:   basic
author:   Armin Rehm ([email protected])      as of Vim:   6.0

This tip shows how to color the active line, the line in which the cursor is, for better reading.
You should try possibility 2 before 1, IMHO it is mostly usable.

possibility 1:
     :au! CursorHold * let @/ = '\%' . line('.') . 'l.*'
     :set ut=500

explanation:
     After 500 ms of waiting for you to hit a key, vim sets the search register to a pattern that matches the current line.

problem:
     Register / holds the search pattern, so you cannot have color the active line and search.
     Therefore another solution:

possibility 2:
     :highlight CurrentLine guibg=darkgrey guifg=white     (or whatever colors you want)
     :au! Cursorhold * exe 'match CurrentLine /\%' . line('.') . 'l.*/'
     :set ut=100

explanation:
     This solution uses 'match' to highlight a string, it does not interface with the current search pattern.

addition:
     Turning the highlighning off:
          :au! Cursorhold
          :match none
     The order of these commands are important. If :match none is executed first, the autocommand would
     almost immediately execute another match command.

references to vim help:
     :help Cursorhold
     :help 'ut'
     :help /\%l
     :help "/
     :help \%

 rate this tip  Life Changing Helpful Unfulfilling 

<<Bored of ur arrow shapped mouseptr? | F5 Compile and Run, F8 Compile (ala Visual Studio) >>

Additional Notes

digitect (at) mindspring • com, June 18, 2002 7:38
It should be noted that the CursorHold event is not updated within insert mode. :(
[email protected], June 19, 2002 5:10
now this is cool.  I really appreciated this tip.  Can anyone tell me how to make this work in insert mode as well?  Then I'll be giddy with the joy that only vim can give me.
[email protected], June 21, 2002 10:05
I wrote a script to do this that will work in insert mode.  By the way, it has problems if there is a delimiter on the line that you are trying to highlight.

Press F2 to highlight a line.
Press F3 to unhighlight the line

The current version is below.

" Vim plugin file
" Description:
" Maintainer:  Torrin <[email protected]>
" Last Change: 2002 JUN 21
" Version:     2

" If we have already loaded this file, don't load it again.
if exists("loaded_highlightline")
   finish
endif
let loaded_highlightline=1

" Save compatable options
let s:save_cpo = &cpo;
set cpo&vim;

let linehighlighted=0

map <F2> :call <SID>HighlightLine()<CR>
map <F3> :call <SID>UnHighlightLine()<CR>
imap <F2> <C-O>:call <SID>HighlightLine()<CR>
imap <F3> <C-O>:call <SID>UnHighlightLine()<CR>

function! s:HighlightLine()
"   execute "syntax keyword OneLine \"" . getline(".") . "\""
   if g:linehighlighted == 1
       call <SID>UnHighlightLine()
   endif
   execute "syntax match OneLine +" . getline(".") . "+ oneline"
   execute "highlight default link OneLine Visual"
   let g:linehighlighted = 1
endfunction

function! s:UnHighlightLine()
   if g:linehighlighted == 1
      execute "syntax clear OneLine"
      let g:linehighlighted = 0
   endif
endfunction

let &cpo; = s:save_cpo
unlet s:save_cpo
[email protected], June 21, 2002 10:18
I've just added the script above to the archive as vimscript #319

Have fun.
Anonymous, July 4, 2002 1:22
The tip seems to be working better than the script...
flavio [at] polettix.it, November 4, 2002 3:55
The tip does'nt work for me. I have VIM 6.1, but I'm not using gvim - is this the problem?

TIA,

   Flavio.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo