History
Article Edit this page Discussion

Highlight the current line in the active window

From Vim Tips Wiki

Jump to: navigation, search

[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

[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.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Highlight the current line in the active window"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation