Wikia

Vim Tips Wiki

Changes: Highlight current line

Edit

Back to page

(Highlighting that moves with the cursor: Improve autocmds to also handle first window on startup and buffer changes.)
(Related scripts: Add reference to CursorLineCurrentWindow plugin.)
Line 63: Line 63:
 
*[[VimTip235|Highlight current word to find cursor]]
 
*[[VimTip235|Highlight current word to find cursor]]
   
==Related scripts==
+
==Related plugins==
 
*{{script|id=555}}
 
*{{script|id=555}}
 
*{{script|id=319}}
 
*{{script|id=319}}
  +
*{{script|id=4178|text=CursorLineCurrentWindow}} highlights the current line only the current window, and allows for exceptions like disabling the cursorline for a particular window or making it permanent for (another) window.
   
 
==Comments==
 
==Comments==

Revision as of 20:35, August 18, 2012

Tip 769 Printable Monobook Previous Next

created 2004 · complexity basic · version 7.0


It is possible to highlight the line containing the cursor. In addition, the column containing the cursor can be highlighted. Highlighting the line makes it easy to locate the cursor when scrolling through a large file, and highlighting the column can help to check horizontal alignment of text in different lines.

Highlighting that moves with the cursor

Simply putting :set cursorline in your vimrc will highlight the current line in every window and update the highlight as the cursor moves.

The following example shows how to change the highlight colors and how to create a mapping to toggle cursorline (to highlight the current line) and cursorcolumn (to highlight the current column):

:hi CursorLine   cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
:hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
:nnoremap <Leader>c :set cursorline! cursorcolumn!<CR>

With the default backslash leader key, typing \c will toggle highlighting on and off. That makes it easy to locate the cursor after scrolling in a large file.

If you only want the highlight applied in the current window, use an autocmd instead:

augroup CursorLine
  au!
  au VimEnter,WinEnter,BufWinEnter * setlocal cursorline
  au WinLeave * setlocal nocursorline
augroup END

To achieve this result on older Vim versions, you could combine the idea in the first method with a CursorHold or a CursorMoved autocmd.

If you only want highlighting in insert mode (but don't mind that it appears in all windows) use InsertEnter and InsertLeave instead of WinEnter and WinLeave above.

Note that setting the 'cursorline' or 'cursorcolumn' options can cause Vim to respond slowly, especially for large files or files with long lines.

Highlighting that stays after cursor moves

To highlight the current line, and have the highlighting stay where it is when the cursor is moved, use this mapping:

:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR>

With the default backslash leader key, pressing \l will highlight the line that currently contains the cursor. The mapping also sets mark l so you can type 'l to return to the highlighted line. Enter :match to clear the highlighting when finished.

To highlight the current virtual column (column after tabs are expanded), and have the highlighting stay where it is when the cursor is moved, use this mapping:

:nnoremap <silent> <Leader>c :execute 'match Search /\%'.virtcol('.').'v/'<CR>

References

See also

Related plugins

  • script#555
  • script#319
  • CursorLineCurrentWindow highlights the current line only the current window, and allows for exceptions like disabling the cursorline for a particular window or making it permanent for (another) window.

Comments

Around Wikia's network

Random Wiki