Vim Tips Wiki
(tweak, minor expand, merge in comment from 235)
m (Fix a grammar error)
(4 intermediate revisions by 3 users not shown)
Line 14: Line 14:
   
 
==Highlighting that moves with the cursor==
 
==Highlighting that moves with the cursor==
Simply putting <tt>:set cursorline</tt> in your [[vimrc]] will highlight the current line in every window and update the highlight as the cursor moves.
+
Simply putting <code>:set cursorline</code> 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 <tt>cursorline</tt> (to highlight the current line) and <tt>cursorcolumn</tt> (to highlight the current column):
+
The following example shows how to change the highlight colors and how to create a mapping to toggle <code>cursorline</code> (to highlight the current line) and <code>cursorcolumn</code> (to highlight the current column):
 
<pre>
 
<pre>
 
:hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
 
:hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
Line 23: Line 23:
 
</pre>
 
</pre>
   
With the default backslash leader key, typing <tt>\c</tt> will toggle highlighting on and off. That makes it easy to locate the cursor after scrolling in a large file.
+
With the default backslash leader key, typing <code>\c</code> 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:
 
If you only want the highlight applied in the current window, use an autocmd instead:
 
<pre>
 
<pre>
  +
augroup CursorLine
autocmd WinEnter * setlocal cursorline
 
  +
au!
autocmd WinLeave * setlocal nocursorline
 
 
au VimEnter,WinEnter,BufWinEnter * setlocal cursorline
 
au WinLeave * setlocal nocursorline
  +
augroup END
 
</pre>
 
</pre>
   
Line 35: Line 38:
 
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.
 
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 <tt>'cursorline'</tt> or <tt>'cursorcolumn'</tt> options can cause Vim to respond slowly, especially for large files or files with long lines.
+
Note that setting the <code>'cursorline'</code> or <code>'cursorcolumn'</code> options can cause Vim to respond slowly, especially for large files or files with long lines.
   
 
==Highlighting that stays after cursor moves==
 
==Highlighting that stays after cursor moves==
Line 43: Line 46:
 
</pre>
 
</pre>
   
With the default backslash leader key, pressing <tt>\l</tt> will highlight the line that currently contains the cursor. The mapping also sets mark <tt>l</tt> so you can type <tt>'l</tt> to return to the highlighted line. Enter <tt>:match</tt> to clear the highlighting when finished.
+
With the default backslash leader key, pressing <code>\l</code> will highlight the line that currently contains the cursor. The mapping also sets mark <code>l</code> so you can type <code>'l</code> to return to the highlighted line. Enter <code>:match</code> 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:
 
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:
Line 60: 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==
  +
  +
It is possible to highlight the entire line permanently (mapped to key \l):
  +
<pre>:nnoremap <silent> <Leader>l :exe "let m = matchadd('WildMenu','\\%" . line('.') . "l')"<CR></pre>
  +
Or one could only highlight the word underneath the cursor (mapped to key \w):
  +
<pre>:nnoremap <silent> <Leader>w :exe "let m=matchadd('WildMenu','\\<\\w*\\%" . line(".") . "l\\%" . col(".") . "c\\w*\\>')"<CR></pre>
  +
To highlight the words contained in the virtual column (mapped to \c):
  +
<pre>:nnoremap <silent> <Leader>c :exe "let m=matchadd('WildMenu','\\<\\w*\\%" . virtcol(".") . "v\\w*\\>')"<CR></pre>
  +
And finally, one can clear the permanent highlights (mapped to \Enter):
  +
<pre>:nnoremap <silent> <Leader><CR> :call clearmatches()<CR></pre>
  +
In the examples, I used a different highlighting group as in the main article.

Revision as of 23:14, 12 May 2013

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

It is possible to highlight the entire line permanently (mapped to key \l):

:nnoremap <silent> <Leader>l :exe "let m = matchadd('WildMenu','\\%" . line('.') . "l')"<CR>

Or one could only highlight the word underneath the cursor (mapped to key \w):

:nnoremap <silent> <Leader>w :exe "let m=matchadd('WildMenu','\\<\\w*\\%" . line(".") . "l\\%" . col(".") . "c\\w*\\>')"<CR>

To highlight the words contained in the virtual column (mapped to \c):

:nnoremap <silent> <Leader>c :exe "let m=matchadd('WildMenu','\\<\\w*\\%" . virtcol(".") . "v\\w*\\>')"<CR>

And finally, one can clear the permanent highlights (mapped to \Enter):

:nnoremap <silent> <Leader><CR> :call clearmatches()<CR>

In the examples, I used a different highlighting group as in the main article.