Wikia

Vim Tips Wiki

Changes: Highlight current word to find cursor

Edit

Back to page

(tweak and merge comments; move one comment to 769)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
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 <tt>zz</tt> (which scrolls the cursor line to the middle of the window), or to quickly type <tt>jk</tt> (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.
+
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 <code>zz</code> (which scrolls the cursor line to the middle of the window), or to quickly type <code>jk</code> (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==
 
==Highlighting text near the cursor==
Line 30: Line 30:
 
</pre>
 
</pre>
   
The search pattern uses <tt>\%#</tt> to match the cursor position, including <tt>\k*</tt> (all consecutive keyword characters) before and after that match. {{help|/\%#}} {{help|/\k}}
+
The search pattern uses <code>\%#</code> to match the cursor position, including <code>\k*</code> (all consecutive keyword characters) before and after that match. {{help|/\%#}} {{help|/\k}}
   
The match uses the <tt>Todo</tt> highlight group.
+
The match uses the <code>Todo</code> highlight group.
   
 
==See also==
 
==See also==

Latest revision as of 05:20, July 13, 2012

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 cursorEdit

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
  else
    match None
    unlet s:highlightcursor
  endif
endfunction

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 alsoEdit

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki