Vim Tips Wiki
No edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=235
 
|id=235
  +
|previous=234
|title=Toggle highlight word under cursor, to find cursor.
 
  +
|next=236
|created=April 11, 2002 7:27
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=HughSasse
 
|author=HughSasse
 
|version=6.0
 
|version=6.0
 
|rating=23/16
 
|rating=23/16
  +
|category1=
|text=
 
  +
|category2=
When the screen has scrolled such as during a search, it may be difficult to find the cursor. :help %&#35; 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:
 
 
 
 
function VIMRCWhere()
 
 
if !exists("s:highlightcursor")
 
 
match Todo /\k*\%&#35;\k*/
 
 
let s:highlightcursor=1
 
 
else
 
 
match None
 
 
unlet s:highlightcursor
 
 
endif
 
 
endfunction
 
 
map &lt;C-K&gt; :call VIMRCWhere()&lt;CR&gt;
 
 
 
 
This means that in "normal" mode ctrl-k will toggle the highlight. Todo is a hightlight group whch is particularly easy to see.
 
 
For further information see ":help s:", "[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:match}} :help match]", ":help exists()" and ":help funtion".
 
 
}}
 
}}
  +
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==
== Comments ==
 
Good trick. Another way to find the cursor is to hit zz, which moves the current line to the center of the screen.
+
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.
 
gsinclair--AT--soyabean.com.au
 
, April 12, 2002 20:44
 
----
 
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 chaged the function to make it highlight the entire line. To do this, change the appropriate line in the function to "match Todo /^.*\%&#35;.*$/".
 
   
  +
Put this script in your [[vimrc]]:
gsinclair--AT--soyabean.com.au
 
  +
<pre>
, April 12, 2002 23:43
 
  +
nnoremap <C-K> :call HighlightNearCursor()<CR>
----
 
  +
function HighlightNearCursor()
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 /^.*\%&#35;.*$/".
 
 
if !exists("s:highlightcursor")
 
match Todo /\k*\%#\k*/
 
let s:highlightcursor=1
  +
else
 
match None
 
unlet s:highlightcursor
  +
endif
 
endfunction
  +
</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}}
gsinclair--AT--soyabean.com.au
 
, April 12, 2002 23:43
 
----
 
jk
 
   
  +
The match uses the <code>Todo</code> highlight group.
Do this quickly, you'll see the cursor because it's moving.
 
   
  +
==See also==
'''Anonymous'''
 
  +
*[[VimTip769|Highlight current line]]
, April 13, 2002 2:15
 
  +
*[[VimTip1380|Highlight cursor line after cursor jump]]
----
 
  +
*[[VimTip182|Keep your cursor centered vertically on the screen]]
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.
 
   
  +
==References==
'''Anonymous'''
 
  +
*{{help|:match}}
, April 11, 2003 2:38
 
----
 
&lt;Space&gt; followed by &lt;Backspace&gt; moves the cursor in both Command and Insert modes, without altering the buffer.
 
   
 
==Comments==
patrick--AT--neuralyte.org
 
, October 2, 2003 20:14
 
----
 
<!-- parsed by vimtips.py in 0.472875 seconds-->
 

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