Whether you are a pro or a novice Emacs user, there are some basic things that are worth knowing such as how to highlight the current line. By default, Emacs doesn’t highlight the current line. You might have been used to highlighting the current line for quick access and better user experience when coming from other text editors.
Even in Emacs, it’s possible to customize it to highlight the current line to increase the visibility. When defining how to highlight the current line, you can specify what color to use for the background and foreground of the highlighted current line.
Why Highlight the Current Line in Emacs?
Knowing how to highlight the current line in Emacs offers you the following benefits:
-
- Better Navigation and Visual Clarity – Quickly noticing the current line improves your navigation of the opened file. That way, you will have a better focus when scrolling through the Emacs file.
- Better Readability – Your overall readability in Emacs will improve once you enable highlighting the current line. For instance, if in the programming mode, you can easily read the current line as it stands out over the others. Moreover, it reduces eye strain.
How to Highlight the Current Line in Emacs
When you open an Emacs file, the current line as you scroll doesn’t get highlighted which limits the visual clarity. We can see no highlight of the current file in the following Emacs file:
To change this, let’s start by accessing the Emacs config file. Open your “.emacs” or “.emacs.d/init.el” file.
Emacs offers the “global-hl-line-mode” feature which allows the users to activate the highlighting of the current line. Once your Emacs config file opens, add the following line to activate the line highlighting:
Save the configuration file by pressing the “C-x c-s” keys. You will get an output showing that the file has been written which means that it has been saved.
Restart Emacs or close the buffer and open Emacs again for the changes to apply. By reopening the previous file on Emacs, we can now notice that the current line is highlighted. With this option, we can navigate the file and we have a better visual of the current line.
Change the Background and Foreground Colors of the Highlighted Line
The default background and foreground colors for the currently highlighted line in Emacs can be changed. If you do not feel okay with the background color, use the “set-face-background” attribute and add the color code that you prefer.
Open your Emacs again and add the following line:
For our case, we are using the #00FF00 color code for the green color. Change it to suit your preference.
Save the configuration file by pressing “c-x c-s” and restart Emacs.
Notice how we now have a new background color for the currently highlighted line in Emacs.
You can also change the foreground color by adding the “set-face-foreground” attribute and specifying the color code to use. For instance, to set the foreground to red, add the following line:
Similarly, you must save the configuration file and restart Emacs for the changes to apply.
We now have red as our foreground color for the currently highlighted line in Emacs.
You can change the color code and use any supported color that you are comfortable with. The aim is to get better visuals when using Emacs, and the choice of color is individual. Feel free to try out different colors and see how they appear. That way, you will make a better choice of which color to use.
Conclusion
Emacs only highlights the current line if you enable the feature. The “global-hl-line-mode” enables the line highlighting. Once you activate it, you can also specify what color to use as the background and foreground of the highlighted line. We discussed about highlighting the current line in Emacs and provided the steps to follow to activate and customize it. Enjoy!