Highlight the current line in the active window
From Vim Tips Wiki
[edit] Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
Tip 1293 Previous Next Created: July 31, 2006 Complexity: basic Author: Allen Wong Version: 5.7
This tip provides current line highlighting that simulates most word processors which highlight only the current line in the active window. It also works in insert mode.
"Highlight current line
highlight CurrentLine guibg=blue guifg=white ctermbg=blue ctermfg=white
:au! Cursorhold * :exe 'match CurrentLine /\%' . line(".") . 'l.*'
:set ut=0
:au! WinLeave * exe 'match none'
inoremap <silent> <Up> <Up><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>i
inoremap <silent> <Down> <Down><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>i
inoremap <silent> <pageup> <pageup><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>i
inoremap <silent> <pagedown> <pagedown><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>i
[edit] See also
- Map to highlight this line to highlight a fixed line
[edit] Comments
As of Vim 7, this seems to do the job:
autocmd WinLeave * setlocal nocursorcolumn nocursorline autocmd WinEnter * setlocal cursorline cursorcolumn
If you add BufEnter/BufLeave it works with splits also:
autocmd BufLeave * setlocal nocursorcolumn nocursorline autocmd BufEnter * setlocal cursorline cursorcolumn
I like to see the cursor in all my buffers all the time and highlight the current line/column very slightly, this is what works for my Vim:
autocmd BufEnter * setlocal cursorline cursorcolumn hi cursorcolumn ctermbg=247 guibg=grey70 hi cursorline ctermbg=247 guibg=grey70
Same thing, but works in normal mode only. This is better for me since I have arrow keys mapped to other things.
"Hightlight current line
highlight CurrentLine guibg=darkblue guifg=white ctermbg=darkblue ctermfg=white
au! Cursorhold * :exe 'match CurrentLine /\%' . line(".") . 'l.*'
set ut=0
au! WinLeave * exe 'match none'
nnoremap k <Up><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>
nnoremap j <Down><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>
nnoremap <silent> <pageup> <pageup><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>
nnoremap <silent> <pagedown> <pagedown><Esc>:exe 'match CurrentLine /\%' . line(".") . 'l.*'<CR>
Unfortunately the 'cursorline' option heavily consumes the CPU. The more viewspace you have, the slower the highlighting is. It seems that the highlighting is done on the whole viewspace instead of just the active line.
