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
: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
'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>