This tip is deprecated for the following reasons:
In Vim 7 you can put ":set cursorline" in your vimrc.
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
created August 14, 2004 · complexity intermediate · author Aditya Mahajan · version 6.0
There is another tip VimTip421 and a script script#555 on how to highlight the current line. But I think that this is much simpler than those.
\%# matches to the current cursor position. Using this one can highlight the whole line
syntax match CurrentLine /.*\%#.*/ hi link CurrentLine Visual
or
hi CurrentLine guifg=white guibg=lightblue
To switch off the highlightline, simply do
syntax match CurrentLine "NONE"
Comments
I had intended this tip to highlight the current line even when you move the cursor. In my system, this is working. That is, when I move the cursor, the line I am in is getting highlighted. But on reading the documentation carefully (help cursor-position), I realized that this should not work. I do not know why it is working here (for both console vim and GUI), perhaps some setting that I have set, or the screen refreshing routine has changed in vim 6.3
Try refreshing the screen with CTRL-L , something like
nmap j :normal! j<cr><C-L>
and so on for jkl <Up> <Down> <left> <right>. But this can cause flickering of the screen, if you are using vim over a telnet or ssh session.
I decided that this was a useful idea so I added the following function and map to my VIMRC file. It uses the CursorHold autocommand to recreate the syntax match. This allows the command to work even when you change to a new file or update the syntax.
function! HighlightCurrentLine() if ! exists("g:CurrentLineUpdateTime") syntax match CurrentLine /.*\%#.*/ highlight link CurrentLine Visual let g:CurrentLineUpdateTime = &updatetime set updatetime=50 augroup CurrentLine au! au CursorHold * nested syntax clear CurrentLine | syntax match CurrentLine /.*\%#.*/ containedin=ALL augroup END else highlight CurrentLine NONE windo syntax clear CurrentLine augroup CurrentLine au! augroup END exe 'set updatetime=' . expand(g:CurrentLineUpdateTime) unlet g:CurrentLineUpdateTime endif endfunction map <silent> <Leader>hl :call HighlightCurrentLine()<CR>
Using vim7 you can use this code to achieve the same:
augroup vim7highlightCurrentLineInActiveWindow au WinEnter * if g:HighlightCurrentLine | setlocal cul | endif au WinLeave * if g:HighlightCurrentLine | setlocal nocul | endif augroup end function! s:ToggleHighlightCurrentLine() if !exists('g:HighlightCurrentLine') || g:HighlightCurrentLine==0 setlocal cul let g:HighlightCurrentLine=1 else setlocal nocul let g:HighlightCurrentLine=0 endif endfunction command! ToggleHighlightCurrentLine :call <sid>ToggleHighlightCurrentLine()
VimTip1293 is another simple script works for both insert and normal mode. I like it better because it highlights only the active window!
All the solutions I have tried including the scripts get slower and slower as time goes by. This seems to be because the highlighting doesnt clean up properly. This is how i "fixed" it.
let g:highlightnu = 0 " Turn off line highlighting initially " Toggles current line highlighting map <silent> <A-g> :call MapKeys()<CR> " Highlight Current Line fun! Highlight() syntax match CurrentLine /.*\%#.*/ highlight link CurrentLine Visual endfun " Function to move down while highlighting fun! MvDwn() call Clean() call cursor((line(".")+1),col(".")) call Highlight() endfun " Function to move down while highlighting fun! MvUp() call Clean() call cursor((line(".")-1),col(".")) call Highlight() endfun " Function to clean the highlighting (This is why this script doesn't slow down) fun! Clean() highlight CurrentLine NONE windo syntax clear CurrentLine endfun " Function to map keys and toggle the highlighting fun! MapKeys() if g:highlightnu == 0 map <silent> j :call MvDwn()<CR> map <silent> k :call MvUp()<CR> map <silent> <Up> :call MvUp()<CR> map <silent> <Down> :call MvDwn()<CR> let g:highlightnu = 1 elseif g:highlightnu == 1 unmap j unmap k unmap <Up> unmap <Down> call Clean() let g:highlightnu = 0 endif endfun