Vim: Highlight current line and column
Summary
Learn how to highlight the current line and column in Vim permanently or temporarily, how to customize colors, and how to prevent color scheme overwrites.
Introduction #
In Vim, highlighting the current line and column helps you maintain visual focus on your current cursor position.
You can configure this behavior permanently in your configuration file (.vimrc) or enable it temporarily during an active session. Vim supports both text-based (terminal) and graphical user interface (GUI) color settings for highlighting.
Important: Setting the cursorline or cursorcolumn options can cause Vim to respond slowly, especially for large files or files with long lines. If you experience lag, consider disabling one or both of these options when working with such files.
Set highlights permanently #
To enable line and column highlighting each time you start Vim, edit your configuration file ~/.vimrc. This file defines persistent options and settings for Vim sessions.
Enable cursorline and cursorcolumn #
Add the following lines to your .vimrc file:
set cursorline
set cursorcolumn
These options enable visual highlighting of the current line and column in every buffer.
Define highlight colors #
You can specify how these highlights look.
- The
ctermoption controls the color for terminal Vim, and - The
guioption applies to graphical Vim environments such as GVim.
To define robust highlight styles, include cterm=NONE gui=NONE to prevent unwanted styling such as bold or underline:
highlight CursorLine cterm=NONE ctermbg=236 gui=NONE guibg=#303030
highlight CursorColumn cterm=NONE ctermbg=238 gui=NONE guibg=#404040
A list of safe, compatible colors for both terminal and GUI environments can be found in the 256 Colors Cheat Sheet.
Overwriting color schemes #
If you use Vim color schemes, they may override your custom highlight settings. To ensure your custom colors persist, use an auto command (autocmd) to reapply the highlight after each color scheme is loaded.
Add this to your .vimrc after any colorscheme command:
autocmd ColorScheme * highlight CursorLine cterm=NONE ctermbg=236 gui=NONE guibg=#303030
autocmd ColorScheme * highlight CursorColumn cterm=NONE ctermbg=238 gui=NONE guibg=#404040
This ensures your highlight settings are always applied, regardless of which color scheme you use.
Set highlights in active session #
You can enable line and column highlighting temporarily while Vim is running. This is useful for testing different highlight configurations without modifying your .vimrc file.
Enable line and column highlighting #
Use the following commands in command mode:
:set cursorline
:set cursorcolumn
These commands activate the highlighting for the current session only. When you exit Vim, the settings are lost unless you save them in your .vimrc file.
Customize highlight colors #
You can also change the highlight colors dynamically in command mode. For example:
:highlight CursorLine cterm=NONE ctermbg=236 gui=NONE guibg=#303030
:highlight CursorColumn cterm=NONE ctermbg=238 gui=NONE guibg=#404040
These commands immediately apply the specified colors for the current session.
Difference between :set and :highlight #
Enable
cursorlineandcursorcolumn:
The command:set cursorlineor:set cursorcolumn
enables the highlighting feature, but it does not define what the highlight looks like.
Define the appearance:
The command:highlight CursorLine ...or:highlight CursorColumn ...
defines the appearance (color, background, style) of that highlight.
Both must be combined to achieve visible, customized highlighting.
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
How do I enable line and column highlighting temporarily in Vim?
You can use the commands :set cursorline and :set cursorcolumn in command mode to enable highlighting for the current session.
Where should I place highlight customization in the .vimrc file?
Always place custom highlight settings after the colorscheme command. Otherwise, the color scheme will overwrite them.
Can I use both cursorline and cursorcolumn at the same time?
Yes, you can enable both simultaneously. Vim will highlight both the line and column of the cursor position.
How do I choose safe colors for terminal highlighting?
You can refer to the 256-color cheat sheet at 256 Colors cheat sheet) to find valid color codes.
Does line and column highlighting affect Vim performance?
Yes, enabling cursorline or cursorcolumn may slow Vim down when editing large files or files with very long lines.
Do I need to restart Vim after editing the .vimrc file?
No, you can apply the changes immediately using the :source ~/.vimrc command.
Further readings #
Sources and recommended, further resources on the topic:
- Ditig: 256 Terminal Colors Cheat Sheet
- Vim Help: 'cursorline' and 'cursorcolumn' options
- Vim Help: Auto Commands (
autocmd)
License
Vim: Highlight current line and column by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/highlight-current-line-and-column-in-vim">Vim: Highlight current line and column</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.