Vim Tips Wiki
Tip 235 Printable Monobook Previous Next

created 2002 · complexity basic · author HughSasse · version 6.0

When scrolling or searching through a large file it is easy to lose sight of the cursor. A simple way to locate the cursor is to type zz (which scrolls the cursor line to the middle of the window), or to quickly type jk (which moves the cursor down then up; the movement shows where the cursor is). This tip shows other ways to find the cursor with highlighting.

Highlighting text near the cursor[]

The script below can highlight the word containing the cursor to make it easily visible: in normal mode, press Ctrl-K to toggle the highlight on or off.

Put this script in your vimrc:

nnoremap <C-K> :call HighlightNearCursor()<CR>
function HighlightNearCursor()
  if !exists("s:highlightcursor")
    match Todo /\k*\%#\k*/
    let s:highlightcursor=1
    match None
    unlet s:highlightcursor

The search pattern uses \%# to match the cursor position, including \k* (all consecutive keyword characters) before and after that match. :help /\%# :help /\k

The match uses the Todo highlight group.

See also[]