Highlighting the current line can be helpful to edit the specific line without mistakenly messing up the other lines. In this tutorial, I will be exploring how to highlight the current line and column in the Vim editor to make your work error-free.
- How to Highlight the Current Line in Vim
- How to Highlight the Current Column in Vim
- How to Customize the Highlight of the Line and Column in Vim
- Conclusion
For this tutorial, I am using a text file with content mentioned in the following image:
The default cursor can be seen at the beginning of the second line.
How to Highlight the Current Line in Vim
Use the cursorline command to highlight the current line in the Vim editor. For the current session use it with the set command:
To make the cursor highlight permanent, open the vimrc file and place the above command without the colon (:).
Save the file and now, the line highlight will not go away at the beginning of each new session.
To remove the highlight of the current line temporarily, use:
Or remove the command from the vimrc file and save it.
How to Highlight the Current Column in Vim
The procedure of highlighting the current column is similar to the method mentioned in the above section. Use the :set cursorcolumn command to enable the column highlight for the current session.
Similarly, place the same command in the vimrc file to enable it for all the sessions.
To disable column highlights, use:
How to Customize the Highlight of the Line and Column in Vim
Vim has unending options to customize its features. Well, why not the current line and column highlighting? We can customize the current line and current column highlights by setting the highlight command in the vimrc file.
Open the vimrc file and create a highlight for cursor line:
In the above highlight command, the option ctermbg represents the background color value while ctermfg is for the foreground color.
To learn more about cterm use the help command:
To get a list of colors that can be set visit here.
Let’s set the background color of the highlight to the shade of green 47 and keep the foreground color a shade of gray 240.
Keep the two lines mentioned below in the vimrc file because missing any line will not yield the desired output.
set cursorline
After making these changes save the file. This is how the current line highlight is looking now:
In the same way, column highlight can also be set; specify the highlight and you are all set.
set cursorcolumn
Now, press shift+zz or :wq to save the modifications and quit the file. Now the line and column highlight will look the following way:
You can change the colors to any color of your preference.
Mapping Keys to Enable and Disable the Current Line Highlight
If you are one who frequently uses the highlighting feature then I would recommend mapping the keys to quickly enable and disable it instead of typing the command.
Open the vimrc file and place the following lines in the file:
You can toggle the current line and column highlight functionality by using control+h keys. The noremap indicates the non-recursive mapping. The <C-h> is control+h, ! to enable toggle operation over command, and <CR> is the carriage return.
Conclusion
The line highlighting in Vim is a perfect way to make the Vim experience even better. It removes the obvious distraction and hassle of finding the cursor in a file with tons of lines. To highlight the current line use the :set cursorline command and for the column use :set cursorcolumn command. The highlight can be disabled at any time using the :set nocursorline and :set nocursorcolumn commands.