How to highlight the current line in Vim
Summary
To highlight the current line in Vim, enable the 'cursorline' setting and customize its appearance through your color scheme or highlight group configuration.
Enabling cursorline
in Vim #
When working in Vim, highlighting the current line can help you keep track of your position and reduce visual strain.
To highlight the current line, Vim provides a built-in option called cursorline
. When enabled, this option causes the line under the cursor to be highlighted using the CursorLine
highlight group.
To enable it temporarily, use the following command in normal mode:
:set cursorline
To make this setting permanent, add the following line to your Vim configuration file, usually located at ~/.vimrc
:
set cursorline
Customizing the highlight style #
The default highlight color of the current line may not always suit your color scheme or preferences. You can customize this by modifying the CursorLine
highlight group.
To change the background color, use:
:highlight CursorLine cterm=NONE ctermbg=darkgray guibg=#2e2e2e
cterm
affects terminal Vim using 8 or 256 colors.ctermbg
sets the background color in terminal Vim.guibg
sets the background color in graphical Vim (GVim).
You can place this command in your .vimrc
to apply it automatically when Vim starts.
See our 256 colors cheat sheet for all available color options.
Controlling when the current line is highlighted #
You might prefer to highlight the current line only in normal mode. To do that, use autocommands in your .vimrc
:
autocmd InsertEnter * set nocursorline
autocmd InsertLeave * set cursorline
This configuration disables the highlight while you are inserting text and re-enables it when you return to normal mode.
Using cursorline
with different color schemes #
Some color schemes automatically define the CursorLine
style. If you apply a color scheme after setting your highlight preferences, it may override them. In that case, make sure to set your custom highlight
commands after the color scheme is loaded. For example:
colorscheme desert
highlight CursorLine cterm=NONE ctermbg=darkgray guibg=#2e2e2e
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
How do I make the current line highlight permanent in Vim?
Add set cursorline
to your .vimrc
file to enable it every time you start Vim.
Can I change the highlight color of the current line?
Yes. You can use highlight CursorLine
followed by color attributes to set your preferred appearance.
Does the 'cursorline' setting work in all modes?
By default, it works in normal mode. To disable it in insert mode, use an autocmd
to turn it off and back on as needed.
Further readings #
Sources and recommended, further resources on the topic:
License
How to highlight the current line in Vim 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/how-to-hightlight-current-line-in-vim">How to highlight the current line in Vim</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.