Vim Tips Wiki
Advertisement

Obsolete tip

This tip has been merged into another tip.
See VimTip769 for the current tip.

Please do not edit this tip, and do not edit the discussion page.
If anything needs to be improved, please fix VimTip769.


Tip 263 Printable Monobook Previous Next

created June 18, 2002 · complexity basic · author Armin Rehm · version 6.0


This tip is deprecated for the following reasons:

In Vim 7 you can put ":set cursorline" in your vimrc.

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

Comments

It should be noted that the CursorHold event is not updated within insert mode.


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
" 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

I've just added the script above to the archive as script#319.


VimTip769 is the best choice


Advertisement