vim

How to Highlight the Current Line in Vim

Have you ever lost editing a huge text file in the Vim editor and wondered where the cursor is? If yes, then Vim addresses this difficulty very efficiently. In Vim you can highlight the line and even the column on which the cursor is currently positioned. Moreover, you can always customize the highlighting of the line according to your preference to make it less distracting.

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.

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:

:set cursorline

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:

:set nocursorline

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.

:set cursorcolumn

Similarly, place the same command in the vimrc file to enable it for all the sessions.

To disable column highlights, use:

:set nocursorcolumn

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:

highlight cursorline ctermbg=<color_value> ctermfg=<color_value>

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:

:help cterm

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.

highlight cursorline ctermbg=47 ctermfg=240

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.

highlight cursorcolumn ctermbg=226 ctermfg=240

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:

noremap <C-h> : set cursorline! cursorcolumn!<CR>

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.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.