Tip: #263 - Color active line
Created: June 18, 2002 7:05 Complexity: basic Author: Armin Rehm (rehm.armin--AT--schwenk.de) Version: 6.0 Karma: 142/50 Imported from: Tip#263
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 \%
Comments
It should be noted that the CursorHold event is not updated within insert mode. :(
digitect (at) mindspring � com
, June 18, 2002 7:38
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.
richardsugg--AT--yahoo.com , June 19, 2002 5:10
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 <jonestor1976--AT--yahoo.com> " 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
jonestor1976--AT--yahoo.com , June 21, 2002 10:05
I've just added the script above to the archive as [/scripts/script.php?script_id=319 vimscript #319]
Have fun.
jonestor1976--AT--yahoo.com , June 21, 2002 10:18
The tip seems to be working better than the script...
Anonymous , July 4, 2002 1:22
The tip does'nt work for me. I have VIM 6.1, but I'm not using gvim - is this the problem?
TIA,
Flavio.
flavio [at] polettix.it , November 4, 2002 3:55
Any idea how one could get the entire ROW to be hilighted?
dsummersl--AT--yahoo.com , April 30, 2003 9:43
modified possibility 2; should now work in xterm:
- highlight CurrentLine guibg=LightCyan guifg=black ctermbg=LightCyan ctermfg=black
- au! Cursorhold * :exe 'match CurrentLine /\%' . line(".") . 'l.*'
set ut=100
doc-brown--AT--gmx.net
, April 27, 2005 13:47
VimTip769 tips769 is the best choice
stevenpan--AT--gmail.com , April 26, 2006 2:17