Vim Tips Wiki
(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=235
 
|id=235
 
|previous=234
 
|previous=234
 
|next=236
 
|next=236
|created=April 11, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=HughSasse
 
|author=HughSasse
Line 12: 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 <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.
When the screen has scrolled such as during a search, it may be difficult to find the cursor. {{help|%#}} explains the pattern one can use to highlight the word around the cursor, which gives a bigger target to look for on the screen. I have this in my [[vimrc]]:
 
   
  +
==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]]:
 
<pre>
 
<pre>
 
nnoremap <C-K> :call HighlightNearCursor()<CR>
function VIMRCWhere()
 
  +
function HighlightNearCursor()
 
if !exists("s:highlightcursor")
 
if !exists("s:highlightcursor")
 
match Todo /\k*\%#\k*/
 
match Todo /\k*\%#\k*/
Line 24: Line 28:
 
endif
 
endif
 
endfunction
 
endfunction
map <C-K> :call VIMRCWhere()<CR>
 
 
</pre>
 
</pre>
   
  +
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}}
This means that in normal mode, ctrl-k will toggle the highlight. Todo is a highlight group which is particularly easy to see.
 
  +
  +
The match uses the <code>Todo</code> highlight group.
  +
  +
==See also==
  +
*[[VimTip769|Highlight current line]]
  +
*[[VimTip1380|Highlight cursor line after cursor jump]]
  +
*[[VimTip182|Keep your cursor centered vertically on the screen]]
   
 
==References==
 
==References==
 
*{{help|:match}}
 
*{{help|:match}}
*{{help|exists()}}
 
   
 
==Comments==
 
==Comments==
Good trick. Another way to find the cursor is to hit zz, which moves the current line to the center of the screen.
 
 
----
 
I found that often the highlighting didn't help, because the cursor was either not on a word or on a single-letter word. Therefore, I changed the function to make it highlight the entire line. To do this, change the appropriate line in the function to "match Todo /^.*\%#.*$/".
 
 
----
 
Do this quickly, you'll see the cursor because it's moving.
 
 
----
 
As well as hitting zz, you can ":set scrolloff=999" (or so=999) to leave it permanently in the centre.
 
 
Takes a bit of getting used to, but you never lose the cursor and you don't have to spend as much time scrolling because you're always editing in the middlle of the action.
 
 
----
 
<Space> followed by <Backspace> moves the cursor in both Command and Insert modes, without altering the buffer.
 
 
----
 

Latest revision as of 05:20, 13 July 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 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
  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 also[]

References[]

Comments[]